-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
72 lines (61 loc) · 2.58 KB
/
config.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
from configparser import ConfigParser
from pathlib import Path
import platform, re
class Config:
def __init__(self, path):
self.path = path
self.config = ConfigParser(allow_no_value=True)
self.config.read(self.path)
def __call__(self, name, fallback=None):
return self.get(name, fallback)
def schema(self):
return {
'paths': {
'DOCUMENT_ROOT': 'Document Root Directory (htdocs)',
'VHOSTS_CONF_PATH': 'Virtual Hosts Configuration File (httpd-vhosts.conf)',
'APACHE_BIN': 'Apache Binaries Directory',
'HOSTS_FILE': 'System Hosts File ::auto'
},
'commands': {
'APACHE_RESTART': 'Restart Apache Service ::auto'
}
}
def get(self, name, fallback=None):
section, option = name.split('.') if '.' in name else ('*', name)
if section == '*':
for s in self.config.sections():
if option.lower() in self.config.options(s):
section = s
break;
else:
return fallback
value = self.config.get(section, option.lower())
args = re.findall(r"\$([\w]*)", value)
for arg in args:
sub = self.get(arg)
if sub != None:
value = value.replace('$' + arg, sub)
return value
def generate(self):
for section, options in self.schema().items():
if not self.config.has_section(section):
self.config.add_section(section)
for option, label in options.items():
label = label.split(' ::')
prmpt = label[0] + ' (leave blank to auto generate)' if 'auto' in label else label[0]
value = input(' >> ' + prmpt + ' :: ')
self.config.set(section, option, value if value != '' else self.auto(option))
with open(self.path, 'w') as fp:
self.config.write(fp)
def validate(self):
for section, options in self.schema().items():
for option, label in options.items():
if self.config.has_option(section, option.lower()) == False:
return False
return True
def auto(self, option):
values = {
'APACHE_RESTART': '$APACHE_BIN/httpd -k restart' if platform.system() == 'Windows' else "apache2 restart",
'HOSTS_FILE': 'C:/Windows/System32/drivers/etc/hosts' if platform.system() == 'Windows' else "/etc/hosts"
}
return values.get(option)