Skip to content

Commit

Permalink
Avoid copy() calls in adjust()
Browse files Browse the repository at this point in the history
Replace `.pop()` calls with skipping "control" keys (when,because..)
during the merge.
  • Loading branch information
lukaszachy committed Apr 11, 2024
1 parent 29190c7 commit db61340
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions fmf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
SUFFIX = ".fmf"
MAIN = "main" + SUFFIX
IGNORED_DIRECTORIES = ['/dev', '/proc', '/sys']
ADJUST_CONTROL_KEYS = ['because', 'continue', 'when']


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -220,6 +221,9 @@ def _merge_minus(self, data, key, value):
def _merge_special(self, data, source):
""" Merge source dict into data, handle special suffixes """
for key, value in sorted(source.items()):
# Merge only attribute keys, not the adjust control keys
if key in ADJUST_CONTROL_KEYS:
continue
# Handle special attribute merging
if key.endswith('+'):
self._merge_plus(data, key.rstrip('+'), value)
Expand Down Expand Up @@ -372,7 +376,7 @@ class describing the environment context. By default, the key

# Adjust rules should be a dictionary or a list of dictionaries
try:
rules = copy.deepcopy(self.data[key])
rules = self.data[key]
log.debug("Applying adjust rules for '{}'.".format(self))
log.data(rules)
if isinstance(rules, dict):
Expand All @@ -394,29 +398,24 @@ class describing the environment context. By default, the key
if not isinstance(rule, dict):
raise utils.FormatError("Adjust rule should be a dictionary.")

original_rule = rule.copy()

# Missing 'when' means always enabled rule
try:
condition = rule.pop('when')
condition = rule['when']
except KeyError:
condition = True

# The optional 'continue' key should be a bool
continue_ = rule.pop('continue', True)
continue_ = rule.get('continue', True)
if not isinstance(continue_, bool):
raise utils.FormatError(
"The 'continue' value should be bool, "
"got '{}'.".format(continue_))

# The 'because' key is reserved for optional comments (ignored)
rule.pop('because', None)

# Apply remaining rule attributes if context matches
try:
if context.matches(condition):
if decision_callback:
decision_callback(self, original_rule, True)
decision_callback(self, rule, True)

self._merge_special(self.data, rule)

Expand All @@ -425,11 +424,11 @@ class describing the environment context. By default, the key
break
else:
if decision_callback:
decision_callback(self, original_rule, False)
decision_callback(self, rule, False)
# Handle undecided rules as requested
except fmf.context.CannotDecide:
if decision_callback:
decision_callback(self, original_rule, None)
decision_callback(self, rule, None)

if undecided == 'skip':
continue
Expand Down

0 comments on commit db61340

Please sign in to comment.