aboutsummaryrefslogtreecommitdiffstats
path: root/chocula/config.py
blob: 223740492f127e99ecd48b52727443c49d1d18fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

from types import SimpleNamespace
import toml

class ChoculaConfig(SimpleNamespace):

    @classmethod
    def from_file(cls, file_path="sources.toml", sources_dir="data/"):

        # TODO: can we just pass _dict=SimpleNamespace here?
        sources = toml.load(file_path)

        # convert all sub-tables to SimpleNamespace
        for k in list(sources.keys()):
            if isinstance(sources[k], dict):
                if "filename" in sources[k]:
                    sources[k]["filepath"] = sources_dir + sources[k]["filename"]
                sources[k] = SimpleNamespace(**sources[k])

        # conver the whole thing to SimpleNamespace
        return ChoculaConfig(**sources)