Skip to content

Commit

Permalink
fix: allow json files, check for old keys
Browse files Browse the repository at this point in the history
  • Loading branch information
AngRodrigues committed Dec 5, 2024
1 parent b265b3b commit bc2f2ff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
64 changes: 63 additions & 1 deletion map2loop/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion map2loop/mapdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions map2loop/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit bc2f2ff

Please sign in to comment.