Skip to content

Commit

Permalink
undefine already defined item + remove mark feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jscotka committed Apr 6, 2022
1 parent 53a7a54 commit bf9faa6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions examples/no_inherit/a/main.fmf
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/inherited:
special!: 2
/stop_inherit:
c!: stop
/no_c:
test: x
1 change: 1 addition & 0 deletions examples/no_inherit/main.fmf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
special!: 1
x: X
c: cc
/a:
b: b
x!: abc
Expand Down
27 changes: 22 additions & 5 deletions fmf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def __init__(self, data, name=None, parent=None):
# (needed to prevent removing nodes with an empty dict).
self._updated = False

# stop inheritance item
self.stop_inheritance_list = list()

# Store symlinks in while walking tree in grow() to detect
# symlink loops
if parent is None:
Expand Down Expand Up @@ -218,6 +221,16 @@ def init(path):
root, error))
return root

def _stop_inherit_item(self, item: str):
if item in self.stop_inheritance_list:
print(f"{item} already")
return item[:-1]
elif item.endswith('!'):
print(f"{item} ADD")
self.stop_inheritance_list.append(item)
return item[:-1]
return None

def merge(self, parent=None):
""" Merge parent data """
# Check parent, append source files
Expand All @@ -230,12 +243,12 @@ def merge(self, parent=None):
# Merge child data with parent da
for key, value in parent.data.items():
# avoid to copy data if marked as stop inheritance
if not key.endswith('!'):
print(f"no inherit {value}")
if key + "!" not in parent.stop_inheritance_list:
data[key] = copy.deepcopy(value)

self._merge_special(data, self.data)
self.data = data
for key, value in data.items():
item = self._stop_inherit_item(key) or key
self.data[item] = data[key]

def inherit(self):
""" Apply inheritance """
Expand Down Expand Up @@ -274,7 +287,11 @@ def update(self, data):
self.child(name, value)
# Update regular attributes
else:
self.data[key] = value
item = self._stop_inherit_item(key)
if item:
self.data[item] = data[key]
else:
self.data[key] = value
log.debug("Data for '{0}' updated.".format(self))
log.data(pretty(self.data))

Expand Down
16 changes: 12 additions & 4 deletions tests/unit/test_no_inheritance_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ def setUp(self):
self.path = EXAMPLES + "no_inherit"
self.tree = fmf.Tree(self.path)

def test(self):
def test_base(self):
root_item = self.tree.find('/')
a_item = self.tree.find('/a')
inherite_item = self.tree.find('/a/inherited')
self.assertIn("special!", root_item.data)
self.assertIn("special!", inherite_item.data)
self.assertNotIn("special!", a_item.data)
self.assertIn("special", root_item.data)
self.assertIn("special", inherite_item.data)
self.assertNotIn("special", a_item.data)

def test_undefine(self):
c_item = self.tree.find("/a/stop_inherit")
no_c_item = self.tree.find("/a/stop_inherit/no_c")
self.assertNotIn("special", c_item.data)
self.assertNotIn("special", no_c_item.data)
self.assertIn("c", c_item.data)
self.assertNotIn("c", no_c_item.data)

0 comments on commit bf9faa6

Please sign in to comment.