Skip to content

load_config

Load the configuration file.

Parameters:

Name Type Description Default
path Path | None

Path to get the configuration from.

None
directory Path | None

Where to look for the configuration.

None
Source code in src/view/config.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def load_config(
    path: Path | None = None,
    *,
    directory: Path | None = None,
) -> Config:
    """Load the configuration file.

    Args:
        path: Path to get the configuration from.
        directory: Where to look for the configuration."""
    paths = (
        "view.toml",
        "view.json",
        "view.ini",
        "view.yaml",
        "view.yml",
        "view_config.py",
        "config.py",
    )

    if path:
        if directory:
            return Config.load(directory / path)
            # idk why someone would do this, but i guess its good to support it
        return Config.load(path)

    for i in paths:
        p = Path(i) if not directory else directory / i

        if not p.exists():
            continue

        if p.suffix == ".py":
            spec = importlib.util.spec_from_file_location(str(p))
            assert spec, "spec is none"
            mod = importlib.util.module_from_spec(spec)
            assert mod, "mod is none"
            sys.modules[p.stem] = mod
            assert spec.loader, "spec.loader is none"
            spec.loader.exec_module(mod)
            return Config.wrap_module(mod)

        return Config.load(p)

    return Config()