def load_configs_from_directory(config_dir, overrides): """ Returns a master configuration object and a list of configuration objects :param config_dir: the directory where the configuration files are located :param overrides: mapping of the command line overrides """ MASTER_CONFIG_FILE_NAME = "master" DEFAULT_CONFIG_NAME = "single execution" EMPTY_MAP = {} master_config = None config_objs = [] # get master config and default mapping config_subdirs = [] default_mapping = {} for dir_item in os.listdir(config_dir): full_path = os.path.join(config_dir, dir_item) if os.path.isdir(full_path): config_subdirs.append(full_path) # save subdirs for processing later elif os.path.isfile(full_path): config_name = os.path.splitext(os.path.basename(full_path))[0] try: mapping = utils.parse_config_file(full_path) except ValueError: logger.debug("Ignored " + full_path + "as configuration due to file extension") else: if MASTER_CONFIG_FILE_NAME in config_name: master_config = Config(MASTER_CONFIG_FILE_NAME, mapping) else: default_mapping.update(mapping) if master_config is None: master_config = Config(MASTER_CONFIG_FILE_NAME, EMPTY_MAP) if len(config_subdirs) == 0: default_mapping.update(overrides) config_objs.append(Config(DEFAULT_CONFIG_NAME, default_mapping)) else: # make a config object for each subdir for config_subdir in config_subdirs: config_files = [os.path.join(config_subdir, config_file) for config_file in os.listdir(config_subdir) if os.path.isfile(os.path.join(config_subdir, config_file))] subdir_mapping = default_mapping.copy() # initialize the configuration as default config_name = os.path.basename(config_subdir) for config_file in config_files: try: mapping = utils.parse_config_file(config_file) except ValueError: logger.debug("Ignored " + config_file + "as configuration due to file extension") else: subdir_mapping.update(mapping) subdir_mapping.update(overrides) config_objs.append(Config(config_name, subdir_mapping)) return master_config, config_objs
def test_parse_config_file_returns_correct_mapping(self): json_mapping = utils.parse_config_file( os.path.join(self.FILE_LOCATION, "samples/sample_config.json")) self.assertEqual(len(json_mapping), 3) self.assertEqual(json_mapping["c"], {'x': 4, 'y': 5, 'z': 6}) py_mapping = utils.parse_config_file( os.path.join(self.FILE_LOCATION, "samples/sample_config.py")) self.assertEqual(len(py_mapping), 3) self.assertEqual(py_mapping["c"], {'x': 4, 'y': 5, 'z': 6}) other_mapping = utils.parse_config_file( os.path.join(self.FILE_LOCATION, "samples/sample_config")) self.assertEqual(len(other_mapping), 3) self.assertEqual(other_mapping["a"], '1')