-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mechanism to override defaults using a custom config file (#…
…1007) This PR implement a merging mechanism for custom app defaults configurations. Users can now drop a custom config.yml in the /home/jovyan/.aiidalab/quantumespresso/ directory. The file follows the structure of the qeapp.yml default configurations file, though it does not strictly require the inclusion of all fields. The custom config.yml file is processed and merged recursively into the object created from processing qeapp.yml.
- Loading branch information
1 parent
6d5a1d3
commit d4c495e
Showing
4 changed files
with
73 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,54 @@ | ||
from collections.abc import Mapping | ||
from importlib import resources | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
from aiidalab_qe.app import parameters | ||
|
||
|
||
def recursive_merge(d1, d2): | ||
"""Merge dictionary `d2` into dictionary `d1` recursively. | ||
- For keys that are in both dictionaries: | ||
- If values are dictionaries, merge them recursively. | ||
- Otherwise, overwrite the value in `d1` with the value in `d2`. | ||
- Keys in `d2` not in `d1` are added to `d1`. | ||
Parameters | ||
---------- | ||
`d1` : `dict` | ||
The dictionary to merge into. | ||
`d2` : `dict` | ||
The dictionary to merge from. | ||
Returns | ||
------- | ||
`dict` | ||
The merged dictionary. | ||
Examples | ||
-------- | ||
>>> d1 = {'a': 1, 'b': {'c': 2, 'd': 3}} | ||
>>> d2 = {'b': {'c': 4, 'e': 5}} | ||
>>> recursive_merge(d1, d2) | ||
{'a': 1, 'b': {'c': 4, 'd': 3, 'e': 5}} | ||
""" | ||
for key, value in d2.items(): | ||
if key in d1: | ||
if isinstance(d1[key], Mapping) and isinstance(value, Mapping): | ||
recursive_merge(d1[key], value) | ||
else: | ||
d1[key] = value | ||
else: | ||
d1[key] = value | ||
return d1 | ||
|
||
|
||
DEFAULT_PARAMETERS = yaml.safe_load(resources.read_text(parameters, "qeapp.yaml")) | ||
|
||
|
||
custom_config_file = Path.home() / ".aiidalab" / "quantumespresso" / "config.yml" | ||
if custom_config_file.exists(): | ||
custom_config = yaml.safe_load(custom_config_file.read_text()) | ||
DEFAULT_PARAMETERS = recursive_merge(DEFAULT_PARAMETERS, custom_config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters