def load_config_from_file(self, filename): if not os.path.exists(filename): raise RuntimeError("%r doesn't exist" % filename) cfg = { "__builtins__": __builtins__, "__name__": "__config__", "__file__": filename, "__doc__": None, "__package__": None } try: execfile_(filename, cfg, cfg) except Exception: print("Failed to read config file: %s" % filename) traceback.print_exc() sys.exit(1) for k, v in cfg.items(): # Ignore unknown names if k not in self.cfg.settings: continue try: self.cfg.set(k.lower(), v) except: sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v)) raise return cfg
def load_config_from_file(self, filename): """ Loads the configuration file: the file is a python file, otherwise raise an RuntimeError Exception or stop the process if the configuration file contains a syntax error. """ if not os.path.exists(filename): raise RuntimeError("%r doesn't exist" % filename) cfg = { "__builtins__": __builtins__, "__name__": "__config__", "__file__": filename, "__doc__": None, "__package__": None } try: execfile_(filename, cfg, cfg) except Exception: print("Failed to read config file: %s" % filename) traceback.print_exc() sys.exit(1) for k, v in cfg.items(): # Ignore unknown names if k not in self.cfg.settings: continue try: self.cfg.set(k.lower(), v) except: sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v)) raise return cfg
def load_config(self): # init configuration self.cfg = Config(self.usage, prog=self.prog) # parse console args parser = self.cfg.parser() args = parser.parse_args() # optional settings from apps cfg = self.init(parser, args, args.args) # Load up the any app specific configuration if cfg and cfg is not None: for k, v in cfg.items(): self.cfg.set(k.lower(), v) # Load up the config file if its found. if args.config: if not os.path.exists(args.config): raise RuntimeError("%r doesn't exist" % args.config) cfg = { "__builtins__": __builtins__, "__name__": "__config__", "__file__": args.config, "__doc__": None, "__package__": None } try: execfile_(args.config, cfg, cfg) except Exception: print("Failed to read config file: %s" % args.config) traceback.print_exc() sys.exit(1) for k, v in cfg.items(): # Ignore unknown names if k not in self.cfg.settings: continue try: self.cfg.set(k.lower(), v) except: sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v)) raise # Lastly, update the configuration with any command line # settings. for k, v in args.__dict__.items(): if v is None: continue if k == "args": continue self.cfg.set(k.lower(), v)
def get_config_from_filename(self, filename): if not os.path.exists(filename): raise RuntimeError("%r doesn't exist" % filename) cfg = { "__builtins__": __builtins__, "__name__": "__config__", "__file__": filename, "__doc__": None, "__package__": None } try: execfile_(filename, cfg, cfg) except Exception: print("Failed to read config file: %s" % filename) traceback.print_exc() sys.exit(1) return cfg
def get_config_from_filename(self, filename): if not os.path.exists(filename): raise RuntimeError("%r doesn't exist" % filename) cfg = { "__builtins__": __builtins__, "__name__": "__config__", "__file__": filename, "__doc__": None, "__package__": None, } try: execfile_(filename, cfg, cfg) except Exception: print("Failed to read config file: %s" % filename) traceback.print_exc() sys.exit(1) return cfg
def load_py(fname): config = globals().copy() config["uri"] = uri config["cfg"] = Config() execfile_(fname, config) return config