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