source: TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/util.py @ 3408

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/util.py@3408
Revision 3408, 2.4 KB checked in by spascoe, 13 years ago (diff)

Trying without any decorators

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"""
6Utilities for constructing ows_common objects
7
8@author: Stephen Pascoe
9"""
10
11from ows_common.domain import Domain, PossibleValues
12from ows_common import exceptions as OWS_E
13
14def make_domain(value=None, possibleValues=None, **kwargs):
15    """
16    Construct a domain object.
17
18    @param value: The defaultValue of the domain
19    @param possibleValues: Either a list of possible values,
20        a PossibleValues object or None to represent any value
21    @param kwargs: all other arguments passed to the Domain constructor.
22    """
23    if possibleValues is None:
24        pv = PossibleValues.fromAnyValue()
25    elif isinstance(possibleValues, PossibleValues):
26        pv = possibleValues
27    else:
28        pv = PossibleValues.fromAllowedValues(possibleValues)
29
30    return Domain(defaultValue=value, possibleValues=pv, **kwargs)
31
32
33
34
35def parse_version(version):
36    return tuple(int(x) for x in version.split('.'))
37
38def negotiate_version(serverVersions, clientVersion=None):
39    versions = [parse_version(v) for v in serverVersions]
40    versions.sort()
41   
42    if clientVersion is None:
43        return versions[-1]
44   
45    cv = parse_version(clientVersion)
46
47    pv = versions[0]
48    for v in versions:
49        if cv == v:
50            return v
51        if cv < v:
52            return pv
53        pv = v
54
55    return pv
56
57
58def check_updatesequence(clientUpdateSequence, serverUpdateSequence):
59    if clientUpdateSequence and serverUpdateSequence:
60        if client_updatesequence == serverUpdateSequence:
61            raise OWS_E.CurrentUpdateSequence
62        elif client_updatesequence > serverUpdateSequence:
63            raise OWS_E.InvalidUpdateSequence
64   
65
66#-----------------------------------------------------------------------------
67
68_test_versions = ['1.0', '1.1.1', '1.3.0']
69def test_version_negotiation1():
70    assert negotiate_version(_test_versions) == (1, 3, 0)
71def test_version_negotiation2():
72    assert negotiate_version(_test_versions, '0.1') == (1, 0)
73def test_version_negotiation3():
74    assert negotiate_version(_test_versions, '1.4') == (1,3,0)
75def test_version_negotiation4():
76    assert negotiate_version(_test_versions, '1.1.1') == (1,1,1)
77def test_version_negotiation5():
78    assert negotiate_version(_test_versions, '1.2') == (1,1,1)
79
Note: See TracBrowser for help on using the repository browser.