def __init__(self, config_path = '', config = {}): BaseObject.__init__(self) default_config = {} default_magic = { 'ready': { 'get': '_ready', 'set': None } } self._ready = False self.path = config_path self.config = extend({}, default_config, config, True) self.changed = ObservableObject(False) self.magic = extend({}, default_magic, True) self.magicWizard() if self.load(): self._ready = True
class ConfigObject(BaseObject): def __init__(self, config_path = '', config = {}): BaseObject.__init__(self) default_config = {} default_magic = { 'ready': { 'get': '_ready', 'set': None } } self._ready = False self.path = config_path self.config = extend({}, default_config, config, True) self.changed = ObservableObject(False) self.magic = extend({}, default_magic, True) self.magicWizard() if self.load(): self._ready = True def magicWizard(self): magic = {} for key, value in self.config.iteritems(): magic[key] = { 'get': 'getConfigItem', 'set': 'setConfigItem', 'getArgs': [key], 'setArgs': [key] } self.magic = extend({}, self.magic, magic, True) def load(self): if os.path.isfile(self.path): config_data = {} with open(self.path, 'r') as data_file: try: config_data = json.load(data_file) except: pass self.config = extend({}, self.config, config_data, True) self.magicWizard() def save(self): if os.path.isfile(self.path): with open(self.path, 'w') as config_file: config_file.write(json.dumps(self.config, indent = 2)) self.changed.set(False) return self def getConfigItem(self, key): if key in self.config: return self.config[key] return None def setConfigItem(self, value, key): if key in self.config: self.config[key] = value self.changed.set(True) return self