-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathconf.py
More file actions
71 lines (56 loc) · 2.08 KB
/
conf.py
File metadata and controls
71 lines (56 loc) · 2.08 KB
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
# Copyright (c) 2008-2019 Alon Swartz <[email protected]>
# - all rights reserved
# Copyright (c) 2020 TurnKey GNU/Linux <[email protected]>
# - all rights reserved
import re
import os
class ConfconsoleConfError(Exception):
pass
def path(filename: str) -> str:
for dir in ("conf", "/etc/confconsole"):
path = os.path.join(dir, filename)
if os.path.exists(path):
return path
raise ConfconsoleConfError(
f"could not find configuration file: {filename}"
)
class Conf:
default_nic: str | None
publicip_cmd: str | None
networking: bool
copy_paste: bool
conf_file: str
def _load_conf(self) -> None:
if not self.conf_file or not os.path.exists(self.conf_file):
return
with open(self.conf_file) as fob:
for line in fob:
line = line.strip()
if not line or line.startswith("#"):
continue
op, val = re.split(r"\s+", line, 1)
if op == "default_nic":
self.default_nic = val
elif op == "publicip_cmd":
self.publicip_cmd = val
elif op == "networking" and val in ("true", "false"):
self.networking = True if val == "true" else False
elif op == "autostart":
pass
elif op == "copy_paste" and val.lower() in ("true", "false"):
self.copy_paste = True if val.lower() == "true" else False
else:
raise ConfconsoleConfError(
f"illegal configuration line: {line}"
)
def __init__(self) -> None:
self.default_nic = None
self.publicip_cmd = None
self.networking = True
self.copy_paste = True
self.conf_file = path("confconsole.conf")
self._load_conf()
def set_default_nic(self, ifname: str) -> None:
self.default_nic = ifname
with open(self.conf_file, "w") as fob:
fob.write(f"default_nic {ifname}\n")