blob: cac4acf34667600a41d823678e7eaed07ded2625 [file] [log] [blame]
koder aka kdanilov22d134e2016-11-08 11:33:19 +02001from typing import Any, Dict
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +02002from .storage import IStorable
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03003
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +02004ConfigBlock = Dict[str, Any]
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +03005
6
koder aka kdanilov22d134e2016-11-08 11:33:19 +02007class Config(IStorable):
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +02008 # make mypy happy
koder aka kdanilov22d134e2016-11-08 11:33:19 +02009 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 kdanilov3d2bc4f2016-11-12 18:31:18 +020018 default_test_local_folder = None # type: str
19 settings_dir = None # type: str
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030020
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020021 def __init__(self, dct: ConfigBlock) -> None:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020022 self.__dict__['_dct'] = dct
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030023
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020024 def get(self, path: str, default: Any = None) -> Any:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020025 curr = self
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030026
koder aka kdanilov22d134e2016-11-08 11:33:19 +020027 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 kdanilovcff7b2e2015-04-18 20:48:15 +030054
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020055 def __contains__(self, name: str) -> bool:
56 return self.get(name) is not None