source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/validators.py @ 2897

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/validators.py@2897
Revision 2897, 4.5 KB checked in by domlowe, 12 years ago (diff)

Added proxyname to development.ini. Needed for wcs retrieval and demo today, cannot see another way of getting at it. Also some changes to 3D bounding box validator to allow CRS atttribute

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6Validate OWS operation parameters using these functions passed in the validator argument to
7@parameter.
8
9@author: Stephen Pascoe
10"""
11
12from ows_common.exceptions import *
13
14def single_layer(layers):
15    """
16    Checks that only one layer is requested.
17
18    """
19    # Support a single layer only
20    if ',' in layers:
21        raise InvalidParameterValue('Only one layer supported per request', 'layers')
22
23    return layers
24
25
26def bbox_2d(bbox):
27    """
28    Checks it's a 2D BBOX and parse it into a tuple of floats.
29
30    """
31
32    try:
33        bbox_t = [float(x) for x in bbox.split(',')]
34    except ValueError:
35        raise InvalidParameterValue("Incorrect BBOX specification %s" % bbox, 'bbox')
36
37    if len(bbox_t) != 4:
38        raise InvalidParameterValue("Incorrect BBOX specification %s" % bbox, 'bbox')
39
40    return bbox_t
41
42def bbox_2or3d(bbox):
43    """
44    WCS style bounding box: "minX , minY, maxX, maxY, srsName"
45    WCS style bounding box: "minX , minY, maxX, maxY, srsName"
46    Checks it's a 2D or 3D BBOX and parse it into a tuple of floats.
47
48    """   
49    bboxlist=bbox.split(',')
50    if len(bboxlist) in [5,7]:
51        #strip off the crs argument as we don't use it..
52        temp = bboxlist[:-1]
53        bboxlist=temp
54    try:
55        bbox_t = [float(x) for x in bboxlist]
56    except ValueError:
57        raise InvalidParameterValue("Incorrect BBOX specification %s" % bbox, 'bbox')
58 
59    if len(bbox_t) not in [4,6]:       
60        #should either have two or 3 pairs now
61        raise InvalidParameterValue("Incorrect BBOX specification %s" % bbox, 'bbox')
62
63    return bbox_t
64
65
66
67def iso8601_time(timestring):
68    """
69        Checks if timestring is a valid iso8601 time string or valid iso8601 time interval
70        This is not a complete implementation of the full iso8601 spec (although could be extended to be so).
71       
72    """
73    #validates:
74    #single time value e.g. 2002-03-01T13:00:00Z
75    #start and end times: e.g 2002-03-01T13:00:00Z/2003-05-11T15:30:00Z
76    #regex matches:
77    #2002-03-01T13:00:00Z
78    #2002-03-01T13:00:00
79    #2002-03-01T13:00:00.00Z
80    #2002-03-01T13:00:00.00
81    #2002-03-01T13:00:00.0Z
82    #2002-03-01T13:00:00.0
83    def _checkTime(timestring):
84        import re
85        regex=re.compile('^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])T(?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?[.]?[0-5]?[0-9]?[Zz]?$')
86
87        if regex.match(timestring) !=None:
88            #time string is ok, return it without any trailing Z
89            if timestring[-1] in ['Z','z']:
90                return timestring[:-1]
91            else:
92                return timestring
93        else: 
94            #timestring is not ok.
95            raise InvalidParameterValue("Incorrect TimeSequence specification %s" % timestring, 'timesequence')
96           
97    #split the timesequence request in to individual timestrings and test each one
98    if timestring.find(',') == -1:
99        timelist=timestring.split('/')
100    else:
101        timelist=timestring.split(',')
102
103    if len(timelist) == 1:         
104        tOK=_checkTime(timelist[0])
105        return tOK
106   
107    else:
108        newtimelist=[]
109        for t in timelist:
110            newtimelist.append(str(_checkTime(t)))
111        return tuple(newtimelist)
112
113
114def integer(param_name, min=None, max=None):
115    """
116    Factory function which returns a validator for integers within a certain range.
117
118    param_name is required to produce valid exceptions.
119
120    """
121    def f(param):
122        try:
123            param_i = int(param)
124        except ValueError:
125            raise InvalidParameterValue("Parameter not an integer", param_name)
126
127        if min is not None and param_i < min:
128            raise InvalidParameterValue("Parameter out of range", param_name)
129        if max is not None and param_i > max:
130            raise InvalidParameterValue("Parameter out of range", param_name)
131
132        return param_i
133
134    return f
135
136
137def boolean(param_name):
138    """
139    Factory function which returns a validator for boolean types.
140
141    'true', 'false', 'yes', 'no', '1', '0' are accepted (case insensitive)
142
143    """
144    def f(param):
145        p = param.lower()
146
147        if p in ['true', 'yes', '1']:
148            return True
149        if p in ['false', 'no', '0']:
150            return False
151
152        raise InvalidParameterValue("Parameter must be boolean", param_name)
153    return f
Note: See TracBrowser for help on using the repository browser.