Mon, 01 Jun 2015 12:46:23 +0300
Use [code] for diffstats in tracker messages
''' Copyright 2014-2015 Teemu Piippo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' import json, sys IsInitialized = False Config = None ConfigFileName = "" 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 (ConfigFileName, '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(): filename = 'cobalt.json' if len (sys.argv) >= 2: filename = sys.argv[1] global IsInitialized global ConfigFileName ConfigFileName = filename if not IsInitialized: print """Loading configuration...""" try: with open (filename, 'r') as fp: jsondata = json.loads (fp.read()) except IOError as e: print ("""couldn't open %s: %s""" % (filename, e)) quit() global Config Config = ConfigNode (jsondata, name=None, parent=None) IsInitialized = True init()