aboutsummaryrefslogtreecommitdiffstats
path: root/chocula/config.py
blob: 3bd8ade8bde7647b067b5159522102122b396086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)