Skip to content

Commit

Permalink
Refactor subargument handling in area_config.py
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Nov 19, 2023
1 parent a960a79 commit a88f9f8
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions pyresample/area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _create_area_def_from_dict(area_name, params):
return area_def


def _capture_subarguments(params, arg_name, sub_arg_list):
def _capture_subarguments(params: dict, arg_name: str, sub_arg_list: list[str]) -> Any:
"""Capture :func:`~pyresample.utils.create_area_def` sub-arguments (i.e. units, height, dx, etc) from a yaml file.
Example:
Expand All @@ -232,21 +232,7 @@ def _capture_subarguments(params, arg_name, sub_arg_list):
argument = params.get(arg_name)
if not isinstance(argument, dict):
return argument
argument_keys = argument.keys()
for sub_arg in argument_keys:
# Verify that provided sub-arguments are valid.
if sub_arg not in sub_arg_list:
raise ValueError('Invalid area definition: {0} is not a valid sub-argument for {1}'.format(sub_arg,
arg_name))
elif arg_name in argument_keys:
# If the arg_name is provided as a sub_arg, then it contains all the data and does not need other sub_args.
if sub_arg != arg_name and sub_arg != 'units':
raise ValueError('Invalid area definition: {0} has too many sub-arguments: Both {0} and {1} were '
'specified.'.
format(arg_name, sub_arg))
# If the arg_name is provided, it's expected that units is also provided.
elif 'units' not in argument_keys:
raise ValueError('Invalid area definition: {0} has the sub-argument {0} without units'.format(arg_name))
_validate_sub_arg_list(argument, arg_name, sub_arg_list)
units = argument.pop('units', None)
list_of_values = argument.pop(arg_name, [])
for sub_arg in sub_arg_list:
Expand All @@ -263,6 +249,23 @@ def _capture_subarguments(params, arg_name, sub_arg_list):
return list_of_values


def _validate_sub_arg_list(argument, arg_name, sub_arg_list):
argument_keys = argument.keys()
for sub_arg in argument_keys:
# Verify that provided sub-arguments are valid.
if sub_arg not in sub_arg_list:
raise ValueError(f"Invalid area definition: {sub_arg} is not a valid sub-argument for {arg_name}")

Check warning on line 257 in pyresample/area_config.py

View check run for this annotation

Codecov / codecov/patch

pyresample/area_config.py#L257

Added line #L257 was not covered by tests
if arg_name in argument_keys:
# If the arg_name is provided as a sub_arg, then it contains all the data and does not need other sub_args.
if sub_arg != arg_name and sub_arg != "units":
raise ValueError(

Check warning on line 261 in pyresample/area_config.py

View check run for this annotation

Codecov / codecov/patch

pyresample/area_config.py#L261

Added line #L261 was not covered by tests
f"Invalid area definition: {arg_name} has too many sub-arguments: "
f"Both {arg_name} and {sub_arg} were specified.")
# If the arg_name is provided, it's expected that units is also provided.
if 'units' not in argument_keys:
raise ValueError(f"Invalid area definition: {arg_name} has the sub-argument {arg_name} without units")

Check warning on line 266 in pyresample/area_config.py

View check run for this annotation

Codecov / codecov/patch

pyresample/area_config.py#L266

Added line #L266 was not covered by tests


def _read_legacy_area_file_lines(area_file_name):
if isinstance(area_file_name, str):
area_file_name = [area_file_name]
Expand Down

0 comments on commit a88f9f8

Please sign in to comment.