blob: fb19a238d49056e9920d80f29aba9a24e4176cb7 (
plain)
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
|
import yaml
from yaml import SafeLoader
from yaml.nodes import SequenceNode
from importlib.resources import path as rpath
from logging import Logger, getLogger
log: Logger = getLogger(__name__)
# required to concat values in yaml using !join [value, value, ...]
def __join_constructor(loader: SafeLoader, node: SequenceNode) -> str:
seq = loader.construct_sequence(node)
return ''.join([str(i) for i in seq])
def __read_raw_yaml(path: str) -> list[dict]:
all_docs: list[dict] = []
with open(path, 'r') as f:
for doc in yaml.safe_load_all(f):
all_docs.append(doc)
return all_docs
def get_parsed_yaml(resource: str, package: str = '') -> list[dict]:
if package == '':
log.debug('parsing yaml; reading "%s"', resource)
return __read_raw_yaml(resource)
log.debug('parsing yaml; reading "%s.%s"', package, resource)
with rpath(package, resource) as p:
return __read_raw_yaml(str(p))
SafeLoader.add_constructor('!join', __join_constructor)
|