Skip to content

Commit

Permalink
simplify update_cape_configs and update tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nbargnesi committed Feb 9, 2023
1 parent ddb060a commit 2bcb167
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
27 changes: 10 additions & 17 deletions modules/processing/CAPE.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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)
22 changes: 11 additions & 11 deletions tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,45 @@ 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

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

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

0 comments on commit 2bcb167

Please sign in to comment.