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