-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmachine_file.py
44 lines (31 loc) · 1.11 KB
/
machine_file.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
from configparser import ConfigParser
from pathlib import Path
from typing import Sequence, Union
def load(mfile: Path) -> dict[str, Union[str, list[str]]]:
config = ConfigParser()
config.read(mfile)
hidden_constants = {
"true": True,
"false": False,
}
items = {}
if config.has_section("constants"):
for name, raw_value in config.items("constants"):
items[name] = eval(raw_value, hidden_constants, items)
for section_name, section in config.items():
if section_name in ("DEFAULT", "constants"):
continue
for name, raw_value in section.items():
value = eval(raw_value, hidden_constants, items)
if section_name == "binaries" and isinstance(value, str):
value = [value]
items[name] = value
if len(items) == 0:
return None
return items
def bool_to_meson(b: bool) -> str:
return "true" if b else "false"
def strv_to_meson(strv: Sequence[str]) -> str:
return "[" + ", ".join(map(str_to_meson, strv)) + "]"
def str_to_meson(s: str) -> str:
return "'" + s + "'"