conf: use shutil.move instead of os.rename for saving
[urk.git] / conf.py
blobea23e97f95e017250e55d74b938b0f319bcd3cbf
1 import events
3 import urk
5 import tempfile
6 import shutil
7 import os
9 CONF_FILE = os.path.join(urk.userpath,'urk.conf')
11 def pprint(obj, depth=-2):
12 depth += 2
14 string = []
16 if isinstance(obj, dict):
17 if obj:
18 string.append('{\n')
20 for key in obj:
21 string.append('%s%s%s' % (' '*depth, repr(key), ': '))
22 string += pprint(obj[key], depth)
24 string.append('%s%s' % (' '*depth, '},\n'))
26 else:
27 string.append('{},\n')
29 elif isinstance(obj, list):
30 if obj:
31 string.append('[\n')
33 for item in obj:
34 string.append('%s' % (' '*depth))
35 string += pprint(item, depth)
37 string.append('%s%s' % (' '*depth, '],\n'))
39 else:
40 string.append('[],\n')
42 else:
43 string.append('%s,\n' % (repr(obj),))
45 if depth:
46 return string
47 else:
48 return ''.join(string)[:-2]
50 def save(*args):
51 fd, new_file = tempfile.mkstemp()
52 os.chmod(new_file,0600)
53 os.close(fd)
54 fd = file(new_file, "wb")
55 try:
56 fd.write(pprint(conf))
57 fd.close()
58 shutil.move(new_file, CONF_FILE)
59 finally:
60 fd.close()
62 events.register('Exit', 'post', save)
64 try:
65 conf = eval(file(CONF_FILE, "U").read().strip())
66 except IOError, e:
67 if e.args[0] == 2:
68 conf = {}
69 else:
70 raise
72 if __name__ == '__main__':
73 print pprint(conf)