Config load from json file
| 16 | |
| 17 | |
| 18 | class Config(object): |
| 19 | """Config load from json file |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, config=None, config_file=None): |
| 23 | if config_file: |
| 24 | with open(config_file, 'r') as fin: |
| 25 | config = json.load(fin) |
| 26 | |
| 27 | self.dict = config |
| 28 | if config: |
| 29 | self._update(config) |
| 30 | |
| 31 | def __getitem__(self, key): |
| 32 | return self.dict[key] |
| 33 | |
| 34 | def __contains__(self, item): |
| 35 | return item in self.dict |
| 36 | |
| 37 | def items(self): |
| 38 | return self.dict.items() |
| 39 | |
| 40 | def add(self, key, value): |
| 41 | """Add key value pair |
| 42 | """ |
| 43 | self.__dict__[key] = value |
| 44 | |
| 45 | def _update(self, config): |
| 46 | if not isinstance(config, dict): |
| 47 | return |
| 48 | |
| 49 | for key in config: |
| 50 | if isinstance(config[key], dict): |
| 51 | config[key] = Config(config[key]) |
| 52 | |
| 53 | if isinstance(config[key], list): |
| 54 | config[key] = [Config(x) if isinstance(x, dict) else x for x in |
| 55 | config[key]] |
| 56 | |
| 57 | self.__dict__.update(config) |
no outgoing calls
no test coverage detected