forked from turnkeylinux/confconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.py
59 lines (45 loc) · 1.82 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
# 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 Error(Exception):
pass
def path(filename):
for dir in ("conf", "/etc/confconsole"):
path = os.path.join(dir, filename)
if os.path.exists(path):
return path
raise Error('could not find configuration file: %s' % path)
class Conf:
def _load_conf(self):
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):
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):
self.default_nic = ifname
with open(self.conf_file, 'w') as fob:
fob.write("default_nic %s\n" % ifname)