blob: 7554fb87674febcc52de637c8ff77e8a01dcf29d [file] [log] [blame]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02001from typing import Any, Dict, Optional
koder aka kdanilov7f59d562016-12-26 01:34:23 +02002from .result_classes import IStorable
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03003
koder aka kdanilov7f59d562016-12-26 01:34:23 +02004
5ConfigBlock = Dict[str, Any]
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +03006
7
koder aka kdanilov22d134e2016-11-08 11:33:19 +02008class Config(IStorable):
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +02009 def __init__(self, dct: ConfigBlock) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020010 # 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 kdanilov7f59d562016-12-26 01:34:23 +020029 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 kdanilov962ee5f2016-12-19 02:40:08 +020035
36 self._dct.clear()
37 self._dct.update(dct)
38
koder aka kdanilov7f59d562016-12-26 01:34:23 +020039 @classmethod
40 def fromraw(cls, data: Dict[str, Any]) -> 'Config':
41 return cls(data)
42
43 def raw(self) -> Dict[str, Any]:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020044 return self._dct
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030045
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020046 def get(self, path: str, default: Any = None) -> Any:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020047 curr = self
koder aka kdanilov22d134e2016-11-08 11:33:19 +020048 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 kdanilov962ee5f2016-12-19 02:40:08 +020064 val = self._dct[name]
koder aka kdanilov22d134e2016-11-08 11:33:19 +020065 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 kdanilov962ee5f2016-12-19 02:40:08 +020074 self._dct[name] = val
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030075
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020076 def __contains__(self, name: str) -> bool:
77 return self.get(name) is not None