def configure(config_files=DEFAULT_CONFIG_FILES, logging_config_files=LOGGING_CONFIG_FILES, argv=sys.argv): cli_cfg, cli_cfg_files = _parse_argv(argv) config_files = config_files + cli_cfg_files CFG = Config(config_files).data CFG = dict_merge(CFG, cli_cfg) try: # Make log directory if it doesn't exist for handler in CFG.logging.get('handlers', {}).itervalues(): if 'filename' in handler: log_dir = os.path.dirname(handler['filename']) if not os.path.exists(log_dir): os.makedirs(log_dir) try: #TODO: This requires python 2.7 logging.config.dictConfig(CFG.logging) except AttributeError: msg = '"logging.config.dictConfig" doesn\'t seem to be supported ' msg += 'in your python. Try Python 2.7.' print >> sys.stderr, msg raise except AttributeError: print >> sys.stderr, "No logging configured, continuing without." pass return CFG
def load(self): """ Load each path in order. Remember paths already loaded and only load new ones. """ data = self.dict_class() for path in self.paths: if path in self.paths_loaded: continue try: with open(path, 'r') as file: path_data = yaml.load(file.read()) data = dict_merge(data, path_data) self.paths_loaded.add(path) except IOError: # TODO: Log this correctly once logging is implemented if not path.endswith('.local.yml'): print 'CONFIG NOT FOUND: %s' % (path) self.data = data
def _parse_argv(argv=copy(sys.argv)): """return argv as a parsed dictionary, looks like the following: app --option1 likethis --option2 likethat --flag -> {'option1': 'likethis', 'option2': 'likethat', 'flag': True} """ cfg = DotDict() cfg_files = [] argv = argv[1:] # Skip command name while argv: arg = argv.pop(0) # split up arg in format --arg=val key_val = re.split('=| ', arg) arg = key_val[0] try: val = key_val[1] except IndexError: if len(argv) > 0 and argv[0][0] != '-': val = argv.pop(0) else: # No val available, probably a flag val = None if arg[0] == '-': key = arg.lstrip('-') if not val: val = True new_cfg = _dict_from_dotted(key, val) cfg = dict_merge(cfg, new_cfg) else: if arg.endswith(".yml"): cfg_files.append(arg) return cfg, cfg_files