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

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

Made OwsController? much simpler. Features will be reintroduced gradualy
or left to subclasses. Included hooks for updateSequence and version
negotiation.

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