Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

special no inheritance mark #159

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/no_inherit/.fmf/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
6 changes: 6 additions & 0 deletions examples/no_inherit/a/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/inherited:
special!: 2
/stop_inherit:
c!: stop
/no_c:
test: x
7 changes: 7 additions & 0 deletions examples/no_inherit/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
special!: 1
x: X
c: cc
/a:
b: b
x!: abc
/inherited:
28 changes: 24 additions & 4 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 @@ -226,10 +239,16 @@ def merge(self, parent=None):
if parent is None:
return
self.sources = parent.sources + self.sources
# Merge child data with parent data
data = copy.deepcopy(parent.data)
data = {}
# Merge child data with parent da
for key, value in parent.data.items():
# avoid to copy data if marked as stop inheritance
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 @@ -268,7 +287,8 @@ def update(self, data):
self.child(name, value)
# Update regular attributes
else:
self.data[key] = value
item = self._stop_inherit_item(key) or key
self.data[item] = data[key]
log.debug("Data for '{0}' updated.".format(self))
log.data(pretty(self.data))

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_no_inheritance_mark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import unittest

import fmf

# Prepare path to examples
PATH = os.path.dirname(os.path.realpath(__file__))
EXAMPLES = PATH + "/../../examples/"


class TestNoInheritance(unittest.TestCase):
""" Verify storing modifed data to disk """

def setUp(self):
self.path = EXAMPLES + "no_inherit"
self.tree = fmf.Tree(self.path)

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)

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)