def encode_value(cls, value: tp.Any) -> _meta.Value: if value is None: raise _ex.ETracInternal("Cannot encode a null value") if isinstance(value, bool): type_desc = _meta.TypeDescriptor(_meta.BasicType.BOOLEAN) return _meta.Value(type_desc, booleanValue=value) if isinstance(value, int): type_desc = _meta.TypeDescriptor(_meta.BasicType.INTEGER) return _meta.Value(type_desc, integerValue=value) if isinstance(value, float): type_desc = _meta.TypeDescriptor(_meta.BasicType.FLOAT) return _meta.Value(type_desc, floatValue=value) if isinstance(value, decimal.Decimal): type_desc = _meta.TypeDescriptor(_meta.BasicType.DECIMAL) return _meta.Value(type_desc, decimalValue=_meta.DecimalValue(str(value))) if isinstance(value, str): type_desc = _meta.TypeDescriptor(_meta.BasicType.STRING) return _meta.Value(type_desc, stringValue=value) # dt.datetime inherits dt.date, so check datetime first to avoid encoding datetime as a date if isinstance(value, dt.datetime): type_desc = _meta.TypeDescriptor(_meta.BasicType.DATETIME) return _meta.Value(type_desc, datetimeValue=_meta.DatetimeValue(value.isoformat())) if isinstance(value, dt.date): type_desc = _meta.TypeDescriptor(_meta.BasicType.DATE) return _meta.Value(type_desc, dateValue=_meta.DateValue(value.isoformat())) raise _ex.ETracInternal(f"Encoding value type [{type(value)}] is not supported yet")
def convert_boolean_value(raw_value: tp.Any) -> _meta.Value: type_desc = _meta.TypeDescriptor(_meta.BasicType.BOOLEAN) if isinstance(raw_value, bool): return _meta.Value(type_desc, booleanValue=raw_value) msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.BOOLEAN.name}" raise _ex.ETracInternal(msg)
def convert_decimal_value(raw_value: tp.Any) -> _meta.Value: type_desc = _meta.TypeDescriptor(_meta.BasicType.DECIMAL) if isinstance(raw_value, decimal.Decimal): return _meta.Value(type_desc, decimalValue=_meta.DecimalValue(str(raw_value))) if isinstance(raw_value, int) or isinstance(raw_value, float): return _meta.Value(type_desc, decimalValue=_meta.DecimalValue(str(raw_value))) msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.DECIMAL.name}" raise _ex.ETracInternal(msg)
def convert_float_value(raw_value: tp.Any) -> _meta.Value: type_desc = _meta.TypeDescriptor(_meta.BasicType.FLOAT) if isinstance(raw_value, float): return _meta.Value(type_desc, floatValue=raw_value) if isinstance(raw_value, int): return _meta.Value(type_desc, floatValue=float(raw_value)) msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.FLOAT.name}" raise _ex.ETracInternal(msg)
def convert_integer_value(raw_value: tp.Any) -> _meta.Value: type_desc = _meta.TypeDescriptor(_meta.BasicType.INTEGER) if isinstance(raw_value, int): return _meta.Value(type_desc, integerValue=raw_value) if isinstance(raw_value, float) and raw_value.is_integer(): return _meta.Value(type_desc, integerValue=int(raw_value)) msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.INTEGER.name}" raise _ex.ETracInternal(msg)
def convert_datetime_value(raw_value: tp.Any) -> _meta.Value: type_desc = _meta.TypeDescriptor(_meta.BasicType.DATETIME) if isinstance(raw_value, dt.datetime): return _meta.Value(type_desc, datetimeValue=_meta.DatetimeValue(isoDatetime=raw_value.isoformat())) if isinstance(raw_value, str): datetime_value = dt.datetime.fromisoformat(raw_value) return _meta.Value(type_desc, datetimeValue=_meta.DatetimeValue(isoDatetime=datetime_value.isoformat())) msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.DATETIME.name}" raise _ex.ETracInternal(msg)
def convert_string_value(raw_value: tp.Any) -> _meta.Value: type_desc = _meta.TypeDescriptor(_meta.BasicType.STRING) if isinstance(raw_value, str): return _meta.Value(type_desc, stringValue=raw_value) if isinstance(raw_value, bool) or \ isinstance(raw_value, int) or \ isinstance(raw_value, float) or \ isinstance(raw_value, decimal.Decimal): return _meta.Value(type_desc, stringValue=str(raw_value)) msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.STRING.name}" raise _ex.ETracInternal(msg)
def declare_parameter( param_name: str, param_type: _tp.Union[_meta.TypeDescriptor, _meta.BasicType], label: str, default_value: _tp.Optional[_tp.Any] = None) \ -> _Named[_meta.ModelParameter]: """ Declare an individual model parameter Individual model parameters can be declared using this method (or :py:func:`trac.P<P>`). The name, type and label are required fields to declare a parameter. Name is used as the identifier to work with the parameter in code, e.g. when calling :py:meth:`get_parameter` or defining parameters in a job config. If a default value is specified, the model parameter becomes optional. It is ok to omit optional parameters when running models or setting up jobs, in which case the default value will be used. If no default is specified then the model parameter becomes mandatory, a value must always be supplied in order to execute the model. Declared parameters should be passed to :py:func:`declare_parameters`, either individually or as a list, to create the set of parameters for a model. :param param_name: The parameter name, used to identify the parameter in code (must be a valid identifier) :param param_type: The parameter type, expressed in the TRAC type system :param label: A descriptive label for the parameter (required) :param default_value: A default value to use if no explicit value is supplied (optional) :return: A named model parameter, suitable for passing to :py:func:`declare_parameters` """ if isinstance(param_type, _meta.TypeDescriptor): param_type_descriptor = param_type else: param_type_descriptor = _meta.TypeDescriptor(param_type, None, None) return _Named( param_name, _meta.ModelParameter(param_type_descriptor, label, default_value))
def _td(basic_type: meta.BasicType) -> meta.TypeDescriptor: return meta.TypeDescriptor(basic_type)
def python_to_trac(cls, python_type: type) -> _meta.TypeDescriptor: basic_type = cls.python_to_trac_basic_type(python_type) return _meta.TypeDescriptor(basic_type)