Skip to content

Commit 23ad8f1

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 4868ffa commit 23ad8f1

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
@@ -288,7 +288,7 @@ def process_file(self, file_path, append_file, metadata: dict, *, category: str,
288288

289289
if cape_name and cape_name not in executed_config_parsers[tmp_path]:
290290
tmp_config = static_config_parsers(cape_name, tmp_path, tmp_data)
291-
self.update_cape_configs(tmp_config)
291+
self.update_cape_configs(cape_name, tmp_config)
292292
executed_config_parsers[tmp_path].add(cape_name)
293293

294294
if type_string:
@@ -301,7 +301,7 @@ def process_file(self, file_path, append_file, metadata: dict, *, category: str,
301301
if tmp_config:
302302
cape_names.add(cape_name)
303303
log.info("CAPE: config returned for: %s", cape_name)
304-
self.update_cape_configs(tmp_config)
304+
self.update_cape_configs(cape_name, tmp_config)
305305

306306
self.add_family_detections(file_info, cape_names)
307307

@@ -379,23 +379,16 @@ def run(self):
379379
self.process_file(filepath, False, meta.get(filepath, {}), category=category, duplicated=duplicated)
380380
return self.cape
381381

382-
def update_cape_configs(self, config):
382+
def update_cape_configs(self, cape_name, config):
383383
"""Add the given config to self.cape["configs"]."""
384384
if not config:
385385
return
386386

387-
updated = False
387+
# look for an existing config matching cape_name; merge them if found
388+
for existing_config in self.cape["configs"]:
389+
if cape_name in existing_config:
390+
existing_config[cape_name].update(config[cape_name])
391+
return
388392

389-
for name, data in config.items():
390-
break
391-
392-
# Some families may have multiple configs. Squash them all together.
393-
if name not in self.cape["configs"]:
394-
for current in self.cape["configs"]:
395-
if name == list(current.keys())[0]:
396-
current[name].update(data)
397-
updated = True
398-
break
399-
400-
if updated is False:
401-
self.cape["configs"].append(config)
393+
# first time this cape_name config was seen
394+
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)