Skip to content

Commit

Permalink
fix: add check for missing/invalid args in project
Browse files Browse the repository at this point in the history
  • Loading branch information
AngRodrigues committed Dec 17, 2024
1 parent 1760a90 commit d55523e
Showing 1 changed file with 80 additions and 5 deletions.
85 changes: 80 additions & 5 deletions map2loop/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
gdal.UseExceptions()
import geopandas
import beartype
from beartype.typing import Union, List
from beartype.typing import Union, List, Dict, Any
import pathlib
import numpy
import pandas
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
loop_project_filename: str = "",
overwrite_loopprojectfile: bool = False,
**kwargs,
):
):
"""
The initialiser for the map2loop project
Expand Down Expand Up @@ -119,6 +119,28 @@ def __init__(
TypeError: Type of bounding_box not a dict or tuple
ValueError: use_australian_state_data not in state list ['WA', 'SA', 'QLD', 'NSW', 'TAS', 'VIC', 'ACT', 'NT']
"""

# Throw error if unexpected keyword arguments are passed to project
allowed_kwargs = {"metadata_filename"}
for key in kwargs.keys():
if key not in allowed_kwargs:
logger.error(
f"Unexpected keyword argument '{key}' passed to Project. Allowed keywords: {', '.join(allowed_kwargs)}."
)
raise TypeError(
f"Project got an unexpected keyword argument '{key}' - please double-check this before proceeding."
)

# make sure all the needed arguments are provided
self.validate_required_inputs(
bounding_box=bounding_box,
working_projection=working_projection,
geology_filename=geology_filename,
structure_filename=structure_filename,
dtm_filename=dtm_filename,
config_dictionary=config_dictionary,
config_filename=config_filename,
)
self._error_state = ErrorState.NONE
self._error_state_msg = ""
self.verbose_level = verbose_level
Expand Down Expand Up @@ -233,6 +255,58 @@ def __init__(
if len(kwargs):
logger.warning(f"Unused keyword arguments: {kwargs}")

@beartype.beartype
def validate_required_inputs(
self,
bounding_box: Dict[str, Union[float, int]],
working_projection: str,
geology_filename: str,
structure_filename: str,
dtm_filename: str,
config_filename: str = None,
config_dictionary: Dict[str, Any] = {},
) -> None:

required_inputs = {
"bounding_box": bounding_box,
"working_projection": working_projection,
"geology_filename": geology_filename,
"structure_filename": structure_filename,
"dtm_filename": dtm_filename,
}

# Check for missing required inputs in project
missing_inputs = [key for key, value in required_inputs.items() if not value]

if missing_inputs:
missing_list = ", ".join(missing_inputs)
logger.error(
f"Project construction is missing required inputs: {missing_list}. "
"Please add them to the Project()."
)
raise ValueError(
f"Project construction is missing required inputs: {missing_list}. "
"Please add them to the Project()."
)

# Either config_filename or config_dictionary must be provided (but not both or neither)
if not config_filename and not config_dictionary:
logger.error(
"Either 'config_filename' or 'config_dictionary' must be provided to initialize the Project."
)
raise ValueError(
"Either 'config_filename' or 'config_dictionary' must be provided to initialize the Project."
)
if config_filename and config_dictionary:
logger.error(
"Both 'config_filename' and 'config_dictionary' were provided. Please specify only one."
)
raise ValueError(
"Both 'config_filename' and 'config_dictionary' were provided. Please specify only one."
)



# Getters and Setters
@beartype.beartype
def set_ignore_lithology_codes(self, codes: list):
Expand Down Expand Up @@ -734,9 +808,10 @@ def save_into_projectfile(self):
logger.info('Saving data into loop project file')
if not self.loop_filename:
logger.info('No loop project file specified, creating a new one')
self.loop_filename = os.path.join(
self.map_data.tmp_path, os.path.basename(self.map_data.tmp_path) + ".loop3d"
)
output_dir = pathlib.Path.cwd()
output_dir.mkdir(parents=True, exist_ok=True)
filename = "new_project.loop3d"
self.loop_filename = str(output_dir / filename)

file_exists = os.path.isfile(self.loop_filename)

Expand Down

0 comments on commit d55523e

Please sign in to comment.