diff --git a/lib/kgenlib/__init__.py b/lib/kgenlib/__init__.py index 8e70be4..fb33857 100644 --- a/lib/kgenlib/__init__.py +++ b/lib/kgenlib/__init__.py @@ -341,7 +341,6 @@ class PatchMutationSpec(BaseModel): patch: dict conditions: MutationCondition - class BundleMutationSpec(BaseModel): filename: str conditions: MutationCondition @@ -444,6 +443,7 @@ def mutate(self, mutations: ContentMutateSpec): self.prune = action.prune if action.break_: break + for action in mutations.bundle: if self.match(action.conditions): try: @@ -465,7 +465,40 @@ def match(self, match_conditions): return True def patch(self, patch): - self.root.merge_update(Dict(patch), box_merge_lists="extend") + """ + Apply a patch to the content, supporting string formatting with content values. + + This method applies the provided patch to the current content. If the patch + contains string values that include formatting placeholders like "{content.field}", + it will attempt to format them using values from the content. + + Args: + patch (dict): The patch to apply to the content + """ + # Create a deep copy of the patch to avoid modifying the original + formatted_patch = Dict(patch.copy()) + + # Process the patch dictionary to format any strings + self._format_patch_values(formatted_patch) + + # Apply the formatted patch + self.root.merge_update(formatted_patch, box_merge_lists="extend") + + def _format_patch_values(self, patch_dict): + """ + Recursively formats string values in a patch dictionary. + + Args: + patch_dict (dict): The patch dictionary to process + """ + for key, value in patch_dict.items(): + if isinstance(value, dict): + self._format_patch_values(value) + elif isinstance(value, str) and "{content." in value: + try: + patch_dict[key] = value.format(content=self) + except (AttributeError, KeyError) as e: + logger.debug(f"Could not format '{value}': {e}") def regex_patch(self, patch): if not isinstance(patch, dict): @@ -479,7 +512,6 @@ def regex_patch(self, patch): patched_dict = yaml.safe_load(yaml_dump) self.parse(Dict(patched_dict)) - class BaseStore(GeneratorClass): content_list: List[BaseContent] = []