/usr/lib/python2.7/dist-packages/pywps/inout/literaltypes.py is in python-pywps 4.0.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | ##################################################################
# Copyright 2016 OSGeo Foundation, #
# represented by PyWPS Project Steering Committee, #
# licensed under MIT, Please consult LICENSE.txt for details #
##################################################################
"""Literaltypes are used for LiteralInputs, to make sure, input data are OK
"""
from pywps._compat import urlparse
import time
from dateutil.parser import parse as date_parser
import datetime
from pywps.exceptions import InvalidParameterValue
from pywps.validator.allowed_value import RANGECLOSURETYPE
from pywps.validator.allowed_value import ALLOWEDVALUETYPE
from pywps._compat import PY2
from pywps import OWS, NAMESPACES
import logging
LOGGER = logging.getLogger('PYWPS')
LITERAL_DATA_TYPES = ('float', 'boolean', 'integer', 'string',
'positiveInteger', 'anyURI', 'time', 'date', 'dateTime',
'scale', 'angle',
'nonNegativeInteger')
# currently we are supporting just ^^^ data types, feel free to add support for
# more
# 'measure', 'angleList',
# 'angle', 'integerList',
# 'positiveIntegerList',
# 'lengthOrAngle', 'gridLength',
# 'measureList', 'lengthList',
# 'gridLengthList', 'scaleList', 'timeList',
# 'nonNegativeInteger', 'length'
class AnyValue(object):
"""Any value for literal input
"""
@property
def json(self):
return {'type': 'anyvalue'}
class NoValue(object):
"""No value allowed
NOTE: not really implemented
"""
@property
def json(self):
return {'type': 'novalue'}
class ValuesReference(object):
"""Any value for literal input
NOTE: not really implemented
"""
@property
def json(self):
return {'type': 'valuesreference'}
class AllowedValue(AnyValue):
"""Allowed value parameters
the values are evaluated in literal validator functions
:param pywps.validator.allowed_value.ALLOWEDVALUETYPE allowed_type: VALUE or RANGE
:param value: single value
:param minval: minimal value in case of Range
:param maxval: maximal value in case of Range
:param spacing: spacing in case of Range
:param pywps.input.literaltypes.RANGECLOSURETYPE range_closure:
"""
def __init__(self, allowed_type=ALLOWEDVALUETYPE.VALUE, value=None,
minval=None, maxval=None, spacing=None,
range_closure=RANGECLOSURETYPE.CLOSED):
AnyValue.__init__(self)
self.allowed_type = allowed_type
self.value = value
self.minval = minval
self.maxval = maxval
self.spacing = spacing
self.range_closure = range_closure
def describe_xml(self):
"""Return back Element for DescribeProcess response
"""
doc = None
if self.allowed_type == ALLOWEDVALUETYPE.VALUE:
doc = OWS.Value(str(self.value))
else:
doc = OWS.Range()
doc.set('{%s}rangeClosure' % NAMESPACES['ows'], self.range_closure)
doc.append(OWS.MinimumValue(str(self.minval)))
doc.append(OWS.MaximumValue(str(self.maxval)))
if self.spacing:
doc.append(OWS.Spacing(str(self.spacing)))
return doc
@property
def json(self):
value = self.value
if hasattr(value, 'json'):
value = value.json
return {
'type': 'allowedvalue',
'allowed_type': self.allowed_type,
'value': value,
'minval': self.minval,
'maxval': self.maxval,
'spacing': self.spacing,
'range_closure': self.range_closure
}
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
def convert_boolean(inpt):
"""Return boolean value from input boolean input
>>> convert_boolean('1')
True
>>> convert_boolean('-1')
True
>>> convert_boolean('FaLsE')
False
>>> convert_boolean('FaLsEx')
True
>>> convert_boolean(0)
False
"""
val = False
if str(inpt).lower() in ['false', 'f']:
val = False
else:
try:
val = int(inpt)
if val == 0:
val = False
else:
val = True
except:
val = True
return val
def convert_float(inpt):
"""Return float value from inpt
>>> convert_float('1')
1.0
"""
return float(inpt)
def convert_integer(inpt):
"""Return integer value from input inpt
>>> convert_integer('1.0')
1
"""
return int(float(inpt))
def convert_string(inpt):
"""Return string value from input lit_input
>>> convert_string(1)
'1'
"""
if PY2:
return str(inpt).decode()
else:
return str(inpt)
def convert_positiveInteger(inpt):
"""Return value of input"""
inpt = convert_integer(inpt)
if inpt < 0:
raise InvalidParameterValue(
'The value "{}" is not of type positiveInteger'.format(inpt))
else:
return inpt
def convert_anyURI(inpt):
"""Return value of input
:rtype: url components
"""
inpt = convert_string(inpt)
components = urlparse.urlparse(inpt)
if components[0] and components[1]:
return components
else:
raise InvalidParameterValue(
'The value "{}" does not seem to be of type anyURI'.format(inpt))
def convert_time(inpt):
"""Return value of input
time formating assumed according to ISO standard:
https://www.w3.org/TR/xmlschema-2/#time
Examples: 12:00:00
:rtype: datetime.time object
"""
if not isinstance(inpt, datetime.time):
inpt = convert_datetime(inpt).time()
return inpt
def convert_date(inpt):
"""Return value of input
date formating assumed according to ISO standard:
https://www.w3.org/TR/xmlschema-2/#date
Examples: 2016-09-20
:rtype: datetime.date object
"""
if not isinstance(inpt, datetime.date):
inpt = convert_datetime(inpt).date()
return inpt
def convert_datetime(inpt):
"""Return value of input
dateTime formating assumed according to ISO standard:
* http://www.w3.org/TR/NOTE-datetime
* https://www.w3.org/TR/xmlschema-2/#dateTime
Examples: 2016-09-20T12:00:00, 2012-12-31T06:30:00Z,
2017-01-01T18:00:00+01:00
:rtype: datetime.datetime object
"""
# TODO: %z directive works only with python 3
# time_format = '%Y-%m-%dT%H:%M:%S%z'
# time_format = '%Y-%m-%dT%H:%M:%S%Z'
# inpt = time.strptime(convert_string(inpt), time_format)
if not isinstance(inpt, datetime.datetime):
inpt = convert_string(inpt)
inpt = date_parser(inpt)
return inpt
def convert_scale(inpt):
"""Return value of input"""
return convert_float(inpt)
def convert_angle(inpt):
"""Return value of input
return degrees
"""
inpt = convert_float(inpt)
return inpt % 360
def make_allowedvalues(allowed_values):
"""convert given value list to AllowedValue objects
:return: list of pywps.inout.literaltypes.AllowedValue
"""
new_allowedvalues = []
for value in allowed_values:
if isinstance(value, AllowedValue):
new_allowedvalues.append(value)
elif type(value) == tuple or type(value) == list:
minval = maxval = spacing = None
if len(value) == 2:
minval = value[0]
maxval = value[1]
else:
minval = value[0]
spacing = value[1]
maxval = value[2]
new_allowedvalues.append(
AllowedValue(allowed_type=ALLOWEDVALUETYPE.RANGE,
minval=minval, maxval=maxval,
spacing=spacing)
)
else:
new_allowedvalues.append(AllowedValue(value=value))
return new_allowedvalues
def is_anyvalue(value):
"""Check for any value object of given value
"""
is_av = False
if value == AnyValue:
is_av = True
elif value is None:
is_av = True
elif isinstance(value, AnyValue):
is_av = True
elif str(value).lower() == 'anyvalue':
is_av = True
return is_av
|