configfile.py

Sat, 11 Apr 2015 21:02:09 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 11 Apr 2015 21:02:09 +0300
changeset 121
ac07779f788d
parent 118
dbf49689af0d
child 124
7b2cd8b1ba86
permissions
-rw-r--r--

- reworked mercurial repository handling, removed hardcoded values

65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 import json
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 class ConfigNode:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 def __init__ (self, obj, name, parent):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 self.obj = obj
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 self.name = name
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 self.root = parent if parent == None or parent.root == None else parent.root
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 def keyname (self, key):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 if self.name == None:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 return key
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 return self.name + ':' + key
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 def get_value (self, key, default=None):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 i = 0
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 needSave = False
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 if not key in self.obj:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 if default == None:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 raise ValueError ('Mandatory key \'%s\' not found' % self.keyname (key))
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 self.obj[key] = default
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 self.save()
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 return self.obj[key]
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 def set_value (self, key, value):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 self.obj[key] = value
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 self.save()
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
29
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
30 def append_value (self, key, value):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
31 if key not in self.obj:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
32 self.obj[key] = []
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
33
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
34 self.obj[key].append (value)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
35 self.save()
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 def get_node (self, key):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 return ConfigNode (obj=self.get_value (key, {}), name=self.keyname (key), parent=self)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
40 def get_nodelist (self, key, default=None):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
41 data = self.get_value (key, default)
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 result = []
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 for entry in data:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 node = ConfigNode (obj=entry, name=self.keyname (key), parent=self)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 result.append (node)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 return result
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
49
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
50 def has_node (self, key):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
51 return key in self.obj
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
53 def append_nodelist (self, key):
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
54 data = self.get_value (key, [])
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
55 obj = {}
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
56 data.append (obj)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
57 return ConfigNode (obj=obj, name=self.keyname (key), parent=self)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
58
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 def save (self):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 if self.root != None:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 self.root.save()
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 return
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 with open ('cobalt.json', 'w') as fp:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 json.dump (self.obj, fp, sort_keys = True, indent = 1)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 print "Config saved."
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
68
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
69 def find_developer_by_email (self, commit_email):
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
70 for developer, emails in self.get_value ('developer_emails', default={}).iteritems():
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
71 if commit_email in emails:
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
72 return developer
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
73
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
74 return ''
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 def init():
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 print 'Loading configuration...'
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 try:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 with open ('cobalt.json', 'r') as fp:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 jsondata = json.loads (fp.read())
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 except IOError as e:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 print 'couldn\'t open cobalt.json: %s' % e
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 quit()
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 global Config
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 Config = ConfigNode (jsondata, name=None, parent=None)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
89 init()

mercurial