def test_update_config_with_other_config(self, base_index):
        list_base = "__BASE__"
        update_config = "__UPDATE_CONFIG__"
        config1 = {
            "config1": {"a": 10,
                        "b": {"b1": [1, 2], "b2": [1]},
                        "c": [1]},
            "config3": [{'a': 1, "b": 2}, {'d': 5}],
            "config4": {"c": 10},
            "config5": {"bar": 5},
            "config6": {"a": 5, "c": 1},
        }
        config2 = {
            "config1": {"a": 20, "b": None},
            "config2": [{"bar": 1, "foo": 20}],
            "config5": [list_base, {"foo": 5}],
            "config6": {update_config: True, "c": 20},
        }
        config3 = [{"d": 10}, {"e": -10}]
        if base_index < 0:
            config2["config3"] = config3
        elif base_index == 0:
            config2["config3"] = [list_base, *config3]
        elif base_index == 1:
            config2["config3"] = [config3[0], list_base, config3[1]]
        else:
            config2["config3"] = [config3[0], config3[1], list_base]

        config1_orig_copy = copy.deepcopy(config1)
        config2_orig_copy = copy.deepcopy(config2)
        config_updated_must = {
            "config1": {"a": 20, "b": None},
            "config2": [{"bar": 1, "foo": 20}],
            "config4": {"c": 10},
            "config5": [{"bar": 5}, {"foo": 5}],
            "config6": {"a": 5, "c": 20},
        }
        config13 = config1["config3"]
        if base_index < 0:
            config_updated_must["config3"] = config3
        elif base_index == 0:
            config_updated_must["config3"] = [*config13, *config3]
        elif base_index == 1:
            config_updated_must["config3"] = [config3[0], *config13, config3[1]]
        else:
            config_updated_must["config3"] = [config3[0], config3[1], *config13]

        config_updated = project_utils.update_config_with_other_config(
            config1, config2)

        self.assertDictEqual(config1_orig_copy,
                             config1)
        self.assertDictEqual(config2_orig_copy,
                             config2)
        self.assertDictEqual(config_updated_must,
                             config_updated)
Example #2
0
def _read_configs_from_directory(config_dir: str,
                                 single_config_fnames: list,
                                 multiple_config_fnames: list,
                                 verify_unused_configs: bool = False) -> dict:
    if verify_unused_configs:
        _verify_unused_configs(config_dir, single_config_fnames,
                               multiple_config_fnames)

    config_main = _read_config_main(config_dir)
    configs_from_files = _get_configs_from_single_files(
        config_dir, single_config_fnames)
    configs_from_directories = _get_configs_from_directories(
        config_dir, multiple_config_fnames)

    config = project_utils.update_config_with_other_config(
        config_main, configs_from_files)
    config = project_utils.update_config_with_other_config(
        config, configs_from_directories)
    return config
Example #3
0
def _read_configs_from_dirs_with_ro(config_dirs: list, single_config_fnames,
                                    multiple_config_fnames,
                                    verify_unused_configs):
    config = {}
    for each_config_dir in config_dirs:
        if not each_config_dir or not os.path.isdir(each_config_dir):
            continue

        config_from_dir = _read_configs_from_directory(
            each_config_dir,
            single_config_fnames,
            multiple_config_fnames,
            verify_unused_configs=verify_unused_configs)
        config = project_utils.update_config_with_other_config(
            config, config_from_dir)
        project_global_config.add_global_project_config_from_project_dir(
            each_config_dir)
    return config
Example #4
0
 def serialize_to_file(self, configs_to_log: dict):
     logger = logging.getLogger(__name__)
     if not self.save_dir:
         logger.warning("No save_dir provided to config serializer!"
                        "Set the project if you want to save the configs")
         return
     save_file_name = self._get_save_file_name()
     logger.info("Save run config to %s", save_file_name)
     configs_to_log = self.format_configs_to_log(configs_to_log)
     if os.path.exists(save_file_name):
         configs_to_log_loaded = io_utils.load_json(save_file_name)
         configs_to_log = project_utils.update_config_with_other_config(
             configs_to_log_loaded, configs_to_log)
     with open(save_file_name, "w", encoding='utf8') as file:
         json.dump(configs_to_log,
                   file,
                   indent=4,
                   sort_keys=True,
                   default=lambda o: RunConfigSerializer.NOT_SERIALIZABLE)