diff --git a/modules/processing/CAPE.py b/modules/processing/CAPE.py index 5b8a4d96859..2a4e503d282 100644 --- a/modules/processing/CAPE.py +++ b/modules/processing/CAPE.py @@ -298,7 +298,7 @@ def process_file(self, file_path, append_file, metadata: dict, *, category: str, if cape_name and cape_name not in executed_config_parsers[tmp_path]: tmp_config = static_config_parsers(cape_name, tmp_path, tmp_data) - self.update_cape_configs(tmp_config) + self.update_cape_configs(cape_name, tmp_config) executed_config_parsers[tmp_path].add(cape_name) if type_string: @@ -311,7 +311,7 @@ def process_file(self, file_path, append_file, metadata: dict, *, category: str, if tmp_config: cape_names.add(cape_name) log.info("CAPE: config returned for: %s", cape_name) - self.update_cape_configs(tmp_config) + self.update_cape_configs(cape_name, tmp_config) self.add_family_detections(file_info, cape_names) @@ -389,23 +389,16 @@ def run(self): self.process_file(filepath, False, meta.get(filepath, {}), category=category, duplicated=duplicated) return self.cape - def update_cape_configs(self, config): + def update_cape_configs(self, cape_name, config): """Add the given config to self.cape["configs"].""" if not config: return - updated = False + # look for an existing config matching cape_name; merge them if found + for existing_config in self.cape["configs"]: + if cape_name in existing_config: + existing_config[cape_name].update(config[cape_name]) + return - for name, data in config.items(): - break - - # Some families may have multiple configs. Squash them all together. - if name not in self.cape["configs"]: - for current in self.cape["configs"]: - if name == list(current.keys())[0]: - current[name].update(data) - updated = True - break - - if updated is False: - self.cape["configs"].append(config) + # first time this cape_name config was seen + self.cape["configs"].append(config) diff --git a/tests/test_processing.py b/tests/test_processing.py index 47b1fcad015..eba59473e23 100644 --- a/tests/test_processing.py +++ b/tests/test_processing.py @@ -7,19 +7,19 @@ class TestConfigUpdates: def test_update_no_config(self): cape_proc_module = CAPE() name, config = "Family", None - cape_proc_module.update_cape_configs(config) + cape_proc_module.update_cape_configs("Family", config) assert cape_proc_module.cape["configs"] == [] def test_update_empty_config(self): cape_proc_module = CAPE() name, config = "Family", {} - cape_proc_module.update_cape_configs(config) + cape_proc_module.update_cape_configs("Family", config) assert cape_proc_module.cape["configs"] == [] def test_update_single_config(self): cape_proc_module = CAPE() cfg = {"Family": {"SomeKey": "SomeValue"}} - cape_proc_module.update_cape_configs(cfg) + cape_proc_module.update_cape_configs("Family", cfg) expected_cfgs = [cfg] assert cape_proc_module.cape["configs"] == expected_cfgs @@ -27,8 +27,8 @@ def test_update_multiple_configs(self): cape_proc_module = CAPE() cfg1 = {"Family": {"SomeKey": "SomeValue"}} cfg2 = {"Family": {"AnotherKey": "AnotherValue"}} - cape_proc_module.update_cape_configs(cfg1) - cape_proc_module.update_cape_configs(cfg2) + cape_proc_module.update_cape_configs("Family", cfg1) + cape_proc_module.update_cape_configs("Family", cfg2) expected_cfgs = [{"Family": {"AnotherKey": "AnotherValue", "SomeKey": "SomeValue"}}] assert cape_proc_module.cape["configs"] == expected_cfgs @@ -36,16 +36,16 @@ def test_update_different_families(self): cape_proc_module = CAPE() cfg1 = {"Family1": {"SomeKey": "SomeValue"}} cfg2 = {"Family2": {"SomeKey": "SomeValue"}} - cape_proc_module.update_cape_configs(cfg1) - cape_proc_module.update_cape_configs(cfg2) + cape_proc_module.update_cape_configs("Family", cfg1) + cape_proc_module.update_cape_configs("Family", cfg2) expected_cfgs = [{"Family1": {"SomeKey": "SomeValue"}}, {"Family2": {"SomeKey": "SomeValue"}}] assert cape_proc_module.cape["configs"] == expected_cfgs - def test_update_same_family(self): + def test_update_same_family_overwrites(self): cape_proc_module = CAPE() cfg1 = {"Family": {"SomeKey": "SomeValue"}} cfg2 = {"Family": {"SomeKey": "DifferentValue"}} - cape_proc_module.update_cape_configs(cfg1) - cape_proc_module.update_cape_configs(cfg2) - expected_cfg = [{"Family": {"SomeKey": ["SomeValue", "DifferentValue"]}}] + cape_proc_module.update_cape_configs("Family", cfg1) + cape_proc_module.update_cape_configs("Family", cfg2) + expected_cfg = [{"Family": {"SomeKey": "DifferentValue"}}] assert cape_proc_module.cape["configs"] == expected_cfg