-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml_helper.py
34 lines (26 loc) · 973 Bytes
/
yaml_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import *
T = TypeVar('T')
class YamlHelper:
def __init__(self, yaml_obj):
self._yaml = yaml_obj
def get(self, path: str, default=None) -> Any:
entries = list(map(str.strip, path.split('/')))
obj = self._yaml.get(entries[0])
for i in range(1, len(entries)):
if obj is None:
return default
obj = obj.get(entries[i])
return obj
def get_list(self, path: str) -> List[Any]:
obj = self.get(path)
if isinstance(obj, list):
return obj
elif obj is not None:
return [obj]
else:
return []
def get_bool(self, path: str, default: bool = False) -> bool:
return bool(self.get(path, default))
def get_as(self, path: str, converter: Callable[[str], T], default: T = None) -> T:
result = self.get(path, default)
return converter(result) if result is not None else result