Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/lib/purk/conf.py
blob: 9cae512713e3cc26d9916453dc1049f8a93bdfdb (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
urkpath = os.path.dirname(__file__)

def path(filename=""):
    if filename:
        return os.path.join(urkpath, filename)
    else:
        return urkpath

if os.access(path('profile'),os.F_OK) or os.path.expanduser("~") == "~":
    userpath = path('profile')
    if not os.access(userpath,os.F_OK):
        os.mkdir(userpath)
    if not os.access(os.path.join(userpath,'scripts'),os.F_OK):
        os.mkdir(os.path.join(userpath,'scripts'))
else:
    userpath = os.path.join(os.path.expanduser("~"), ".urk")
    if not os.access(userpath,os.F_OK):
        os.mkdir(userpath, 0700)
    if not os.access(os.path.join(userpath,'scripts'),os.F_OK):
        os.mkdir(os.path.join(userpath,'scripts'), 0700)

CONF_FILE = os.path.join(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):
    new_file = not os.access(CONF_FILE,os.F_OK)
    fd = file(CONF_FILE, "wb")
    try:
        if new_file:
            os.chmod(CONF_FILE,0600)
        fd.write(pprint(conf))
    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)