1+ from __future__ import annotations
2+
13import json
24import os
35import sys
46from pathlib import Path
5- from typing import Tuple , Any
7+ from typing import Any , Callable
68
79# Ruff disabled because imports are order-dependent
810# ruff: noqa: I001
3840
3941class GeophiresXSchemaGenerator :
4042 def __init__ (self ):
41- pass
43+ # noinspection PyProtectedMember
44+ self .result_fields_by_category : dict [str , list [Any ]] = GeophiresXResult ._RESULT_FIELDS_BY_CATEGORY
4245
4346 @staticmethod
4447 def _get_dummy_model ():
@@ -85,7 +88,7 @@ def get_parameter_sources(self) -> list:
8588 def get_schema_title (self ) -> str :
8689 return 'GEOPHIRES'
8790
88- def get_parameters_json (self ) -> Tuple [str , str ]:
91+ def get_parameters_json (self ) -> tuple [str , str ]:
8992
9093 def with_category (param_dict : dict , category : str ):
9194 def _with_cat (p : Parameter , cat : str ):
@@ -103,7 +106,7 @@ def _with_cat(p: Parameter, cat: str):
103106
104107 return json_dumpse (input_params ), json_dumpse (output_params )
105108
106- def generate_json_schema (self ) -> Tuple [dict , dict ]:
109+ def generate_json_schema (self ) -> tuple [dict , dict ]:
107110 """
108111 :return: request schema, result schema
109112 :rtype: Tuple[dict, dict]
@@ -153,6 +156,10 @@ def get_result_json_schema(self, output_params_json) -> dict:
153156 properties = {}
154157 required = []
155158
159+ # if result_fields_by_category is None:
160+ # # noinspection PyProtectedMember
161+ # result_fields_by_category = GeophiresXResult._RESULT_FIELDS_BY_CATEGORY
162+
156163 output_params = json .loads (output_params_json )
157164 display_name_aliases = {}
158165 for param_name in output_params :
@@ -165,11 +172,10 @@ def get_result_json_schema(self, output_params_json) -> dict:
165172
166173 output_params = {** output_params , ** display_name_aliases }
167174
168- # noinspection PyProtectedMember
169- for category in GeophiresXResult ._RESULT_FIELDS_BY_CATEGORY :
175+ for category in self .result_fields_by_category :
170176 cat_properties = {}
171- # noinspection PyProtectedMember
172- for field in GeophiresXResult . _RESULT_FIELDS_BY_CATEGORY [category ]:
177+
178+ for field in self . result_fields_by_category [category ]:
173179 param_name = field if isinstance (field , str ) else field .field_name
174180
175181 ignored_output_param_names = ['After-Tax IRR' ] # Silently ignored in favor of "After-tax IRR"
@@ -357,7 +363,7 @@ def _get_key(param: dict, k: str, default_val='') -> Any:
357363 return default_val
358364
359365
360- def _get_min_and_max (param : dict , default_val = '' ) -> Tuple :
366+ def _get_min_and_max (param : dict , default_val = '' ) -> tuple :
361367 min_val = _get_key (param , 'Min' , default_val = default_val )
362368 max_val = _get_key (param , 'Max' , default_val = default_val )
363369
@@ -377,13 +383,30 @@ def _fix_floating_point_error(val: Any) -> Any:
377383
378384
379385class HipRaXSchemaGenerator (GeophiresXSchemaGenerator ):
380- # Single implicit category used to keep the result schema shape consistent
381- # with the GEOPHIRES result schema (top-level properties -> category -> properties -> fields).
382- # FIXME WIP refactor to reflect SUMMARY OF RESULTS and SUMMARY OF INPUTS categories
383- _RESULT_CATEGORY : str = 'HIP-RA-X OUTPUTS'
386+
387+ def __init__ (self ):
388+ dummy_model = HIP_RA_X ()
389+
390+ def _get_result_fields_for_category (
391+ output_config : list [tuple [Parameter , Callable [[Parameter ], str ]]],
392+ ) -> list [Any ]:
393+ return [it [0 ].Name for it in output_config ]
394+
395+ # noinspection PyProtectedMember
396+ self .result_fields_by_category : dict [str , list [Any ]] = {
397+ HIP_RA_X ._SUMMARY_OF_RESULTS_OUTPUT_CATEGORY : _get_result_fields_for_category (
398+ dummy_model ._get_output_config_for_summary_of_results_category (None , None )
399+ ),
400+ HIP_RA_X ._SUMMARY_OF_INPUTS_OUTPUT_CATEGORY : _get_result_fields_for_category (
401+ dummy_model ._get_output_config_for_summary_of_inputs_category (None , None )
402+ ),
403+ }
384404
385405 def get_parameter_sources (self ) -> list :
386406 """
407+ Single implicit input category used to keep the result schema shape consistent
408+ with the GEOPHIRES result schema (top-level properties -> category -> properties -> fields).
409+
387410 :rtype: list[Tuple[Any, str]]
388411 """
389412 dummy_model = HIP_RA_X ()
@@ -395,22 +418,24 @@ def get_schema_title(self) -> str:
395418 def get_result_json_schema (self , output_params_json ) -> dict :
396419 output_params = json .loads (output_params_json )
397420
398- cat_properties = {}
399- for param_name , output_param in output_params .items ():
400- description = _get_key (output_param , 'ToolTipText' , default_val = None ) or None
401- units_val = output_param ['CurrentUnits' ] if isinstance (output_param .get ('CurrentUnits' ), str ) else None
402- cat_properties [param_name ] = {
403- 'type' : _get_key (output_param , 'json_parameter_type' , default_val = None ) or None ,
404- 'description' : description ,
405- 'units' : units_val ,
406- }
421+ properties = {}
407422
408- properties = {
409- self ._RESULT_CATEGORY : {
423+ # noinspection PyProtectedMember
424+ for category in [HIP_RA_X ._SUMMARY_OF_RESULTS_OUTPUT_CATEGORY , HIP_RA_X ._SUMMARY_OF_INPUTS_OUTPUT_CATEGORY ]:
425+ cat_properties = {}
426+ for param_name , output_param in output_params .items ():
427+ description = _get_key (output_param , 'ToolTipText' , default_val = None ) or None
428+ units_val = output_param ['CurrentUnits' ] if isinstance (output_param .get ('CurrentUnits' ), str ) else None
429+ cat_properties [param_name ] = {
430+ 'type' : _get_key (output_param , 'json_parameter_type' , default_val = None ) or None ,
431+ 'description' : description ,
432+ 'units' : units_val ,
433+ }
434+
435+ properties [category ] = {
410436 'type' : 'object' ,
411437 'properties' : cat_properties ,
412438 }
413- }
414439
415440 result_schema = {
416441 'definitions' : {},
@@ -459,4 +484,4 @@ def get_input_schema_reference(self) -> str:
459484 return 'hip-ra-x-request.json'
460485
461486 def get_output_schema_reference (self ) -> str :
462- return None
487+ return 'hip-ra-x-result.json'
0 commit comments