Skip to content

Commit 2bcb167

Browse files
committed
simplify update_cape_configs and update tests
Ideally key collisions across multiple configs could combine data, so multiple values could be generated across configs. For example, config kevoreilly#1 generates the following: {"cfg1": {"key": "value1"} While config kevoreilly#2 generates: {"cfg1": {"key": "value2"} And their combined config becomes: {"cfg1": {"key": ["value1", "value2"]}} The existing behavior is to let the last config win on key collisions, resulting instead in: {"cfg1": {"key": "value2"}} So reflect that in the new test coverage, and simplify the config update method by forwarding the cape_name.
1 parent ddb060a commit 2bcb167

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

modules/processing/CAPE.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def process_file(self, file_path, append_file, metadata: dict, *, category: str,
298298

299299
if cape_name and cape_name not in executed_config_parsers[tmp_path]:
300300
tmp_config = static_config_parsers(cape_name, tmp_path, tmp_data)
301-
self.update_cape_configs(tmp_config)
301+
self.update_cape_configs(cape_name, tmp_config)
302302
executed_config_parsers[tmp_path].add(cape_name)
303303

304304
if type_string:
@@ -311,7 +311,7 @@ def process_file(self, file_path, append_file, metadata: dict, *, category: str,
311311
if tmp_config:
312312
cape_names.add(cape_name)
313313
log.info("CAPE: config returned for: %s", cape_name)
314-
self.update_cape_configs(tmp_config)
314+
self.update_cape_configs(cape_name, tmp_config)
315315

316316
self.add_family_detections(file_info, cape_names)
317317

@@ -389,23 +389,16 @@ def run(self):
389389
self.process_file(filepath, False, meta.get(filepath, {}), category=category, duplicated=duplicated)
390390
return self.cape
391391

392-
def update_cape_configs(self, config):
392+
def update_cape_configs(self, cape_name, config):
393393
"""Add the given config to self.cape["configs"]."""
394394
if not config:
395395
return
396396

397-
updated = False
397+
# look for an existing config matching cape_name; merge them if found
398+
for existing_config in self.cape["configs"]:
399+
if cape_name in existing_config:
400+
existing_config[cape_name].update(config[cape_name])
401+
return
398402

399-
for name, data in config.items():
400-
break
401-
402-
# Some families may have multiple configs. Squash them all together.
403-
if name not in self.cape["configs"]:
404-
for current in self.cape["configs"]:
405-
if name == list(current.keys())[0]:
406-
current[name].update(data)
407-
updated = True
408-
break
409-
410-
if updated is False:
411-
self.cape["configs"].append(config)
403+
# first time this cape_name config was seen
404+
self.cape["configs"].append(config)

tests/test_processing.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,45 @@ class TestConfigUpdates:
77
def test_update_no_config(self):
88
cape_proc_module = CAPE()
99
name, config = "Family", None
10-
cape_proc_module.update_cape_configs(config)
10+
cape_proc_module.update_cape_configs("Family", config)
1111
assert cape_proc_module.cape["configs"] == []
1212

1313
def test_update_empty_config(self):
1414
cape_proc_module = CAPE()
1515
name, config = "Family", {}
16-
cape_proc_module.update_cape_configs(config)
16+
cape_proc_module.update_cape_configs("Family", config)
1717
assert cape_proc_module.cape["configs"] == []
1818

1919
def test_update_single_config(self):
2020
cape_proc_module = CAPE()
2121
cfg = {"Family": {"SomeKey": "SomeValue"}}
22-
cape_proc_module.update_cape_configs(cfg)
22+
cape_proc_module.update_cape_configs("Family", cfg)
2323
expected_cfgs = [cfg]
2424
assert cape_proc_module.cape["configs"] == expected_cfgs
2525

2626
def test_update_multiple_configs(self):
2727
cape_proc_module = CAPE()
2828
cfg1 = {"Family": {"SomeKey": "SomeValue"}}
2929
cfg2 = {"Family": {"AnotherKey": "AnotherValue"}}
30-
cape_proc_module.update_cape_configs(cfg1)
31-
cape_proc_module.update_cape_configs(cfg2)
30+
cape_proc_module.update_cape_configs("Family", cfg1)
31+
cape_proc_module.update_cape_configs("Family", cfg2)
3232
expected_cfgs = [{"Family": {"AnotherKey": "AnotherValue", "SomeKey": "SomeValue"}}]
3333
assert cape_proc_module.cape["configs"] == expected_cfgs
3434

3535
def test_update_different_families(self):
3636
cape_proc_module = CAPE()
3737
cfg1 = {"Family1": {"SomeKey": "SomeValue"}}
3838
cfg2 = {"Family2": {"SomeKey": "SomeValue"}}
39-
cape_proc_module.update_cape_configs(cfg1)
40-
cape_proc_module.update_cape_configs(cfg2)
39+
cape_proc_module.update_cape_configs("Family", cfg1)
40+
cape_proc_module.update_cape_configs("Family", cfg2)
4141
expected_cfgs = [{"Family1": {"SomeKey": "SomeValue"}}, {"Family2": {"SomeKey": "SomeValue"}}]
4242
assert cape_proc_module.cape["configs"] == expected_cfgs
4343

44-
def test_update_same_family(self):
44+
def test_update_same_family_overwrites(self):
4545
cape_proc_module = CAPE()
4646
cfg1 = {"Family": {"SomeKey": "SomeValue"}}
4747
cfg2 = {"Family": {"SomeKey": "DifferentValue"}}
48-
cape_proc_module.update_cape_configs(cfg1)
49-
cape_proc_module.update_cape_configs(cfg2)
50-
expected_cfg = [{"Family": {"SomeKey": ["SomeValue", "DifferentValue"]}}]
48+
cape_proc_module.update_cape_configs("Family", cfg1)
49+
cape_proc_module.update_cape_configs("Family", cfg2)
50+
expected_cfg = [{"Family": {"SomeKey": "DifferentValue"}}]
5151
assert cape_proc_module.cape["configs"] == expected_cfg

0 commit comments

Comments
 (0)