Skip to content

findconfig

Module for discovering configuration files (entries)

get_config_roots(_)

Try <parent>/config folders iteratively if we're in a Jupyter (IPython) environment

Source code in gamma/config/findconfig.py
@dispatch
def get_config_roots(_: Literal["JUPYTER"]) -> Optional[List[Path]]:
    """Try `<parent>/config` folders iteratively if we're in a Jupyter (IPython)
    environment"""

    if not _isnotebook():
        return None

    path = Path(".").absolute()
    while path != path.parent:
        candidate = path / "config"
        if candidate.is_dir():
            return [candidate]
        path = path.parent

    return None

get_entries(folder)

Get all entries in a given folder.

Source code in gamma/config/findconfig.py
@dispatch
def get_entries(folder: Path) -> List[Tuple[str, Any]]:
    """Get all entries in a given folder."""
    paths = list(folder.glob("*.yaml")) + list(folder.glob("*.yml"))
    entries = [(p.name, p) for p in paths if not _is_meta(p)]

    meta = load_meta()
    for subfolder in meta["include_folders"]:
        if subfolder is None:
            continue
        subpath: Path = folder / subfolder
        if subpath.is_dir():
            for k, e in get_entries(subpath):
                entries.append((k, e))

    return entries

load_dotenv(root=None)

Load dotenv files located in:

  • $PWD/config.local.env
  • {config_root}/../config.local.env
  • $PWD/config.env
  • {config_root}/../config.env
Source code in gamma/config/findconfig.py
def load_dotenv(root: Path = None):
    """Load dotenv files located in:

    - `$PWD/config.local.env`
    - `{config_root}/../config.local.env`
    - `$PWD/config.env`
    - `{config_root}/../config.env`

    """

    import dotenv

    home = root.parent
    dotenv.load_dotenv("./.env")
    dotenv.load_dotenv(f"{home}/.env")
    dotenv.load_dotenv("./config.local.env")
    dotenv.load_dotenv(f"{home}/config.local.env")
    dotenv.load_dotenv("./config.env")
    dotenv.load_dotenv(f"{home}/config.env")

load_meta(config_roots)

Load the XX-meta.yaml file in a given folder.

Source code in gamma/config/findconfig.py
@dispatch
def load_meta(config_roots: List[Path]) -> dict:
    """Load the `XX-meta.yaml` file in a given folder."""

    from . import to_dict
    from .merge import merge_nodes

    meta_paths = []
    for root in config_roots:
        for p in root.glob("*-meta.yaml"):
            if re.match(META_PATTERN, p.name):
                meta_paths.append(p)

    if len(meta_paths) == 0:
        return to_dict(ConfigNode(load_node(DEFAULT_META)))

    elif len(meta_paths) == 1:
        return to_dict(ConfigNode(load_node(meta_paths[0])))

    else:
        meta = [ConfigNode(load_node(p))._node for p in meta_paths]
        _, meta = merge_nodes(meta)
        return to_dict(meta, config=None)

set_config_roots(modules)

Manually set the config roots living inside packages.

This function resets the global config.

Parameters:

Name Type Description Default
modules List[ModuleType]

List of modules to search for configs.

required
Source code in gamma/config/findconfig.py
@dispatch
def set_config_roots(modules: List[ModuleType]):
    """Manually set the config roots living inside packages.

    This function resets the global config.

    Args:
        modules: List of modules to search for configs.
    """

    from itertools import chain

    paths = [m.__path__ for m in modules]
    paths = list(chain(*paths))
    paths = list(set(paths))
    set_config_roots(paths)