Passed
Push — dev ( 854d9c...2ebbae )
by Stephan
01:34 queued 11s
created

data.config.settings()   A

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 0
dl 0
loc 19
rs 10
c 0
b 0
f 0
1
from __future__ import annotations
2
3
from pathlib import Path
4
import os
5
6
import yaml
7
8
import egon
9
10
11
def paths(pid=None):
12
    """Obtain configuration file paths.
13
14
    If no `pid` is supplied, return the location of the standard
15
    configuration file. If `pid` is the string `"current"`, the
16
    path to the configuration file containing the configuration specific
17
    to the currently running process, i.e. the configuration obtained by
18
    overriding the values from the standard configuration file with the
19
    values explicitly supplied when the currently running process was
20
    invoked, is returned. If `pid` is the string `"*"` a list of all
21
    configuration belonging to currently running `egon-data` processes
22
    is returned. This can be used for error checking, because there
23
    should only ever be one such file.
24
    """
25
    pid = os.getpid() if pid == "current" else pid
26
    insert = f".pid-{pid}" if pid is not None else ""
27
    filename = f"egon-data{insert}.configuration.yaml"
28
    if pid == "*":
29
        return [p.absolute() for p in Path(".").glob(filename)]
30
    else:
31
        return [(Path(".") / filename).absolute()]
32
33
34
# TODO: Add a command for this, so it's easy to double check the
35
#       configuration.
36
def settings() -> dict[str, dict[str, str]]:
37
    """Return a nested dictionary containing the configuration settings.
38
39
    It's a nested dictionary because the top level has command names as keys
40
    and dictionaries as values where the second level dictionary has command
41
    line switches applicable to the command as keys and the supplied values
42
    as values.
43
44
    So you would obtain the ``--database-name`` configuration setting used
45
    by the current invocation of of ``egon-data`` via
46
47
    .. code-block:: python
48
49
        settings()["egon-data"]["--database-name"]
50
51
    """
52
    files = paths(pid="*") + paths()
53
    with open(files[0]) as f:
54
        return yaml.safe_load(f)
55
56
57
def datasets(config_file=None):
58
    """Return dataset configuration.
59
60
    Parameters
61
    ----------
62
    config_file : str, optional
63
        Path of the dataset configuration file in YAML format. If not
64
        supplied, a default configuration shipped with this package is
65
        used.
66
67
    Returns
68
    -------
69
    dict
70
        A nested dictionary containing the configuration as parsed from
71
        the supplied file, or the default configuration if no file was
72
        given.
73
74
    """
75
    if not config_file:
76
        package_path = egon.data.__path__[0]
77
        config_file = os.path.join(package_path, "datasets.yml")
78
79
    return yaml.load(open(config_file), Loader=yaml.SafeLoader)
80