diff --git a/map2loop/config.py b/map2loop/config.py index 5755957c..48d017d3 100644 --- a/map2loop/config.py +++ b/map2loop/config.py @@ -93,13 +93,19 @@ def to_dict(self): } @beartype.beartype - def update_from_dictionary(self, dictionary: dict, lower: bool = False): + def update_from_dictionary(self, dictionary: dict, lower: bool = True): """ Update the config dictionary from a provided dict Args: dictionary (dict): The dictionary to update from """ + # make sure dictionary doesn't contain legacy keys + self.check_for_legacy_keys(dictionary) + + # make sure it has the minimum requirements + self.validate_config_dictionary(dictionary) + if "structure" in dictionary: self.structure_config.update(dictionary["structure"]) for key in dictionary["structure"].keys(): @@ -108,6 +114,7 @@ def update_from_dictionary(self, dictionary: dict, lower: bool = False): f"Config dictionary structure segment contained {key} which is not used" ) dictionary.pop("structure") + if "geology" in dictionary: self.geology_config.update(dictionary["geology"]) for key in dictionary["geology"].keys(): @@ -208,3 +215,58 @@ def update_from_file( err_string += "Please check the file exists and is accessible then\n" err_string += "Check the contents for mismatched quotes or brackets!" raise Exception(err_string) + + @beartype.beartype + def validate_config_dictionary(self, config_dict: dict) -> None: + """ + Validate the structure and keys of the configuration dictionary. + + Args: + config_dict (dict): The config dictionary to validate. + + Raises: + ValueError: If the dictionary does not meet the minimum requirements for ma2p2loop. + """ + required_keys = { + "structure": {"dipdir_column", "dip_column"}, + "geology": {"unitname_column", "alt_unitname_column"}, + } + + for section, keys in required_keys.items(): + if section not in config_dict: + logger.error(f"Missing required section '{section}' in config dictionary.") + raise ValueError(f"Missing required section '{section}' in config dictionary.") + + for key in keys: + if key not in config_dict[section]: + logger.error( + f"Missing required key '{key}' for '{section}' section of the config dictionary." + ) + raise ValueError( + f"Missing required key '{key}' for '{section}' section of the config dictionary." + ) + + @beartype.beartype + def check_for_legacy_keys(self, config_dict: dict) -> None: + + legacy_keys = { + "otype", "dd", "d", "sf", "bedding", "bo", "btype", "gi", "c", "u", + "g", "g2", "ds", "min", "max", "r1", "r2", "sill", "intrusive", "volcanic", + "f", "fdipnull", "fdipdip_flag", "fdipdir", "fdip", "fdipest", + "fdipest_vals", "n", "ff", "t", "syn" + } + + # Recursively search for keys in the dictionary + def check_keys(d: dict, parent_key=""): + for key, value in d.items(): + if key in legacy_keys: + logger.error( + f"Legacy key found in config - '{key}' at '{parent_key + key}'. Please use the new config format. Use map2loop.utils.update_from_legacy_file to convert between the formats if needed" + ) + raise ValueError( + f"Legacy key found in config - '{key}' at '{parent_key + key}'. Please use the new config format. Use map2loop.utils.update_from_legacy_file to convert between the formats if needed" + ) + if isinstance(value, dict): + check_keys(value, parent_key=f"{parent_key}{key}.") + + check_keys(config_dict) \ No newline at end of file diff --git a/map2loop/mapdata.py b/map2loop/mapdata.py index 59fcd3b3..c1c8b653 100644 --- a/map2loop/mapdata.py +++ b/map2loop/mapdata.py @@ -267,8 +267,9 @@ def set_config_filename( Flag to convert the config file to lowercase. Defaults to False. """ logger.info('Setting config filename to {filename}') - + self.config.update_from_file(filename, lower=lower) + logger.info(f"Config is: {self.config.to_dict()}") def get_config_filename(self): diff --git a/map2loop/project.py b/map2loop/project.py index 1dd832b6..39aac197 100644 --- a/map2loop/project.py +++ b/map2loop/project.py @@ -105,7 +105,7 @@ def __init__( The filename of the configuration json file to use (if not using config_dictionary). Defaults to "". config_dictionary (dict, optional): A dictionary version of the configuration file. Defaults to {}. - clut_filename (str, deprecated): + clut_filename (str, optional): The filename of the colour look up table to use. Defaults to "". save_pre_checked_map_data (bool, optional): A flag to save all map data to file before use. Defaults to False. @@ -201,14 +201,13 @@ def __init__( self.map_data.set_filename(Datatype.DTM, dtm_filename) if fault_orientation_filename != "": self.map_data.set_filename(Datatype.FAULT_ORIENTATION, fault_orientation_filename) - + if config_filename != "": - - self.map_data.set_config_filename(config_filename) if config_dictionary != {}: self.map_data.config.update_from_dictionary(config_dictionary) + if clut_filename != "": self.map_data.set_colour_filename(clut_filename)