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

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

Fixed bugs in qs_util.py via a regression test (test_wxs.py).

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