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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/qs_util.py@4917
Revision 4912, 2.9 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"""
9This module replaces urlparse.parse_qsl and urlparse.parse_qs with versions that
10provide control over the use of ';' as a KVP separator.
11
12The code is based on http://svn.python.org/view/python/tags/r261/Lib/urlparse.py?rev=67516&view=markup
13
14"""
15
16from urllib import unquote
17
18def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
19    """Parse a query given as a string argument.
20
21        Arguments:
22
23        qs: URL-encoded query string to be parsed
24
25        keep_blank_values: flag indicating whether blank values in
26            URL encoded queries should be treated as blank strings.
27            A true value indicates that blanks should be retained as
28            blank strings.  The default false value indicates that
29            blank values are to be ignored and treated as if they were
30            not included.
31
32        strict_parsing: flag indicating what to do with parsing errors.
33            If false (the default), errors are silently ignored.
34            If true, errors raise a ValueError exception.
35    """
36    dict = {}
37    for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
38        if name in dict:
39            dict[name].append(value)
40        else:
41            dict[name] = [value]
42    return dict
43
44def parse_qsl(qs, keep_blank_values=0, strict_parsing=0, semicolon_separator=1):
45    """Parse a query given as a string argument.
46
47    Arguments:
48
49    qs: URL-encoded query string to be parsed
50
51    keep_blank_values: flag indicating whether blank values in
52        URL encoded queries should be treated as blank strings.  A
53        true value indicates that blanks should be retained as blank
54        strings.  The default false value indicates that blank values
55        are to be ignored and treated as if they were  not included.
56
57    strict_parsing: flag indicating what to do with parsing errors. If
58        false (the default), errors are silently ignored. If true,
59        errors raise a ValueError exception.
60
61    Returns a list, as G-d intended.
62    """
63    if semicolon_separator:
64        pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
65    else:
66        pairs = qs.split('&')
67    r = []
68    for name_value in pairs:
69        if not name_value and not strict_parsing:
70            continue
71        nv = name_value.split('=', 1)
72        if len(nv) != 2:
73            if strict_parsing:
74                raise ValueError, "bad query field: %r" % (name_value,)
75            # Handle case of a control-name with no equal sign
76            if keep_blank_values:
77                nv.append('')
78            else:
79                continue
80        if len(nv[1]) or keep_blank_values:
81            name = unquote(nv[0].replace('+', ' '))
82            value = unquote(nv[1].replace('+', ' '))
83            r.append((name, value))
84
85    return r
Note: See TracBrowser for help on using the repository browser.