|
1 | 1 | import os |
| 2 | +from collections.abc import MutableMapping as DictMixin |
| 3 | +from importlib.resources import files |
2 | 4 | from shutil import copyfile |
3 | 5 |
|
4 | 6 | from appdirs import user_config_dir |
5 | 7 | from loguru import logger |
6 | | -from norns import config as cfg |
| 8 | +from yaml import dump, full_load |
7 | 9 |
|
8 | 10 | __all__ = ["config", "manage_config"] |
9 | 11 |
|
10 | | -config = cfg("genomepy", default="config/default.yaml") |
11 | 12 |
|
| 13 | +class Config(DictMixin): |
| 14 | + """Configuration class. |
12 | 15 |
|
13 | | -def generate_config(): |
| 16 | + State will be shared across config objects. |
| 17 | + """ |
| 18 | + |
| 19 | + # Store shared state, Borg pattern |
| 20 | + __shared_state = {} |
14 | 21 | config_dir = user_config_dir("genomepy") |
15 | | - new_config = os.path.join(config_dir, "genomepy.yaml") |
| 22 | + config_file = os.path.join(config_dir, "genomepy.yaml") |
| 23 | + default_config_file = str(files("genomepy.config").joinpath("default.yaml")) |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + """ |
| 27 | + Create a Config object and read a config file. |
| 28 | + """ |
| 29 | + self.__dict__ = self.__shared_state |
| 30 | + |
| 31 | + # Lookup the config file according to XDG hierarchy |
| 32 | + if not os.path.exists(self.config_dir): |
| 33 | + os.makedirs(self.config_dir) |
| 34 | + if not os.path.exists(self.config_file): |
| 35 | + copyfile(self.default_config_file, self.config_file) |
| 36 | + self.config = {} |
| 37 | + self.load(self.config_file) |
| 38 | + |
| 39 | + def load(self, path=None): |
| 40 | + """ |
| 41 | + Load yaml-formatted config file. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + path : str | None |
| 46 | + path to config file |
| 47 | + """ |
| 48 | + if path is None: |
| 49 | + path = self.config_file |
| 50 | + with open(path) as f: |
| 51 | + self.config = full_load(f) |
| 52 | + if self.config is None: |
| 53 | + logger.warning("config file is empty!") |
| 54 | + self.config = {} |
| 55 | + |
| 56 | + def save(self, path=None): |
| 57 | + """ |
| 58 | + Save current state of config dictionary. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + path : str | None |
| 63 | + path to config file |
| 64 | + """ |
| 65 | + if path is None: |
| 66 | + path = self.config_file |
| 67 | + with open(path, "w") as f: |
| 68 | + f.write(dump(self.config, default_flow_style=False)) |
| 69 | + |
| 70 | + def __getitem__(self, key): |
| 71 | + return self.config.__getitem__(key) |
16 | 72 |
|
17 | | - # existing config must be removed before norns picks up the default again |
18 | | - os.makedirs(config_dir, exist_ok=True) |
19 | | - if os.path.exists(new_config): |
20 | | - os.remove(new_config) |
| 73 | + def __delitem__(self, key): |
| 74 | + self.config.__delitem__(key) |
21 | 75 |
|
22 | | - default_config = cfg("genomepy", default="config/default.yaml").config_file |
23 | | - copyfile(default_config, new_config) |
24 | | - config.config_file = new_config |
25 | | - logger.info(f"Created config file {new_config}") |
| 76 | + def __setitem__(self, key, value): |
| 77 | + return self.config.__setitem__(key, value) |
| 78 | + |
| 79 | + def __len__(self): |
| 80 | + return self.config.__len__() |
| 81 | + |
| 82 | + def __iter__(self): |
| 83 | + return self.config.__iter__() |
| 84 | + |
| 85 | + def __str__(self): |
| 86 | + return ( |
| 87 | + "{" |
| 88 | + + ", ".join( |
| 89 | + ": ".join([str(key), str(value)]) for key, value in self.items() |
| 90 | + ) |
| 91 | + + "}" |
| 92 | + ) |
| 93 | + |
| 94 | + def keys(self): |
| 95 | + return self.config.keys() |
| 96 | + |
| 97 | + |
| 98 | +config = Config() |
| 99 | + |
| 100 | + |
| 101 | +def generate_config(): |
| 102 | + # overwrite existing config |
| 103 | + copyfile(config.default_config_file, config.config_file) |
| 104 | + config.load() |
| 105 | + logger.info(f"Created config file {config.config_file}") |
26 | 106 |
|
27 | 107 |
|
28 | 108 | def manage_config(command): |
|
0 commit comments