Sun, 17 May 2015 20:42:32 +0300
Merged diverged heads
124
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
1 | ''' |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
2 | Copyright 2014-2015 Teemu Piippo |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
3 | All rights reserved. |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
4 | |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
6 | modification, are permitted provided that the following conditions |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
7 | are met: |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
8 | |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
9 | 1. Redistributions of source code must retain the above copyright |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
10 | notice, this list of conditions and the following disclaimer. |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
11 | 2. Redistributions in binary form must reproduce the above copyright |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
14 | 3. The name of the author may not be used to endorse or promote products |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
15 | derived from this software without specific prior written permission. |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
16 | |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
27 | ''' |
7b2cd8b1ba86
- now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
118
diff
changeset
|
28 | |
132 | 29 | import json, sys |
30 | IsInitialized = False | |
31 | Config = None | |
32 | ConfigFileName = "" | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | class ConfigNode: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | def __init__ (self, obj, name, parent): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | self.obj = obj |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | self.name = name |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | 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
|
39 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | def keyname (self, key): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | if self.name == None: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | return key |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | return self.name + ':' + key |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | def get_value (self, key, default=None): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | i = 0 |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | needSave = False |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | if not key in self.obj: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | if default == None: |
132 | 51 | raise ValueError ("""Mandatory key '%s' not found""" % self.keyname (key)) |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | self.obj[key] = default |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | self.save() |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | return self.obj[key] |
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 | def set_value (self, key, value): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | self.obj[key] = value |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | self.save() |
118
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
60 | |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
61 | def append_value (self, key, value): |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
62 | if key not in self.obj: |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
63 | self.obj[key] = [] |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
64 | |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
65 | self.obj[key].append (value) |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
66 | self.save() |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | def get_node (self, key): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | 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
|
70 | |
118
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
71 | def get_nodelist (self, key, default=None): |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
72 | data = self.get_value (key, default) |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | result = [] |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | for entry in data: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | 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
|
77 | result.append (node) |
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 | return result |
118
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
80 | |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
81 | def has_node (self, key): |
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
82 | return key in self.obj |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
84 | def append_nodelist (self, key): |
118
dbf49689af0d
- added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
85 | data = self.get_value (key, []) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
86 | obj = {} |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
87 | data.append (obj) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
88 | return ConfigNode (obj=obj, name=self.keyname (key), parent=self) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
89 | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | def save (self): |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | if self.root != None: |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | self.root.save() |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | return |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | |
132 | 95 | with open (ConfigFileName, 'w') as fp: |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | 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
|
97 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | print "Config saved." |
76 | 99 | |
100 | def find_developer_by_email (self, commit_email): | |
101 | for developer, emails in self.get_value ('developer_emails', default={}).iteritems(): | |
102 | if commit_email in emails: | |
103 | return developer | |
104 | ||
105 | return '' | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | def init(): |
132 | 108 | filename = 'cobalt.json' |
109 | ||
110 | if len (sys.argv) >= 2: | |
111 | filename = sys.argv[1] | |
112 | ||
113 | global IsInitialized | |
114 | global ConfigFileName | |
115 | ConfigFileName = filename | |
116 | ||
117 | if not IsInitialized: | |
118 | print """Loading configuration...""" | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | |
132 | 120 | try: |
121 | with open (filename, 'r') as fp: | |
122 | jsondata = json.loads (fp.read()) | |
123 | except IOError as e: | |
124 | print ("""couldn't open %s: %s""" % (filename, e)) | |
125 | quit() | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | |
132 | 127 | global Config |
128 | Config = ConfigNode (jsondata, name=None, parent=None) | |
129 | IsInitialized = True | |
130 | ||
131 | init() |