From e25883599d96cff7d30f6bacc5124230455859dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Prchl=C3=ADk?= Date: Wed, 25 Jun 2025 16:27:36 +0200 Subject: [PATCH] Pass the original node content to adjust decision callback Because I would really like to show the diff between "before" and "after", and callback was getting just one version. --- fmf/base.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/fmf/base.py b/fmf/base.py index 31959bb..968bf76 100644 --- a/fmf/base.py +++ b/fmf/base.py @@ -39,16 +39,21 @@ class AdjustCallback(Protocol): A callback for per-rule notifications made by Tree.adjust() Function which will be called for every rule inspected by adjust(). - It will be given three arguments: fmf tree being inspected, - current adjust rule, and whether the rule was skipped (``None``), - applied (``True``) or not applied (``False``). + It will be given the following arguments: + + * fmf tree being inspected - **after ``adjust``** rule was applied, + * the current adjust rule, + * whether the rule was skipped (``None``), applied (``True``) or not + applied (``False``), + * and if the rule was applied, the original node content. """ def __call__( self, node: 'Tree', rule: Dict[str, Any], - applied: Optional[bool]) -> None: + applied: Optional[bool], + before: Optional['Tree'] = None) -> None: pass @@ -584,16 +589,22 @@ def apply_rules(rule_set): # Apply remaining rule attributes if context matches try: if context.matches(condition): - if decision_callback: - decision_callback(self, rule, True) - # Remove special keys (when, because...) from the rule apply_rule = { key: value for key, value in rule.items() if key not in ADJUST_CONTROL_KEYS } - self._merge_special(self.data, apply_rule) + + if decision_callback: + node_before = self.copy() + + self._merge_special(self.data, apply_rule) + + decision_callback(self, rule, True, before=node_before) + + else: + self._merge_special(self.data, apply_rule) # First matching rule wins, skip the rest of this set unless continue if not continue_: