-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfigure.py
69 lines (62 loc) · 2.09 KB
/
configure.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
#!coding=utf-8
import os
import toml
import copy
CONF_LOCATIONS = ["/etc/mcx", "~/.mcx"]
CONF_FILE = "config.toml"
HOSTS_DIRS = "hosts.d"
class Configuration(object):
def __init__(self):
for item in CONF_LOCATIONS:
conf_path = os.path.expanduser(os.path.join(item, CONF_FILE))
if os.path.exists(conf_path):
with open(conf_path) as f:
for k,v in toml.load(f).iteritems():
self.__setattr__(k, v)
self.hosts = {}
self.get_hosts()
def get_hosts(self):
for item in CONF_LOCATIONS:
d = os.path.expanduser(os.path.join(item, HOSTS_DIRS))
if os.path.exists(d):
self.hosts.update(read_hosts_from_dir(d))
def read_hosts_from_dir(dir):
conns = {}
for (base, _, files) in os.walk(dir):
for path in files:
with open(os.path.join(base, path)) as f:
conns.update(read_host(f))
return conns
def read_host(f):
fields = f.readline().strip().split(",")
prefix = None
hosts = {}
default_vs = {}
in_local=False
for line in f:
line = line.strip()
if not line:continue
if line[0] == "[" and line[-1] == "]":
prefix = line[1:-1]
in_local = True
local_vs = copy.deepcopy(default_vs)
elif line[0] == "#":
k, v = [x.strip() for x in line[1:].split("=")]
if in_local:
local_vs[k] = eval(v)
else:
default_vs[k] = eval(v)
else:
name,values = [x.strip() for x in line.split(":")]
if prefix:
name = "%s/%s" % (prefix.decode("utf-8"), name.decode("utf-8"))
else:
name = name.decode("utf-8")
values = [x.strip() for x in values.split(",")]
hosts[name] = copy.deepcopy(local_vs)
hosts[name].update({k:v for (k,v) in zip(fields, values)})
return hosts
configuration = Configuration()
if __name__ == '__main__':
for h,v in configuration.hosts.iteritems():
print h,v