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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/qs_util.py@4570
Revision 4570, 2.6 KB checked in by spascoe, 12 years ago (diff)

Added QUERY_STRING parsing utilities directly from Python 2.6.1 codebase. These will be edited to provide a workaround for ticket:1014.

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