diff --git a/kadmos/cmdows/__init__.py b/kadmos/cmdows/__init__.py index 3fe08165e354415129149157c204ce6b6430c6fb..b62034daeffa9f18af218380787d2d8265808d80 100644 --- a/kadmos/cmdows/__init__.py +++ b/kadmos/cmdows/__init__.py @@ -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')