Search Paths#

from pprint import pprint

from dicfg import ConfigReader
!cat ./configs/config.yml
config_reader = ConfigReader(name="myconfig")
default:
    config_int: 1
    config_string: "fire"
    config_list: [1, 2, 3]
    config_none: None
    config_dict:
        sub_config: "water"

Search for config file interpolation is only done in the current working directory, configs folder, and the presets folder.#

!cat ./my_other_config_dir/my_other_config.yml
100
user_config = {"myconfig": {"default": {"config_int": "my_other_config.yml"}}}
pprint(config_reader.read(user_config), sort_dicts=False)
---------------------------------------------------------------------------
ConfigNotFoundError                       Traceback (most recent call last)
Cell In [4], line 2
      1 user_config = {"myconfig": {"default": {"config_int": "my_other_config.yml"}}}
----> 2 pprint(config_reader.read(user_config), sort_dicts=False)

File ~/code/dicfg/dicfg/reader.py:106, in ConfigReader.read(self, user_config, presets)
    103 cli_config = self._read_cli(sys.argv[1:])
    105 configs = (self_config, *preset_configs, user_config, cli_config)
--> 106 configs = self._fuse_configs(configs, self._context_keys, search_paths)
    108 return merge(*configs).cast()

File ~/code/dicfg/dicfg/reader.py:147, in ConfigReader._fuse_configs(self, configs, context_keys, search_paths)
    143 def _fuse_configs(self, configs, context_keys, search_paths):
    144     fuse_config = partial(
    145         self._fuse_config, context_keys=context_keys, search_paths=search_paths
    146     )
--> 147     return tuple(map(fuse_config, configs))

File ~/code/dicfg/dicfg/reader.py:150, in ConfigReader._fuse_config(self, config, context_keys, search_paths)
    149 def _fuse_config(self, config: dict, context_keys: tuple, search_paths):
--> 150     config = _include_configs(config, search_paths)
    151     fused_config = deepcopy(
    152         {key: deepcopy(config.get("default", {})) for key in context_keys}
    153     )
    154     return merge(fused_config, config)

File ~/miniconda3/envs/mart/lib/python3.8/functools.py:874, in singledispatch.<locals>.wrapper(*args, **kw)
    870 if not args:
    871     raise TypeError(f'{funcname} requires at least '
    872                     '1 positional argument')
--> 874 return dispatch(args[0].__class__)(*args, **kw)

File ~/code/dicfg/dicfg/reader.py:193, in _include_configs_dict(config, search_paths)
    190 @_include_configs.register
    191 def _include_configs_dict(config: dict, search_paths):
    192     for key, value in config.items():
--> 193         config[key] = _include_configs(value, search_paths)
    194     return config

File ~/miniconda3/envs/mart/lib/python3.8/functools.py:874, in singledispatch.<locals>.wrapper(*args, **kw)
    870 if not args:
    871     raise TypeError(f'{funcname} requires at least '
    872                     '1 positional argument')
--> 874 return dispatch(args[0].__class__)(*args, **kw)

File ~/code/dicfg/dicfg/reader.py:193, in _include_configs_dict(config, search_paths)
    190 @_include_configs.register
    191 def _include_configs_dict(config: dict, search_paths):
    192     for key, value in config.items():
--> 193         config[key] = _include_configs(value, search_paths)
    194     return config

File ~/miniconda3/envs/mart/lib/python3.8/functools.py:874, in singledispatch.<locals>.wrapper(*args, **kw)
    870 if not args:
    871     raise TypeError(f'{funcname} requires at least '
    872                     '1 positional argument')
--> 874 return dispatch(args[0].__class__)(*args, **kw)

File ~/code/dicfg/dicfg/reader.py:184, in _include_configs_str(config, search_paths)
    181 @_include_configs.register
    182 def _include_configs_str(config: str, search_paths):
    183     if Path(config).suffix in _FILE_READERS:
--> 184         config_path = _search_config(config, search_paths)
    185         open_config = _FILE_READERS[Path(config_path).suffix](config_path)
    186         return _include_configs(open_config, search_paths)

File ~/code/dicfg/dicfg/reader.py:173, in _search_config(config_name, search_paths)
    171     if config_path.exists():
    172         return config_path
--> 173 raise ConfigNotFoundError(config_name)

ConfigNotFoundError: my_other_config.yml

Additional search paths can be set to find additional config files.#

!cat ./my_other_config_dir/my_other_config.yml
100
# adding "./my_other_config_dir" to search path
config_reader = ConfigReader(name="myconfig", search_paths=("./my_other_config_dir",))
user_config = {"myconfig": {"default": {"config_int": "my_other_config.yml"}}}
pprint(
    config_reader.read(user_config=user_config),
    sort_dicts=False,
)
{'default': {'config_int': 100,
             'config_string': 'fire',
             'config_list': [1, 2, 3],
             'config_none': 'None',
             'config_dict': {'sub_config': 'water'}}}