userver: en/testsuite/utils/yaml_util.py Source File
Loading...
Searching...
No Matches
yaml_util.py
1import yaml
2import yaml.parser
3
4from testsuite.utils import object_hook as object_hook_util
5
6
7class BaseError(Exception):
8 pass
9
10
12 pass
13
14
15if hasattr(yaml, 'CLoader'):
16 _Loader = yaml.CLoader # type: ignore
17else:
18 _Loader = yaml.Loader # type: ignore
19
20
21def load_file(path, encoding='utf-8', object_hook=None):
22 with open(path, encoding=encoding) as fp:
23 return load(fp, object_hook=object_hook)
24
25
26def load(string_or_stream, object_hook=None):
27 try:
28 yaml_obj = yaml.load(string_or_stream, Loader=_Loader)
29 hook = object_hook_util.build_object_hook(object_hook=object_hook)
30 return object_hook_util.substitute(yaml_obj, hook)
31 except yaml.parser.ParserError as exc:
32 raise ParserError(str(exc)) from exc