[docs]classComputationStep:""" "Abstract" Class for all Computation Step (ExposurePreAnalysis, GulCalc, ...) initialise the object with all specified param un step_param and sub- ComputationStep provide a generic interface to get the all those parameter definitions (get_params) the Run method must be implemented and contain le business execution logic. """
def__init__(self,**kwargs):""" initialise the ComputationStep objects: - do the basic check for required parameter (required) - provide default value if defined (default) - check path existence (pre_exist) - create necessary directories (is_dir, is_path) """
[docs]defget_params(cls):""" return all the params of the computation step defined in step_params and the params from the sub_computation step in chained_commands if two params have the same name, return the param definition of the first param found only this allow to overwrite the param definition of sub step if necessary. """params=[]param_names=set()defall_params():forparamincls.step_params:yieldparamforcommandincls.chained_commands:forparamincommand.get_params():yieldparamforparaminall_params():ifparam['name']notinparam_names:param_names.add(param['name'])params.append(param)returnparams
@classmethod
[docs]defget_arguments(cls,**kwargs):""" Return a list of default arguments values for the functions parameters If given arg values in 'kwargs' these will override the defaults """func_args={el['name']:el.get('default',None)forelincls.get_params()}type_map={el['name']:el.get('type',None)forelincls.get_params()}func_kwargs=update_config(kwargs)env_override=str2bool(os.getenv('OASIS_ENV_OVERRIDE',default=False))forparaminfunc_args:ifenv_overrideandhas_oasis_env(param):func_args[param]=get_oasis_env(param,type_map[param])elifparaminfunc_kwargs:func_args[param]=func_kwargs[param]returnfunc_args
@classmethod
[docs]defget_signature(cls):""" Create a function signature based on the 'get_params()' return """try:# Create keyword params (without default values)params=["{}=None".format(p.get('name'))forpincls.get_params()ifnotp.get('default')]# Create keyword params (with default values)forpin[pforpincls.get_params()ifp.get('default')]:ifisinstance(p.get('default'),str):params.append("{}='{}'".format(p.get('name'),p.get('default')))elifisinstance(p.get('default'),dict):params.append("{}=dict()".format(p.get('name'),p.get('default')))elifisinstance(p.get('default'),OrderedDict):params.append("{}=OrderedDict()".format(p.get('name'),p.get('default')))else:params.append("{}={}".format(p.get('name'),p.get('default')))exec('def func_sig({}): pass'.format(", ".join(params)))returninspect.signature(locals()['func_sig'])exceptException:# ignore any errors in signature creation and return blankreturnNone
defrun(self):"""method that will be call by all the interface to execute the computation step"""raiseNotImplemented(f'Methode run must be implemented in {self.__class__.__name__}')