source: cows/trunk/cows/util.py @ 4912

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/util.py@4917
Revision 4912, 2.4 KB checked in by spascoe, 11 years ago (diff)

Added LICENSE file and a short banner at the top of each python file.
Use add_license.py to add a license to new files.

NOTE: also contains a couple of files missed at last commit.

Line 
1# BSD Licence
2# Copyright (c) 2009, Science & Technology Facilities Council (STFC)
3# All rights reserved.
4#
5# See the LICENSE file in the source distribution of this software for
6# the full license text.
7
8# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
9# This software may be distributed under the terms of the
10# Q Public License, version 1.0 or later.
11# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
12"""
13Utilities for OWS protocol processing.
14
15@author: Stephen Pascoe
16"""
17
18from cows import exceptions as OWS_E
19
20
21def parse_version(version):
22    """
23    Convert a version string into a tuple of integers suitable for doing
24    comparisons on.
25
26    """
27    return tuple(int(x) for x in version.split('.'))
28
29def serialise_version(version):
30    """
31    Convert a version tuple back to a string.
32
33    """
34    return '.'.join(str(v) for v in version)
35
36def negotiate_version(serverVersions, clientVersion=None):
37    """
38    Negotiate which OWS version to use based on the version supplied by
39    the client and the list of supported versions.
40
41    """
42    versions = [parse_version(v) for v in serverVersions]
43    versions.sort()
44   
45    if clientVersion is None:
46        return serialise_version(versions[-1])
47   
48    cv = parse_version(clientVersion)
49
50    pv = versions[0]
51    for v in versions:
52        if cv == v:
53            return serialise_version(v)
54        if cv < v:
55            return serialise_version(pv)
56        pv = v
57
58    return serialise_version(pv)
59
60
61def check_updatesequence(clientUpdateSequence, serverUpdateSequence):
62    if clientUpdateSequence and serverUpdateSequence:
63        if client_updatesequence == serverUpdateSequence:
64            raise OWS_E.CurrentUpdateSequence
65        elif client_updatesequence > serverUpdateSequence:
66            raise OWS_E.InvalidUpdateSequence
67   
68                     
69
70#-----------------------------------------------------------------------------
71
72_test_versions = ['1.0', '1.1.1', '1.3.0']
73def test_version_negotiation1():
74    assert negotiate_version(_test_versions) == (1, 3, 0)
75def test_version_negotiation2():
76    assert negotiate_version(_test_versions, '0.1') == (1, 0)
77def test_version_negotiation3():
78    assert negotiate_version(_test_versions, '1.4') == (1,3,0)
79def test_version_negotiation4():
80    assert negotiate_version(_test_versions, '1.1.1') == (1,1,1)
81def test_version_negotiation5():
82    assert negotiate_version(_test_versions, '1.2') == (1,1,1)
83
Note: See TracBrowser for help on using the repository browser.