Skip to content
Snippets Groups Projects
Commit 6e637742 authored by Lukas Müller's avatar Lukas Müller
Browse files

Enhanced CMDOWS class with a hack for renaming duplicate uIDs

Former-commit-id: ddc5dd7436d4415b2429fed107b0564021e372ba
parent 00237714
No related branches found
No related tags found
No related merge requests found
......@@ -111,3 +111,20 @@ class CMDOWS(object):
if not result:
logger.info('The following uIDs do not exist although they are referred to: ' + ', '.join(invalids))
return result
def resolve_uids(self):
"""Method to rename duplicate UIDs in a CMDOWS file"""
logger.warning('The resolve_uids method is a hack and should not be used.')
ids = [element.attrib['uID'] for element in self.root.xpath('.//*[@uID]')]
result = (len(ids) == len(set(ids)))
if not result:
duplicates = [k for k, v in Counter(ids).items() if v > 1]
for duplicate in duplicates:
duplicate_elements = self.root.xpath('.//*[@uID="' + duplicate + '"]')
for duplicate_id, duplicate_element in enumerate(duplicate_elements):
duplicate_element.attrib['uID'] = duplicate_element.attrib['uID'] + '_' + str(duplicate_id)
def save(self, pretty_print=False):
"""Method to save a manipulated CMDOWS file"""
ElementTree(self.root).write(self.file, pretty_print=pretty_print, method='xml', xml_declaration=True,
encoding='UTF-8')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment