Skip to content

Commit 25330df

Browse files
Merge pull request #312 from mpsonntag/convNaming
[Converter] Change same name section renaming
2 parents 4df4314 + 76e908a commit 25330df

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

odml/tools/version_converter.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,34 +370,45 @@ def _handle_value(self, value, log_id):
370370
@classmethod
371371
def _replace_same_name_entities(cls, tree):
372372
"""
373-
Changes same section names in the doc by adding <-{index}> to the next section occurrences.
373+
Changes same section names in the doc by adding <-{index}>
374+
to the next section occurrences.
374375
:param tree: ElementTree of the doc
375376
:return: ElementTree
376377
"""
377378
sec_map = {}
378379
prop_map = {}
379380
root = tree.getroot()
380381
for sec in root.iter("section"):
381-
n = sec.find("name")
382-
if n is not None:
383-
cls._change_entity_name(sec_map, n)
382+
383+
sec_name = sec.find("name")
384+
if sec_name is not None:
385+
cls._change_entity_name(tree, sec_map, sec_name)
384386
else:
385387
raise Exception("Section attribute name is not specified")
388+
386389
for prop in sec.iter("property"):
387390
if prop.getparent() == sec:
388-
n = prop.find("name")
389-
if n is not None:
390-
cls._change_entity_name(prop_map, n)
391+
prop_name = prop.find("name")
392+
if prop_name is not None:
393+
cls._change_entity_name(tree, prop_map, prop_name)
391394
prop_map.clear()
392395
return tree
393396

394397
@staticmethod
395-
def _change_entity_name(elem_map, name):
396-
if name.text not in elem_map:
397-
elem_map[name.text] = 1
398+
def _change_entity_name(tree, elem_map, name):
399+
"""
400+
Adds numbering to identical element names where their odml.Section
401+
or odml.Property parents reside on the same level in the tree.
402+
:param tree: The element tree containing the 'name' element.
403+
:param elem_map: lxml path to occurrence maps of named Sections or Properties.
404+
:param name: lxml element containing the name text of a Section or Property.
405+
"""
406+
named_path = "%s:%s" % (tree.getpath(name.getparent().getparent()), name.text)
407+
if named_path not in elem_map:
408+
elem_map[named_path] = 1
398409
else:
399-
elem_map[name.text] += 1
400-
name.text += "-" + str(elem_map[name.text])
410+
elem_map[named_path] += 1
411+
name.text += "-" + str(elem_map[named_path])
401412

402413
def _log(self, msg):
403414
"""

0 commit comments

Comments
 (0)