Skip to content

Commit

Permalink
Merge pull request #21 from martvanrijthoven/feature/multiple-user-co…
Browse files Browse the repository at this point in the history
…nfgis

Feature/multiple user confgis
  • Loading branch information
martvanrijthoven authored Sep 9, 2024
2 parents d9eb8da + 2b5a44c commit c3dfd1a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- .github/workflows/ci_tests.yml
- pyproject.toml
- "!dicfg/_version.py"
- requirements.txt

pull_request:
paths:
Expand All @@ -18,6 +19,7 @@ on:
- .github/workflows/ci_tests.yml
- pyproject.toml
- "!dicfg/_version.py"
- requirements.txt

jobs:
call-tests:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- README.md

env:
PYTHON_VERSION: "3.8"
PYTHON_VERSION: "3.10"

jobs:
docs:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
- uses: actions/checkout@v2

# Sets up python
- name: Set up Python 3.8
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.10

# Install dependencies
- name: "Installs dependencies"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set up Python 3.8
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.10

- name: release version
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
tests:
strategy:
matrix:
python: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python: ["3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand Down
52 changes: 33 additions & 19 deletions dicfg/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from copy import deepcopy
from functools import partial, singledispatch
from pathlib import Path
from typing import List, Union
from typing import List, Optional, Union

import yaml

Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(

def read(
self,
user_config: Union[dict, str, Path] = None,
user_config: Optional[Union[dict, str, Path, list[dict, str, Path]]] = None,
presets: tuple = (),
) -> dict:
"""Reads Config File
Expand All @@ -87,33 +87,47 @@ def read(
dict: read configs
"""

user_config_search_path = None
if user_config is not None and not isinstance(user_config, dict):
user_config_search_path = Path(user_config).parent

search_paths = self._set_search_paths(
user_config_search_path, self._search_paths
)

self_config = self._read(self._main_config_path)
user_config = self._read_user_config(user_config)

arg_preset_configs = self._read_presets(presets)
user_presets = user_config.pop("presets", ())
user_preset_configs = self._read_presets(user_presets)
preset_configs = arg_preset_configs + user_preset_configs

cli_config = self._read_cli(sys.argv[1:])

configs = (self_config, *preset_configs, user_config, cli_config)
user_configs = []
user_presets_configs = []
user_config_search_paths = []
if user_config is not None:
if not isinstance(user_config, list):
user_config = [user_config]

for config in user_config:
if isinstance(config, (str, Path)) and not isinstance(config, dict):
user_config_search_path = Path(config).parent
user_config_search_paths.append(user_config_search_path)

read_user_config = self._read_user_config(config)
user_presets = read_user_config.pop("presets", ())
user_configs.append(read_user_config)
user_presets_configs.extend(self._read_presets(user_presets))

configs = (
self_config,
*tuple(user_presets_configs),
*tuple(arg_preset_configs),
*tuple(user_configs),
cli_config,
)

search_paths = self._set_search_paths(
user_config_search_paths, self._search_paths
)

configs = self._fuse_configs(configs, self._context_keys, search_paths)

return merge(*configs).cast()

def _set_search_paths(self, user_config_search_path, search_paths):
def _set_search_paths(self, user_config_search_paths, search_paths):
return (
Path(),
user_config_search_path,
*tuple(user_config_search_paths),
self._configs_folder,
self._presets_folder,
*search_paths,
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ license = { file = "LICENSE" }
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PyYaml>='5.4.1'
PyYaml>=5.4.1
25 changes: 25 additions & 0 deletions tests/dicfg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,28 @@ def test_dont_build():
)
config = config_reader.read()["default"]
assert config_dont_build_build == config


def test_multiple_configs():
config_reader = ConfigReader(
name="testconfig",
main_config_path="./configs/config.yml",
)

config_1 = {
"testconfig": {
"presets": ["deepreplace.yml"],
"default": {"deep_replace": {"c": 2}},
}
}

config_2 = {
"testconfig": {
"presets": ["deepreplace.yml"],
"default": {"deep_replace": {"c": 3}},
}
}

config = config_reader.read([config_1, config_2])
test_config = build_config(config["default"])
assert test_config["deep_replace"] == {"c": 3, "d": 2}

0 comments on commit c3dfd1a

Please sign in to comment.