Skip to content

Commit

Permalink
Merge pull request #668 from gschwind/improve-literaltype-implementation
Browse files Browse the repository at this point in the history
Improve the implementation of pywps.inout.literaltypes
  • Loading branch information
cehbrecht authored Feb 7, 2023
2 parents 6fa8a21 + d4ee662 commit d0bb978
Showing 1 changed file with 46 additions and 51 deletions.
97 changes: 46 additions & 51 deletions pywps/inout/literaltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -253,6 +237,7 @@ def convert_boolean(inpt):
return val


@register_convert_type('float')
def convert_float(inpt):
"""Return float value from inpt
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"""

Expand All @@ -293,6 +282,7 @@ def convert_positiveInteger(inpt):
return inpt


@register_convert_type('anyURI')
def convert_anyURI(inpt):
"""Return value of input
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit d0bb978

Please sign in to comment.