-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.py
73 lines (54 loc) · 1.53 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
69
70
71
72
73
import events
import urk
import tempfile
import shutil
import os
CONF_FILE = os.path.join(urk.userpath,'urk.conf')
def pprint(obj, depth=-2):
depth += 2
string = []
if isinstance(obj, dict):
if obj:
string.append('{\n')
for key in obj:
string.append('%s%s%s' % (' '*depth, repr(key), ': '))
string += pprint(obj[key], depth)
string.append('%s%s' % (' '*depth, '},\n'))
else:
string.append('{},\n')
elif isinstance(obj, list):
if obj:
string.append('[\n')
for item in obj:
string.append('%s' % (' '*depth))
string += pprint(item, depth)
string.append('%s%s' % (' '*depth, '],\n'))
else:
string.append('[],\n')
else:
string.append('%s,\n' % (repr(obj),))
if depth:
return string
else:
return ''.join(string)[:-2]
def save(*args):
fd, new_file = tempfile.mkstemp()
os.chmod(new_file,0600)
os.close(fd)
fd = file(new_file, "wb")
try:
fd.write(pprint(conf))
fd.close()
shutil.move(new_file, CONF_FILE)
finally:
fd.close()
events.register('Exit', 'post', save)
try:
conf = eval(file(CONF_FILE, "U").read().strip())
except IOError, e:
if e.args[0] == 2:
conf = {}
else:
raise
if __name__ == '__main__':
print pprint(conf)