From d4ee66230c021f3849bff918d9f3c2173c146d31 Mon Sep 17 00:00:00 2001 From: Benoit Gschwind Date: Wed, 9 Nov 2022 17:38:59 +0100 Subject: [PATCH] Improve the implementation of pywps.inout.literaltypes --- pywps/inout/literaltypes.py | 97 ++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/pywps/inout/literaltypes.py b/pywps/inout/literaltypes.py index c92295439..086e64228 100644 --- a/pywps/inout/literaltypes.py +++ b/pywps/inout/literaltypes.py @@ -15,10 +15,29 @@ import logging LOGGER = logging.getLogger('PYWPS') -LITERAL_DATA_TYPES = ('float', 'boolean', 'integer', 'string', - 'positiveInteger', 'anyURI', 'time', 'date', 'dateTime', - 'scale', 'angle', - 'nonNegativeInteger', None) +LITERAL_DATA_TYPES = dict() +""" +Store converter functions +""" + + +# Decorator to use on converter function +def register_convert_type(name): + """Decorator to register a converter + + >>> @register_convert_type('type_name') + ... def convert_my_stuff(value): + ... # convert value ... + ... return value + """ + + def _register_convert_type(function): + global LITERAL_DATA_TYPES + LITERAL_DATA_TYPES[name] = function + return function + + return _register_convert_type + # currently we are supporting just ^^^ data types, feel free to add support for # more @@ -170,59 +189,24 @@ def from_json(cls, json_input): ALLOWED_VALUES_TYPES = (AllowedValue, AnyValue, NoValue, ValuesReference) -def get_converter(convertor): - """function for decoration of convert - """ - - def decorator_selector(data_type, data): - convert = None - if data_type in LITERAL_DATA_TYPES: - if data_type == 'string': - convert = convert_string - elif data_type == 'integer': - convert = convert_integer - elif data_type == 'float': - convert = convert_float - elif data_type == 'boolean': - convert = convert_boolean - elif data_type == 'positiveInteger': - convert = convert_positiveInteger - elif data_type == 'anyURI': - convert = convert_anyURI - elif data_type == 'time': - convert = convert_time - elif data_type == 'date': - convert = convert_date - elif data_type == 'dateTime': - convert = convert_datetime - elif data_type == 'scale': - convert = convert_scale - elif data_type == 'angle': - convert = convert_angle - elif data_type == 'nonNegativeInteger': - convert = convert_positiveInteger - else: - raise InvalidParameterValue( - "Invalid data_type value of LiteralInput " - "set to '{}'".format(data_type)) - try: - return convert(data) - except ValueError: - raise InvalidParameterValue( - "Could not convert value '{}' to format '{}'".format( - data, data_type)) - - return decorator_selector - - -@get_converter def convert(data_type, data): """Convert data to target value """ - return data_type, data + convert = LITERAL_DATA_TYPES.get(data_type, None) + if convert is None: + raise InvalidParameterValue( + "Invalid data_type value of LiteralInput " + "set to '{}'".format(data_type)) + try: + return convert(data) + except ValueError: + raise InvalidParameterValue( + "Could not convert value '{}' to format '{}'".format( + data, data_type)) +@register_convert_type('boolean') def convert_boolean(inpt): """Return boolean value from input boolean input @@ -253,6 +237,7 @@ def convert_boolean(inpt): return val +@register_convert_type('float') def convert_float(inpt): """Return float value from inpt @@ -263,6 +248,7 @@ def convert_float(inpt): return float(inpt) +@register_convert_type('integer') def convert_integer(inpt): """Return integer value from input inpt @@ -273,6 +259,7 @@ def convert_integer(inpt): return int(float(inpt)) +@register_convert_type('string') def convert_string(inpt): """Return string value from input lit_input @@ -282,6 +269,8 @@ def convert_string(inpt): return str(inpt) +@register_convert_type('nonNegativeInteger') +@register_convert_type('positiveInteger') def convert_positiveInteger(inpt): """Return value of input""" @@ -293,6 +282,7 @@ def convert_positiveInteger(inpt): return inpt +@register_convert_type('anyURI') def convert_anyURI(inpt): """Return value of input @@ -308,6 +298,7 @@ def convert_anyURI(inpt): 'The value "{}" does not seem to be of type anyURI'.format(inpt)) +@register_convert_type('time') def convert_time(inpt): """Return value of input time formating assumed according to ISO standard: @@ -323,6 +314,7 @@ def convert_time(inpt): return inpt +@register_convert_type('date') def convert_date(inpt): """Return value of input date formating assumed according to ISO standard: @@ -338,6 +330,7 @@ def convert_date(inpt): return inpt +@register_convert_type('dateTime') def convert_datetime(inpt): """Return value of input dateTime formating assumed according to ISO standard: @@ -360,12 +353,14 @@ def convert_datetime(inpt): return inpt +@register_convert_type('scale') def convert_scale(inpt): """Return value of input""" return convert_float(inpt) +@register_convert_type('angle') def convert_angle(inpt): """Return value of input