Skip to content

Commit

Permalink
Add encoding to load_config (#370)
Browse files Browse the repository at this point in the history
<!-- Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [x] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR fixes #xyz
- [x] (If applicable) Documentation has been added / updated (for bug
fixes / features).
- [x] (If applicable) Tests have been added.
- [x] This PR does not seem to break the templates.
- [x] CHANGES.rst has been updated (with summary of main changes).
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added.

### What kind of change does this PR introduce?

* Adds an `encoding` parameter to `load_config`.

### Does this PR introduce a breaking change?


### Other information:

* For when Québec should be Québec :)
  • Loading branch information
RondeauG authored Mar 19, 2024
2 parents 640cbfa + 2fc0e74 commit c138b91
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Internal changes
* Updated `ruff` to v0.2.0 and `black` to v24.2.0.
* Added more tests. (:pull:`366`, :pull:`367`).
* Refactored ``xs.spatial.subset`` into smaller functions. (:pull:`367`).
* An `encoding` argument was added to ``xs.config.load_config``. (:pull:`370`).

Bug fixes
^^^^^^^^^
Expand Down
8 changes: 6 additions & 2 deletions xscen/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def args_as_str(*args: tuple[Any, ...]) -> tuple[str, ...]:
return tuple(new_args)


def load_config(*elements, reset: bool = False, verbose: bool = False):
def load_config(
*elements, reset: bool = False, encoding: str = None, verbose: bool = False
):
"""Load configuration from given files or key=value pairs.
Once all elements are loaded, special sections are dispatched to their module, but only if
Expand All @@ -151,6 +153,8 @@ def load_config(*elements, reset: bool = False, verbose: bool = False):
"key=value" pairs are set last, after all files are being processed.
reset: bool
If True, the current config is erased before loading files.
encoding: str, optional
The encoding to use when reading files.
verbose: bool
if True, each element triggers a INFO log line.
Expand Down Expand Up @@ -184,7 +188,7 @@ def load_config(*elements, reset: bool = False, verbose: bool = False):
configfiles = [file]

for configfile in configfiles:
with configfile.open() as f:
with configfile.open(encoding=encoding) as f:
recursive_update(CONFIG, yaml.safe_load(f))
if verbose:
logger.info(f"Updated the config with {configfile}.")
Expand Down

0 comments on commit c138b91

Please sign in to comment.