kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 1 | from typing import Any, Dict, Optional, Set |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 2 | |
| 3 | from .common_types import IStorable |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 4 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 5 | |
| 6 | ConfigBlock = Dict[str, Any] |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 7 | |
| 8 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 9 | class Config(IStorable): |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 10 | def __init__(self, dct: ConfigBlock) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 11 | # make mypy happy, set fake dict |
| 12 | self.__dict__['_dct'] = {} |
| 13 | self.run_uuid = None # type: str |
| 14 | self.storage_url = None # type: str |
| 15 | self.comment = None # type: str |
| 16 | self.keep_vm = None # type: bool |
| 17 | self.dont_discover_nodes = None # type: bool |
| 18 | self.build_id = None # type: str |
| 19 | self.build_description = None # type: str |
| 20 | self.build_type = None # type: str |
| 21 | self.default_test_local_folder = None # type: str |
| 22 | self.settings_dir = None # type: str |
| 23 | self.connect_timeout = None # type: int |
| 24 | self.no_tests = False # type: bool |
| 25 | self.debug_agents = False # type: bool |
| 26 | |
| 27 | # None, disabled, enabled, metadata, ignore_errors |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 28 | self.discover = None # type: Optional[str] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 29 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 30 | self.logging = None # type: 'Config' |
| 31 | self.ceph = None # type: 'Config' |
| 32 | self.openstack = None # type: 'Config' |
| 33 | self.fuel = None # type: 'Config' |
| 34 | self.test = None # type: 'Config' |
| 35 | self.sensors = None # type: 'Config' |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 36 | self.discover = None # type: Set[str] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 37 | |
| 38 | self._dct.clear() |
| 39 | self._dct.update(dct) |
| 40 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 41 | @classmethod |
| 42 | def fromraw(cls, data: Dict[str, Any]) -> 'Config': |
| 43 | return cls(data) |
| 44 | |
| 45 | def raw(self) -> Dict[str, Any]: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 46 | return self._dct |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 47 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 48 | def get(self, path: str, default: Any = None) -> Any: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 49 | curr = self |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 50 | while path: |
| 51 | if '/' in path: |
| 52 | name, path = path.split('/', 1) |
| 53 | else: |
| 54 | name = path |
| 55 | path = "" |
| 56 | |
| 57 | try: |
| 58 | curr = getattr(curr, name) |
| 59 | except AttributeError: |
| 60 | return default |
| 61 | |
| 62 | return curr |
| 63 | |
| 64 | def __getattr__(self, name: str) -> Any: |
| 65 | try: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 66 | val = self._dct[name] |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 67 | except KeyError: |
| 68 | raise AttributeError(name) |
| 69 | |
| 70 | if isinstance(val, dict): |
| 71 | val = self.__class__(val) |
| 72 | |
| 73 | return val |
| 74 | |
| 75 | def __setattr__(self, name: str, val: Any): |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 76 | self._dct[name] = val |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 77 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 78 | def __contains__(self, name: str) -> bool: |
| 79 | return self.get(name) is not None |