Skip to content

Commit

Permalink
utils: use recursive update on config
Browse files Browse the repository at this point in the history
  • Loading branch information
sandre35 committed Feb 27, 2024
1 parent 4301419 commit ab06a5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ChangeLog
8.2 (unreleased)
----------------

- Nothing changed yet.
- Use recursive update on grocker config


8.1 (2024-02-23)
Expand Down
10 changes: 10 additions & 0 deletions src/grocker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
import importlib_resources as resources # type: ignore


def deep_update(initial_mapping, updating_mapping):
updated_mapping = initial_mapping.copy()
for k, v in updating_mapping.items():
if k in updated_mapping and isinstance(updated_mapping[k], dict) and isinstance(v, dict):
updated_mapping[k] = deep_update(updated_mapping[k], v)
else:
updated_mapping[k] = v
return updated_mapping


def copy_resources(package, destination):
os.makedirs(destination, exist_ok=True)

Expand Down
6 changes: 2 additions & 4 deletions src/grocker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ def parse_config(config_paths, **kwargs):

for config_path in config_paths:
project_config = helpers.load_yaml(config_path)
config.update(project_config or {})
config = helpers.deep_update(config, project_config or {})

config.update({k: v for k, v in kwargs.items() if v})

return config
return helpers.deep_update(config, {k: v for k, v in kwargs.items() if v})


class GrockerRequirement:
Expand Down
12 changes: 9 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import textwrap
import unittest

import yaml

import grocker.builders as grocker_builders
import grocker.utils as grocker_utils

Expand All @@ -25,7 +27,7 @@ def mkchtmpdir():

def write_file(directory, name, content):
with open(os.path.join(directory, name), 'w') as fp:
fp.write(textwrap.dedent(content))
fp.write(yaml.dump(yaml.safe_load(content)))


class ConfigTestCase(unittest.TestCase):
Expand All @@ -37,11 +39,14 @@ class ConfigTestCase(unittest.TestCase):

first_config_content = """
file: first.yml
dependencies: first.yml
dependencies:
run: first.yml
"""[1:-1]

second_config_content = """
file: second.yml
dependencies:
build: second.yml
runtime: second.yml
"""[1:-1]

Expand All @@ -54,7 +59,8 @@ def test_parse_config(self):
config = grocker_utils.parse_config(['first.yml', 'second.yml'])
self.assertNotIn('not_used_key', config) # .grocker.yml is not read
self.assertIn('entrypoint_name', config) # grocker internal config is read
self.assertEqual(config.get('dependencies'), 'first.yml') # from first.yml
self.assertEqual(config.get('dependencies').get('run'), 'first.yml') # from first.yml
self.assertEqual(config.get('dependencies').get('build'), 'second.yml') # from second.yml
self.assertEqual(config.get('runtime'), 'second.yml') # from second.yml
self.assertEqual(config.get('file'), 'second.yml') # from second.yml

Expand Down

0 comments on commit ab06a5e

Please sign in to comment.