def Save(self): for key, d in self.private.iteritems(): if not isinstance(d, dict): continue if not d.get(IS_CHANGED): continue if not d.get(PATH): d[PATH] = os.path.join(self.GetDirectory(), "%s.json" % key) # # This is the version we are going to persist # nd = {} for subkey, subvalue in d.iteritems(): if subkey.startswith("@"): continue nd[subkey] = subvalue # # Save, preserving old data # wd = json.loads(bm_io.readfile(d[PATH])) wd[key] = nd bm_io.AtomicWrite(filename = d[PATH], data = json.dumps(wd, indent = 4, sort_keys = True)) Log("saved partial cfg", filename = d[PATH], keys = wd.keys())
def load(self, path, exception = False, depth = 0): try: if os.path.isdir(path) and depth < 2: for file in os.listdir(path): self.load(os.path.join(path, file)) elif os.path.isfile(path): if path.endswith(".json"): d = json.loads(bm_io.readfile(path)) if type(d) != types.DictType: raise TypeError("only dictionaries can be added") for key, subd in d.iteritems(): if isinstance(subd, dict): subd[PATH] = path subd[IS_CHANGED] = False self.add(d) except: if exception: raise Log("ignoring exception", exception = True, path = path)
def CustomizeProduce(self, command, arguments): Log( "PRODUCED", command = command, arguments = arguments, ) class DispatchJDParser(JDParser): """All commands are implemented as methods called call_<command>. If 'call' is implemented it's a catchall""" def CustomizeProduce(self, command, arguments): ad = dict(arguments) fname = "call_%s" % command if hasattr(self, fname): getattr(self, fname)(**ad) return self.CustomizeCall(command, **ad) def CustomizeCall(self, command, **ad): self.RaiseError(JDUnknownCommandError, message = command) if __name__ == '__main__': ## Log.verbose = True parser = LogJDParser() for file in sys.argv[1:]: parser.FeedString(bm_io.readfile(file))