|
a |
|
b/src/settings.py |
|
|
1 |
import ast |
|
|
2 |
import configparser |
|
|
3 |
from collections.abc import Mapping |
|
|
4 |
from os import getenv |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
def parse_values(config): |
|
|
8 |
config_parsed = {} |
|
|
9 |
for section in config.sections(): |
|
|
10 |
config_parsed[section] = {} |
|
|
11 |
for key, value in config[section].items(): |
|
|
12 |
config_parsed[section][key] = ast.literal_eval(value) |
|
|
13 |
return config_parsed |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
class Settings(Mapping): |
|
|
17 |
def __init__(self, setting_file=getenv('COMPNET_CONFIG')): |
|
|
18 |
|
|
|
19 |
if not setting_file: |
|
|
20 |
raise EnvironmentError('environment variable COMPNET_CONFIG is not defined, see docs/README.md for details') |
|
|
21 |
|
|
|
22 |
config = configparser.ConfigParser() |
|
|
23 |
config.read(setting_file) |
|
|
24 |
self.settings_dict = parse_values(config) |
|
|
25 |
|
|
|
26 |
def __getitem__(self, key): |
|
|
27 |
return self.settings_dict[key] |
|
|
28 |
|
|
|
29 |
def __len__(self): |
|
|
30 |
return len(self.settings_dict) |
|
|
31 |
|
|
|
32 |
def __iter__(self): |
|
|
33 |
return self.settings_dict.items() |
|
|
34 |
|