blob: 95ae511ab72d0f287d6e178865b774d3b84de103 [file] [log] [blame]
kdanylov aka koder150b2192017-04-01 16:53:01 +03001from typing import Any, Dict, Optional, Set
koder aka kdanilov108ac362017-01-19 20:17:16 +02002
3from .common_types import IStorable
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03004
koder aka kdanilov7f59d562016-12-26 01:34:23 +02005
6ConfigBlock = Dict[str, Any]
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +03007
8
koder aka kdanilov22d134e2016-11-08 11:33:19 +02009class Config(IStorable):
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020010 def __init__(self, dct: ConfigBlock) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020011 # make mypy happy, set fake dict
12 self.__dict__['_dct'] = {}
13 self.run_uuid = None # type: str
14 self.storage_url = None # type: str
15 self.comment = None # type: str
16 self.keep_vm = None # type: bool
17 self.dont_discover_nodes = None # type: bool
18 self.build_id = None # type: str
19 self.build_description = None # type: str
20 self.build_type = None # type: str
21 self.default_test_local_folder = None # type: str
22 self.settings_dir = None # type: str
23 self.connect_timeout = None # type: int
24 self.no_tests = False # type: bool
25 self.debug_agents = False # type: bool
26
27 # None, disabled, enabled, metadata, ignore_errors
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030028 self.discover = None # type: Optional[str]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020029
koder aka kdanilov7f59d562016-12-26 01:34:23 +020030 self.logging = None # type: 'Config'
31 self.ceph = None # type: 'Config'
32 self.openstack = None # type: 'Config'
33 self.fuel = None # type: 'Config'
34 self.test = None # type: 'Config'
35 self.sensors = None # type: 'Config'
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030036 self.discover = None # type: Set[str]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020037
38 self._dct.clear()
39 self._dct.update(dct)
40
koder aka kdanilov7f59d562016-12-26 01:34:23 +020041 @classmethod
42 def fromraw(cls, data: Dict[str, Any]) -> 'Config':
43 return cls(data)
44
45 def raw(self) -> Dict[str, Any]:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020046 return self._dct
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030047
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020048 def get(self, path: str, default: Any = None) -> Any:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020049 curr = self
koder aka kdanilov22d134e2016-11-08 11:33:19 +020050 while path:
51 if '/' in path:
52 name, path = path.split('/', 1)
53 else:
54 name = path
55 path = ""
56
57 try:
58 curr = getattr(curr, name)
59 except AttributeError:
60 return default
61
62 return curr
63
64 def __getattr__(self, name: str) -> Any:
65 try:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020066 val = self._dct[name]
koder aka kdanilov22d134e2016-11-08 11:33:19 +020067 except KeyError:
68 raise AttributeError(name)
69
70 if isinstance(val, dict):
71 val = self.__class__(val)
72
73 return val
74
75 def __setattr__(self, name: str, val: Any):
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020076 self._dct[name] = val
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030077
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020078 def __contains__(self, name: str) -> bool:
79 return self.get(name) is not None