Mon, 08 Dec 2014 15:37:03 -0500
- commit latest changes
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() |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | def get_node (self, key): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | 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
|
32 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | def get_nodelist (self, key): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | data = self.get_value (key) |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | result = [] |
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 | for entry in data: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | 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
|
39 | result.append (node) |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | return result |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
43 | def append_nodelist (self, key): |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
44 | data = self.get_value (key) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
45 | obj = {} |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
46 | data.append (obj) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
47 | return ConfigNode (obj=obj, name=self.keyname (key), parent=self) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
48 | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | def save (self): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | if self.root != None: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | self.root.save() |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | return |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | with open ('cobalt.json', 'w') as fp: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | 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
|
56 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | print "Config saved." |
76 | 58 | |
59 | def find_developer_by_email (self, commit_email): | |
60 | for developer, emails in self.get_value ('developer_emails', default={}).iteritems(): | |
61 | if commit_email in emails: | |
62 | return developer | |
63 | ||
64 | return '' | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | def init(): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | print 'Loading configuration...' |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | try: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | with open ('cobalt.json', 'r') as fp: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | jsondata = json.loads (fp.read()) |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | except IOError as e: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | 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
|
74 | quit() |
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 | global Config |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | Config = ConfigNode (jsondata, name=None, parent=None) |
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 | init() |