Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable deep update of grocker config #182

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 10 additions & 5 deletions src/grocker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
import subprocess # noqa: S404
import tempfile
import time
from importlib import resources

import jinja2
import yaml

try:
from importlib import resources
except ImportError:
# Python<3.9: use importlib-resources package
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):
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
Loading