Thu, 15 Jan 2015 19:06:14 +0200
- added bridging functionality
import json class ConfigNode: def __init__ (self, obj, name, parent): self.obj = obj self.name = name self.root = parent if parent == None or parent.root == None else parent.root def keyname (self, key): if self.name == None: return key return self.name + ':' + key def get_value (self, key, default=None): i = 0 needSave = False if not key in self.obj: if default == None: raise ValueError ('Mandatory key \'%s\' not found' % self.keyname (key)) self.obj[key] = default self.save() return self.obj[key] def set_value (self, key, value): self.obj[key] = value self.save() def append_value (self, key, value): if key not in self.obj: self.obj[key] = [] self.obj[key].append (value) self.save() def get_node (self, key): return ConfigNode (obj=self.get_value (key, {}), name=self.keyname (key), parent=self) def get_nodelist (self, key, default=None): data = self.get_value (key, default) result = [] for entry in data: node = ConfigNode (obj=entry, name=self.keyname (key), parent=self) result.append (node) return result def has_node (self, key): return key in self.obj def append_nodelist (self, key): data = self.get_value (key, []) obj = {} data.append (obj) return ConfigNode (obj=obj, name=self.keyname (key), parent=self) def save (self): if self.root != None: self.root.save() return with open ('cobalt.json', 'w') as fp: json.dump (self.obj, fp, sort_keys = True, indent = 1) print "Config saved." def find_developer_by_email (self, commit_email): for developer, emails in self.get_value ('developer_emails', default={}).iteritems(): if commit_email in emails: return developer return '' def init(): print 'Loading configuration...' try: with open ('cobalt.json', 'r') as fp: jsondata = json.loads (fp.read()) except IOError as e: print 'couldn\'t open cobalt.json: %s' % e quit() global Config Config = ConfigNode (jsondata, name=None, parent=None) init()