blob: 59db0e1aebd81ea73547beeecc30428c8720c1fd [file] [log] [blame]
koder aka kdanilov22d134e2016-11-08 11:33:19 +02001from typing import Any, Dict
2from .storage import IStorable, IStorage
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03003
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03004class NoData:
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +03005 @classmethod
koder aka kdanilov22d134e2016-11-08 11:33:19 +02006 def get(cls: type, name: str, x: Any) -> type:
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +03007 return cls
8
9
koder aka kdanilov22d134e2016-11-08 11:33:19 +020010class 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 kdanilov0fdaaee2015-06-30 11:10:48 +030021
koder aka kdanilov22d134e2016-11-08 11:33:19 +020022 def __init__(self, dct: Dict[str, Any]) -> None:
23 self.__dict__['_dct'] = dct
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030024
koder aka kdanilov22d134e2016-11-08 11:33:19 +020025 def get(self, path: str, default: Any = NoData) -> Any:
26 curr = self
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030027
koder aka kdanilov22d134e2016-11-08 11:33:19 +020028 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 kdanilovcff7b2e2015-04-18 20:48:15 +030055
56
koder aka kdanilov22d134e2016-11-08 11:33:19 +020057class Context:
58 def __init__(self, config: Config, storage: IStorage):
59 self.config = config
60 self.storage = storage