Skip to content

Commit

Permalink
fix(postprocess): Allow input as file, string, or list
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkelkp committed Jul 14, 2023
1 parent bae3dc4 commit 23923f0
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pollination_handlers/inputs/postprocess.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Handlers for post-processing options."""
import os
import json
from jsonschema import validate

from .helper import get_tempfile


def grid_metrics(json_file):
def grid_metrics(gm_obj):
"""Validate the file for custom grid metrics.
Args:
json_file: A JSON file with custom grid metrics.
gm_obj: An object with custom grid metrics. This can be either a
JSON file, a string, or a list of grid metrics.
Returns:
str -- Path to a the custom grid metrics file.
Expand Down Expand Up @@ -42,8 +44,20 @@ def grid_metrics(json_file):
}
}

with open(json_file) as file:
grid_metrics = json.load(file)
if isinstance(gm_obj, str):
if os.path.isfile(gm_obj):
with open(gm_obj) as file:
grid_metrics = json.load(file)
else:
grid_metrics = json.loads(gm_obj)
elif isinstance(gm_obj, list):
grid_metrics = [_gm for _gm in gm_obj]
else:
raise TypeError(
'Unexpected type of input gm_obj. Valid types are str and list. '
'Type of input is: %s.' % type(gm_obj)
)

validate(grid_metrics, schema)

file_path = get_tempfile('json', 'grid_metrics')
Expand Down

0 comments on commit 23923f0

Please sign in to comment.