-
Notifications
You must be signed in to change notification settings - Fork 29
/
conf.py
68 lines (53 loc) · 2.01 KB
/
conf.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
# 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
from typing import Optional
class Error(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 Error(f'could not find configuration file: {filename}')
class Conf:
default_nic: Optional[str]
publicip_cmd: Optional[str]
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, 'r') 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 Error("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")