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

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

WMS 1.1.1 capabilities improved

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 OWS protocol processing.
7
8@author: Stephen Pascoe
9"""
10
11from ows_common import exceptions as OWS_E
12
13
14def parse_version(version):
15    """
16    Convert a version string into a tuple of integers suitable for doing
17    comparisons on.
18
19    """
20    return tuple(int(x) for x in version.split('.'))
21
22def serialise_version(version):
23    """
24    Convert a version tuple back to a string.
25
26    """
27    return '.'.join(str(v) for v in version)
28
29def negotiate_version(serverVersions, clientVersion=None):
30    """
31    Negotiate which OWS version to use based on the version supplied by
32    the client and the list of supported versions.
33
34    """
35    versions = [parse_version(v) for v in serverVersions]
36    versions.sort()
37   
38    if clientVersion is None:
39        return versions[-1]
40   
41    cv = parse_version(clientVersion)
42
43    pv = versions[0]
44    for v in versions:
45        if cv == v:
46            return serialise_version(v)
47        if cv < v:
48            return serialise_version(pv)
49        pv = v
50
51    return serialise_version(pv)
52
53
54def check_updatesequence(clientUpdateSequence, serverUpdateSequence):
55    if clientUpdateSequence and serverUpdateSequence:
56        if client_updatesequence == serverUpdateSequence:
57            raise OWS_E.CurrentUpdateSequence
58        elif client_updatesequence > serverUpdateSequence:
59            raise OWS_E.InvalidUpdateSequence
60   
61                     
62
63#-----------------------------------------------------------------------------
64
65_test_versions = ['1.0', '1.1.1', '1.3.0']
66def test_version_negotiation1():
67    assert negotiate_version(_test_versions) == (1, 3, 0)
68def test_version_negotiation2():
69    assert negotiate_version(_test_versions, '0.1') == (1, 0)
70def test_version_negotiation3():
71    assert negotiate_version(_test_versions, '1.4') == (1,3,0)
72def test_version_negotiation4():
73    assert negotiate_version(_test_versions, '1.1.1') == (1,1,1)
74def test_version_negotiation5():
75    assert negotiate_version(_test_versions, '1.2') == (1,1,1)
76
Note: See TracBrowser for help on using the repository browser.