koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 1 | from typing import Any, Dict |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 2 | from .storage import IStorable |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 3 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 4 | ConfigBlock = Dict[str, Any] |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 5 | |
| 6 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 7 | class Config(IStorable): |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 8 | # make mypy happy |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 9 | run_uuid = None # type: str |
| 10 | storage_url = None # type: str |
| 11 | comment = None # type: str |
| 12 | keep_vm = None # type: bool |
| 13 | no_tests = None # type: bool |
| 14 | dont_discover_nodes = None # type: bool |
| 15 | build_id = None # type: str |
| 16 | build_description = None # type: str |
| 17 | build_type = None # type: str |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 18 | default_test_local_folder = None # type: str |
| 19 | settings_dir = None # type: str |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 20 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 21 | def __init__(self, dct: ConfigBlock) -> None: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 22 | self.__dict__['_dct'] = dct |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 23 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 24 | def get(self, path: str, default: Any = None) -> Any: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 25 | curr = self |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 26 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 27 | while path: |
| 28 | if '/' in path: |
| 29 | name, path = path.split('/', 1) |
| 30 | else: |
| 31 | name = path |
| 32 | path = "" |
| 33 | |
| 34 | try: |
| 35 | curr = getattr(curr, name) |
| 36 | except AttributeError: |
| 37 | return default |
| 38 | |
| 39 | return curr |
| 40 | |
| 41 | def __getattr__(self, name: str) -> Any: |
| 42 | try: |
| 43 | val = self.__dct[name] |
| 44 | except KeyError: |
| 45 | raise AttributeError(name) |
| 46 | |
| 47 | if isinstance(val, dict): |
| 48 | val = self.__class__(val) |
| 49 | |
| 50 | return val |
| 51 | |
| 52 | def __setattr__(self, name: str, val: Any): |
| 53 | self.__dct[name] = val |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 54 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame^] | 55 | def __contains__(self, name: str) -> bool: |
| 56 | return self.get(name) is not None |