Skip to content

Commit 8fae3e3

Browse files
committed
[base/section] Add 'keep_id' flag to 'clone'
Closes #259, closes #258
1 parent 7d69442 commit 8fae3e3

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

odml/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def clean(self):
581581
for i in self:
582582
i.clean()
583583

584-
def clone(self, children=True):
584+
def clone(self, children=True, keep_id=False):
585585
"""
586586
Clone this object recursively allowing to copy it independently
587587
to another document
@@ -592,7 +592,7 @@ def clone(self, children=True):
592592
obj._sections = SmartList(BaseSection)
593593
if children:
594594
for s in self._sections:
595-
obj.append(s.clone())
595+
obj.append(s.clone(keep_id=keep_id))
596596

597597
return obj
598598

odml/section.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,26 @@ def remove(self, obj):
390390
else:
391391
raise ValueError("Can only remove sections and properties")
392392

393-
def clone(self, children=True):
393+
def clone(self, children=True, keep_id=False):
394394
"""
395-
Clone this object recursively allowing to copy it independently
396-
to another document
395+
Clone this Section allowing to copy it independently
396+
to another document. By default the id of any cloned
397+
object will be set to a new uuid.
398+
399+
:param children: If True, also clone child sections and properties
400+
recursively.
401+
:param keep_id: If this attribute is set to True, the uuids of the
402+
Section and all child objects will remain unchanged.
403+
:return: The cloned Section.
397404
"""
398-
obj = super(BaseSection, self).clone(children)
399-
obj.new_id()
405+
obj = super(BaseSection, self).clone(children, keep_id)
406+
if not keep_id:
407+
obj.new_id()
400408

401409
obj._props = base.SmartList(BaseProperty)
402410
if children:
403411
for p in self._props:
404-
obj.append(p.clone())
412+
obj.append(p.clone(keep_id))
405413

406414
return obj
407415

0 commit comments

Comments
 (0)