Skip to content

Commit

Permalink
more control over merge levels. still need a cleaner solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ajul committed Jul 1, 2016
1 parent 8705b51 commit 457890c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pyradox/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,22 @@ def weakUpdate(self, other):
for key, value in other.items():
if key not in self:
self.append(key, copy.deepcopy(value))

def mergeItem(self, key, value, mergeLevels = 0):
if key in self and isinstance(self[key], Struct):
#print('Merge', value, 'into', self[key])
self[key].merge(value, mergeLevels)
#print('Result', self[key])
else:
self[key] = copy.deepcopy(value)

# -1 for fully recursive merge
def merge(self, other, mergeLevels = 0):
if mergeLevels == 0:
self += copy.deepcopy(other)
else:
for key, value in other.items():
# TODO: non-structs
if key in self:
self[key].merge(value, mergeLevels - 1)
else:
self[key] = copy.deepcopy(value)
self.mergeItem(key, value, mergeLevels - 1)

# comments

Expand Down Expand Up @@ -280,7 +285,7 @@ def prettyprint(self, level = 0, indentString = ' '):
return result

# other methods
def atDate(self, date = False):
def atDate(self, date = False, mergeLevels = -1):
"""
returns a deep copy of this tree with all date blocks at or before the specified date deep copied and promoted to the top and the rest omitted
if date is True, use all date blocks
Expand All @@ -303,7 +308,7 @@ def atDate(self, date = False):
if isinstance(item.key, pyradox.primitive.Date):
if date is True or item.key <= date:
for item in item.value._data:
result[item.key] = copy.deepcopy(item.value)
result.mergeItem(item.key, item.value, mergeLevels)
return result

class List(Struct):
Expand Down Expand Up @@ -381,6 +386,13 @@ def __getitem__(self, i):
def append(self, value, preComments = None, lineComment = None):
"""Append a new value"""
self._data.append(List._Item(value, preComments, lineComment))

def merge(self, other, mergeLevels = 0):
# TODO: recursive, aliasing
if isinstance(other, List):
self.__iadd__(other)
else:
self.append(other)

def insert(self, i, value):
"""Insert a new value"""
Expand Down

0 comments on commit 457890c

Please sign in to comment.