diff options
author | Facundo Batista <facundobatista@gmail.com> | 2008-09-03 22:35:50 (GMT) |
---|---|---|
committer | Facundo Batista <facundobatista@gmail.com> | 2008-09-03 22:35:50 (GMT) |
commit | c585df94766702ac6d6f3f0d27ad5d07e5546164 (patch) | |
tree | fafe4eb0d5bc1d5e6935ab4baf0f203d833d7ccd /Lib/urlparse.py | |
parent | 69acb43327be1ee4dd19c4df3dc67668ebaa2d66 (diff) | |
download | cpython-c585df94766702ac6d6f3f0d27ad5d07e5546164.zip cpython-c585df94766702ac6d6f3f0d27ad5d07e5546164.tar.gz cpython-c585df94766702ac6d6f3f0d27ad5d07e5546164.tar.bz2 |
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
to the urlparse one. Added a PendingDeprecationWarning in the old
module, it will be deprecated in the future. Docs and tests updated.
Diffstat (limited to 'Lib/urlparse.py')
-rw-r--r-- | Lib/urlparse.py | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/Lib/urlparse.py b/Lib/urlparse.py index 1914304..c56d883 100644 --- a/Lib/urlparse.py +++ b/Lib/urlparse.py @@ -5,7 +5,7 @@ UC Irvine, June 1995. """ __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", - "urlsplit", "urlunsplit"] + "urlsplit", "urlunsplit", "parse_qs", "parse_qsl"] # A classification of schemes ('' means apply by default) uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'imap', @@ -267,6 +267,92 @@ def urldefrag(url): else: return url, '' +# unquote method for parse_qs and parse_qsl +# Cannot use directly from urllib as it would create circular reference. +# urllib uses urlparse methods ( urljoin) + +_hextochr = dict(('%02x' % i, chr(i)) for i in range(256)) +_hextochr.update(('%02X' % i, chr(i)) for i in range(256)) + +def unquote(s): + """unquote('abc%20def') -> 'abc def'.""" + res = s.split('%') + for i in xrange(1, len(res)): + item = res[i] + try: + res[i] = _hextochr[item[:2]] + item[2:] + except KeyError: + res[i] = '%' + item + except UnicodeDecodeError: + res[i] = unichr(int(item[:2], 16)) + item[2:] + return "".join(res) + +def parse_qs(qs, keep_blank_values=0, strict_parsing=0): + """Parse a query given as a string argument. + + Arguments: + + qs: URL-encoded query string to be parsed + + keep_blank_values: flag indicating whether blank values in + URL encoded queries should be treated as blank strings. + A true value indicates that blanks should be retained as + blank strings. The default false value indicates that + blank values are to be ignored and treated as if they were + not included. + + strict_parsing: flag indicating what to do with parsing errors. + If false (the default), errors are silently ignored. + If true, errors raise a ValueError exception. + """ + dict = {} + for name, value in parse_qsl(qs, keep_blank_values, strict_parsing): + if name in dict: + dict[name].append(value) + else: + dict[name] = [value] + return dict + +def parse_qsl(qs, keep_blank_values=0, strict_parsing=0): + """Parse a query given as a string argument. + + Arguments: + + qs: URL-encoded query string to be parsed + + keep_blank_values: flag indicating whether blank values in + URL encoded queries should be treated as blank strings. A + true value indicates that blanks should be retained as blank + strings. The default false value indicates that blank values + are to be ignored and treated as if they were not included. + + strict_parsing: flag indicating what to do with parsing errors. If + false (the default), errors are silently ignored. If true, + errors raise a ValueError exception. + + Returns a list, as G-d intended. + """ + pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')] + r = [] + for name_value in pairs: + if not name_value and not strict_parsing: + continue + nv = name_value.split('=', 1) + if len(nv) != 2: + if strict_parsing: + raise ValueError, "bad query field: %r" % (name_value,) + # Handle case of a control-name with no equal sign + if keep_blank_values: + nv.append('') + else: + continue + if len(nv[1]) or keep_blank_values: + name = unquote(nv[0].replace('+', ' ')) + value = unquote(nv[1].replace('+', ' ')) + r.append((name, value)) + + return r + test_input = """ http://a/b/c/d |