-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevalconf.py
84 lines (68 loc) · 2.57 KB
/
evalconf.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# (c) 2020–2021 Vladimír Štill <code@vstill.eu>
from __future__ import annotations
import yaml
import copy
import re
from pathlib import Path
from os import PathLike
from typing import Dict, Any, TypeVar, Union
T = TypeVar("T")
class EvalConf:
def __init__(self) -> None:
self.config: Dict[str, Any] = {}
@staticmethod
def _merge_in(left: Dict[str, Any], right: Dict[str, Any]) -> None:
def checktype(r: Any, t: type) -> None:
if not isinstance(r, t):
raise ValueError(f"config type mismatch in {k}, expected {t}, "
f"got {type(r)}")
for k, v in right.items():
if k not in left:
left[k] = v
elif isinstance(left[k], dict):
checktype(v, dict)
EvalConf._merge_in(left[k], v)
elif isinstance(left[k], list):
checktype(v, list)
left[k] += v
else:
left[k] = v
def add(self, raw: Dict[str, Any]) -> EvalConf:
EvalConf._merge_in(self.config, copy.deepcopy(raw))
return self
def load(self, path: str) -> EvalConf:
with open(path, "r") as fh:
# do not go through add - avoid copy
EvalConf._merge_in(self.config, yaml.safe_load(fh))
return self
def get(self) -> Dict[str, Any]:
return copy.deepcopy(self.config)
def dump_to(self, stream: Any) -> None:
yaml.safe_dump(self.config, stream, default_flow_style=False)
def dump(self, path: str) -> None:
with open(path, "w") as fh:
self.dump_to(fh)
def from_source_file(self, path: Union[str, PathLike[Any]]) -> EvalConf:
path = Path(path)
if path.suffix == ".hs":
self._from_source_file(path, "-- @")
elif path.suffix in [".c", ".cc", "cpp"]:
self._from_source_file(path, "//@")
elif path.suffix in [".py", ".pl", ".sh"]:
self._from_source_file(path, "#@")
return self
def _from_source_file(self, path: Path, prefix: str) -> None:
pat = re.compile(f"{prefix}\\s+([^:]*):\\s*(.*)")
collected = ""
with open(path, "r") as h:
for line in h:
m = pat.match(line)
if m is not None:
collected += f"{m[1]}: {m[2]}\n"
raw = yaml.safe_load(collected)
if raw is not None:
self.add(raw)
def __setitem__(self, key: str, value: T) -> T:
self.config[key] = value
return value
# vim: colorcolumn=80 expandtab sw=4 ts=4