Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: urlparse.py
"""Parse (absolute and relative) URLs.
[0] Fix | Delete
[1] Fix | Delete
urlparse module is based upon the following RFC specifications.
[2] Fix | Delete
[3] Fix | Delete
RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding
[4] Fix | Delete
and L. Masinter, January 2005.
[5] Fix | Delete
[6] Fix | Delete
RFC 2732 : "Format for Literal IPv6 Addresses in URL's by R.Hinden, B.Carpenter
[7] Fix | Delete
and L.Masinter, December 1999.
[8] Fix | Delete
[9] Fix | Delete
RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T.
[10] Fix | Delete
Berners-Lee, R. Fielding, and L. Masinter, August 1998.
[11] Fix | Delete
[12] Fix | Delete
RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zwinski, July 1998.
[13] Fix | Delete
[14] Fix | Delete
RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June
[15] Fix | Delete
1995.
[16] Fix | Delete
[17] Fix | Delete
RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M.
[18] Fix | Delete
McCahill, December 1994
[19] Fix | Delete
[20] Fix | Delete
RFC 3986 is considered the current standard and any future changes to
[21] Fix | Delete
urlparse module should conform with it. The urlparse module is
[22] Fix | Delete
currently not entirely compliant with this RFC due to defacto
[23] Fix | Delete
scenarios for parsing, and for backward compatibility purposes, some
[24] Fix | Delete
parsing quirks from older RFCs are retained. The testcases in
[25] Fix | Delete
test_urlparse.py provides a good indicator of parsing behavior.
[26] Fix | Delete
[27] Fix | Delete
The WHATWG URL Parser spec should also be considered. We are not compliant with
[28] Fix | Delete
it either due to existing user code API behavior expectations (Hyrum's Law).
[29] Fix | Delete
It serves as a useful guide when making changes.
[30] Fix | Delete
[31] Fix | Delete
"""
[32] Fix | Delete
[33] Fix | Delete
import re
[34] Fix | Delete
import os
[35] Fix | Delete
[36] Fix | Delete
__all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
[37] Fix | Delete
"urlsplit", "urlunsplit", "parse_qs", "parse_qsl"]
[38] Fix | Delete
[39] Fix | Delete
# A classification of schemes ('' means apply by default)
[40] Fix | Delete
uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'imap',
[41] Fix | Delete
'wais', 'file', 'https', 'shttp', 'mms',
[42] Fix | Delete
'prospero', 'rtsp', 'rtspu', '', 'sftp',
[43] Fix | Delete
'svn', 'svn+ssh']
[44] Fix | Delete
uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet',
[45] Fix | Delete
'imap', 'wais', 'file', 'mms', 'https', 'shttp',
[46] Fix | Delete
'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '',
[47] Fix | Delete
'svn', 'svn+ssh', 'sftp','nfs','git', 'git+ssh']
[48] Fix | Delete
uses_params = ['ftp', 'hdl', 'prospero', 'http', 'imap',
[49] Fix | Delete
'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips',
[50] Fix | Delete
'mms', '', 'sftp', 'tel']
[51] Fix | Delete
[52] Fix | Delete
# These are not actually used anymore, but should stay for backwards
[53] Fix | Delete
# compatibility. (They are undocumented, but have a public-looking name.)
[54] Fix | Delete
non_hierarchical = ['gopher', 'hdl', 'mailto', 'news',
[55] Fix | Delete
'telnet', 'wais', 'imap', 'snews', 'sip', 'sips']
[56] Fix | Delete
uses_query = ['http', 'wais', 'imap', 'https', 'shttp', 'mms',
[57] Fix | Delete
'gopher', 'rtsp', 'rtspu', 'sip', 'sips', '']
[58] Fix | Delete
uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news',
[59] Fix | Delete
'nntp', 'wais', 'https', 'shttp', 'snews',
[60] Fix | Delete
'file', 'prospero', '']
[61] Fix | Delete
[62] Fix | Delete
# Characters valid in scheme names
[63] Fix | Delete
scheme_chars = ('abcdefghijklmnopqrstuvwxyz'
[64] Fix | Delete
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
[65] Fix | Delete
'0123456789'
[66] Fix | Delete
'+-.')
[67] Fix | Delete
[68] Fix | Delete
# Leading and trailing C0 control and space to be stripped per WHATWG spec.
[69] Fix | Delete
# == "".join([chr(i) for i in range(0, 0x20 + 1)])
[70] Fix | Delete
_WHATWG_C0_CONTROL_OR_SPACE = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f '
[71] Fix | Delete
[72] Fix | Delete
# Unsafe bytes to be removed per WHATWG spec
[73] Fix | Delete
_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
[74] Fix | Delete
[75] Fix | Delete
MAX_CACHE_SIZE = 20
[76] Fix | Delete
_parse_cache = {}
[77] Fix | Delete
[78] Fix | Delete
def clear_cache():
[79] Fix | Delete
"""Clear the parse cache."""
[80] Fix | Delete
_parse_cache.clear()
[81] Fix | Delete
[82] Fix | Delete
[83] Fix | Delete
class ResultMixin(object):
[84] Fix | Delete
"""Shared methods for the parsed result objects."""
[85] Fix | Delete
[86] Fix | Delete
@property
[87] Fix | Delete
def username(self):
[88] Fix | Delete
netloc = self.netloc
[89] Fix | Delete
if "@" in netloc:
[90] Fix | Delete
userinfo = netloc.rsplit("@", 1)[0]
[91] Fix | Delete
if ":" in userinfo:
[92] Fix | Delete
userinfo = userinfo.split(":", 1)[0]
[93] Fix | Delete
return userinfo
[94] Fix | Delete
return None
[95] Fix | Delete
[96] Fix | Delete
@property
[97] Fix | Delete
def password(self):
[98] Fix | Delete
netloc = self.netloc
[99] Fix | Delete
if "@" in netloc:
[100] Fix | Delete
userinfo = netloc.rsplit("@", 1)[0]
[101] Fix | Delete
if ":" in userinfo:
[102] Fix | Delete
return userinfo.split(":", 1)[1]
[103] Fix | Delete
return None
[104] Fix | Delete
[105] Fix | Delete
@property
[106] Fix | Delete
def hostname(self):
[107] Fix | Delete
netloc = self.netloc.split('@')[-1]
[108] Fix | Delete
if '[' in netloc and ']' in netloc:
[109] Fix | Delete
return netloc.split(']')[0][1:].lower()
[110] Fix | Delete
elif ':' in netloc:
[111] Fix | Delete
return netloc.split(':')[0].lower()
[112] Fix | Delete
elif netloc == '':
[113] Fix | Delete
return None
[114] Fix | Delete
else:
[115] Fix | Delete
return netloc.lower()
[116] Fix | Delete
[117] Fix | Delete
@property
[118] Fix | Delete
def port(self):
[119] Fix | Delete
netloc = self.netloc.split('@')[-1].split(']')[-1]
[120] Fix | Delete
if ':' in netloc:
[121] Fix | Delete
port = netloc.split(':')[1]
[122] Fix | Delete
if port:
[123] Fix | Delete
port = int(port, 10)
[124] Fix | Delete
# verify legal port
[125] Fix | Delete
if (0 <= port <= 65535):
[126] Fix | Delete
return port
[127] Fix | Delete
return None
[128] Fix | Delete
[129] Fix | Delete
from collections import namedtuple
[130] Fix | Delete
[131] Fix | Delete
class SplitResult(namedtuple('SplitResult', 'scheme netloc path query fragment'), ResultMixin):
[132] Fix | Delete
[133] Fix | Delete
__slots__ = ()
[134] Fix | Delete
[135] Fix | Delete
def geturl(self):
[136] Fix | Delete
return urlunsplit(self)
[137] Fix | Delete
[138] Fix | Delete
[139] Fix | Delete
class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin):
[140] Fix | Delete
[141] Fix | Delete
__slots__ = ()
[142] Fix | Delete
[143] Fix | Delete
def geturl(self):
[144] Fix | Delete
return urlunparse(self)
[145] Fix | Delete
[146] Fix | Delete
[147] Fix | Delete
def urlparse(url, scheme='', allow_fragments=True):
[148] Fix | Delete
"""Parse a URL into 6 components:
[149] Fix | Delete
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
[150] Fix | Delete
Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
[151] Fix | Delete
Note that we don't break the components up in smaller bits
[152] Fix | Delete
(e.g. netloc is a single string) and we don't expand % escapes."""
[153] Fix | Delete
tuple = urlsplit(url, scheme, allow_fragments)
[154] Fix | Delete
scheme, netloc, url, query, fragment = tuple
[155] Fix | Delete
if scheme in uses_params and ';' in url:
[156] Fix | Delete
url, params = _splitparams(url)
[157] Fix | Delete
else:
[158] Fix | Delete
params = ''
[159] Fix | Delete
return ParseResult(scheme, netloc, url, params, query, fragment)
[160] Fix | Delete
[161] Fix | Delete
def _splitparams(url):
[162] Fix | Delete
if '/' in url:
[163] Fix | Delete
i = url.find(';', url.rfind('/'))
[164] Fix | Delete
if i < 0:
[165] Fix | Delete
return url, ''
[166] Fix | Delete
else:
[167] Fix | Delete
i = url.find(';')
[168] Fix | Delete
return url[:i], url[i+1:]
[169] Fix | Delete
[170] Fix | Delete
def _splitnetloc(url, start=0):
[171] Fix | Delete
delim = len(url) # position of end of domain part of url, default is end
[172] Fix | Delete
for c in '/?#': # look for delimiters; the order is NOT important
[173] Fix | Delete
wdelim = url.find(c, start) # find first of this delim
[174] Fix | Delete
if wdelim >= 0: # if found
[175] Fix | Delete
delim = min(delim, wdelim) # use earliest delim position
[176] Fix | Delete
return url[start:delim], url[delim:] # return (domain, rest)
[177] Fix | Delete
[178] Fix | Delete
def _checknetloc(netloc):
[179] Fix | Delete
if not netloc or not isinstance(netloc, unicode):
[180] Fix | Delete
return
[181] Fix | Delete
# looking for characters like \u2100 that expand to 'a/c'
[182] Fix | Delete
# IDNA uses NFKC equivalence, so normalize for this check
[183] Fix | Delete
import unicodedata
[184] Fix | Delete
n = netloc.replace(u'@', u'') # ignore characters already included
[185] Fix | Delete
n = n.replace(u':', u'') # but not the surrounding text
[186] Fix | Delete
n = n.replace(u'#', u'')
[187] Fix | Delete
n = n.replace(u'?', u'')
[188] Fix | Delete
netloc2 = unicodedata.normalize('NFKC', n)
[189] Fix | Delete
if n == netloc2:
[190] Fix | Delete
return
[191] Fix | Delete
for c in '/?#@:':
[192] Fix | Delete
if c in netloc2:
[193] Fix | Delete
raise ValueError("netloc %r contains invalid characters "
[194] Fix | Delete
"under NFKC normalization"
[195] Fix | Delete
% netloc)
[196] Fix | Delete
[197] Fix | Delete
def _remove_unsafe_bytes_from_url(url):
[198] Fix | Delete
for b in _UNSAFE_URL_BYTES_TO_REMOVE:
[199] Fix | Delete
url = url.replace(b, "")
[200] Fix | Delete
return url
[201] Fix | Delete
[202] Fix | Delete
def urlsplit(url, scheme='', allow_fragments=True):
[203] Fix | Delete
"""Parse a URL into 5 components:
[204] Fix | Delete
<scheme>://<netloc>/<path>?<query>#<fragment>
[205] Fix | Delete
Return a 5-tuple: (scheme, netloc, path, query, fragment).
[206] Fix | Delete
Note that we don't break the components up in smaller bits
[207] Fix | Delete
(e.g. netloc is a single string) and we don't expand % escapes."""
[208] Fix | Delete
url = _remove_unsafe_bytes_from_url(url)
[209] Fix | Delete
scheme = _remove_unsafe_bytes_from_url(scheme)
[210] Fix | Delete
url = url.lstrip(_WHATWG_C0_CONTROL_OR_SPACE)
[211] Fix | Delete
scheme = scheme.strip(_WHATWG_C0_CONTROL_OR_SPACE)
[212] Fix | Delete
allow_fragments = bool(allow_fragments)
[213] Fix | Delete
key = url, scheme, allow_fragments, type(url), type(scheme)
[214] Fix | Delete
cached = _parse_cache.get(key, None)
[215] Fix | Delete
if cached:
[216] Fix | Delete
return cached
[217] Fix | Delete
if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth
[218] Fix | Delete
clear_cache()
[219] Fix | Delete
netloc = query = fragment = ''
[220] Fix | Delete
i = url.find(':')
[221] Fix | Delete
if i > 0:
[222] Fix | Delete
if url[:i] == 'http': # optimize the common case
[223] Fix | Delete
scheme = url[:i].lower()
[224] Fix | Delete
url = url[i+1:]
[225] Fix | Delete
if url[:2] == '//':
[226] Fix | Delete
netloc, url = _splitnetloc(url, 2)
[227] Fix | Delete
if (('[' in netloc and ']' not in netloc) or
[228] Fix | Delete
(']' in netloc and '[' not in netloc)):
[229] Fix | Delete
raise ValueError("Invalid IPv6 URL")
[230] Fix | Delete
if allow_fragments and '#' in url:
[231] Fix | Delete
url, fragment = url.split('#', 1)
[232] Fix | Delete
if '?' in url:
[233] Fix | Delete
url, query = url.split('?', 1)
[234] Fix | Delete
_checknetloc(netloc)
[235] Fix | Delete
v = SplitResult(scheme, netloc, url, query, fragment)
[236] Fix | Delete
_parse_cache[key] = v
[237] Fix | Delete
return v
[238] Fix | Delete
for c in url[:i]:
[239] Fix | Delete
if c not in scheme_chars:
[240] Fix | Delete
break
[241] Fix | Delete
else:
[242] Fix | Delete
# make sure "url" is not actually a port number (in which case
[243] Fix | Delete
# "scheme" is really part of the path)
[244] Fix | Delete
rest = url[i+1:]
[245] Fix | Delete
if not rest or any(c not in '0123456789' for c in rest):
[246] Fix | Delete
# not a port number
[247] Fix | Delete
scheme, url = url[:i].lower(), rest
[248] Fix | Delete
[249] Fix | Delete
if url[:2] == '//':
[250] Fix | Delete
netloc, url = _splitnetloc(url, 2)
[251] Fix | Delete
if (('[' in netloc and ']' not in netloc) or
[252] Fix | Delete
(']' in netloc and '[' not in netloc)):
[253] Fix | Delete
raise ValueError("Invalid IPv6 URL")
[254] Fix | Delete
if allow_fragments and '#' in url:
[255] Fix | Delete
url, fragment = url.split('#', 1)
[256] Fix | Delete
if '?' in url:
[257] Fix | Delete
url, query = url.split('?', 1)
[258] Fix | Delete
_checknetloc(netloc)
[259] Fix | Delete
v = SplitResult(scheme, netloc, url, query, fragment)
[260] Fix | Delete
_parse_cache[key] = v
[261] Fix | Delete
return v
[262] Fix | Delete
[263] Fix | Delete
def urlunparse(data):
[264] Fix | Delete
"""Put a parsed URL back together again. This may result in a
[265] Fix | Delete
slightly different, but equivalent URL, if the URL that was parsed
[266] Fix | Delete
originally had redundant delimiters, e.g. a ? with an empty query
[267] Fix | Delete
(the draft states that these are equivalent)."""
[268] Fix | Delete
scheme, netloc, url, params, query, fragment = data
[269] Fix | Delete
if params:
[270] Fix | Delete
url = "%s;%s" % (url, params)
[271] Fix | Delete
return urlunsplit((scheme, netloc, url, query, fragment))
[272] Fix | Delete
[273] Fix | Delete
def urlunsplit(data):
[274] Fix | Delete
"""Combine the elements of a tuple as returned by urlsplit() into a
[275] Fix | Delete
complete URL as a string. The data argument can be any five-item iterable.
[276] Fix | Delete
This may result in a slightly different, but equivalent URL, if the URL that
[277] Fix | Delete
was parsed originally had unnecessary delimiters (for example, a ? with an
[278] Fix | Delete
empty query; the RFC states that these are equivalent)."""
[279] Fix | Delete
scheme, netloc, url, query, fragment = data
[280] Fix | Delete
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
[281] Fix | Delete
if url and url[:1] != '/': url = '/' + url
[282] Fix | Delete
url = '//' + (netloc or '') + url
[283] Fix | Delete
if scheme:
[284] Fix | Delete
url = scheme + ':' + url
[285] Fix | Delete
if query:
[286] Fix | Delete
url = url + '?' + query
[287] Fix | Delete
if fragment:
[288] Fix | Delete
url = url + '#' + fragment
[289] Fix | Delete
return url
[290] Fix | Delete
[291] Fix | Delete
def urljoin(base, url, allow_fragments=True):
[292] Fix | Delete
"""Join a base URL and a possibly relative URL to form an absolute
[293] Fix | Delete
interpretation of the latter."""
[294] Fix | Delete
if not base:
[295] Fix | Delete
return url
[296] Fix | Delete
if not url:
[297] Fix | Delete
return base
[298] Fix | Delete
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
[299] Fix | Delete
urlparse(base, '', allow_fragments)
[300] Fix | Delete
scheme, netloc, path, params, query, fragment = \
[301] Fix | Delete
urlparse(url, bscheme, allow_fragments)
[302] Fix | Delete
if scheme != bscheme or scheme not in uses_relative:
[303] Fix | Delete
return url
[304] Fix | Delete
if scheme in uses_netloc:
[305] Fix | Delete
if netloc:
[306] Fix | Delete
return urlunparse((scheme, netloc, path,
[307] Fix | Delete
params, query, fragment))
[308] Fix | Delete
netloc = bnetloc
[309] Fix | Delete
if path[:1] == '/':
[310] Fix | Delete
return urlunparse((scheme, netloc, path,
[311] Fix | Delete
params, query, fragment))
[312] Fix | Delete
if not path and not params:
[313] Fix | Delete
path = bpath
[314] Fix | Delete
params = bparams
[315] Fix | Delete
if not query:
[316] Fix | Delete
query = bquery
[317] Fix | Delete
return urlunparse((scheme, netloc, path,
[318] Fix | Delete
params, query, fragment))
[319] Fix | Delete
segments = bpath.split('/')[:-1] + path.split('/')
[320] Fix | Delete
# XXX The stuff below is bogus in various ways...
[321] Fix | Delete
if segments[-1] == '.':
[322] Fix | Delete
segments[-1] = ''
[323] Fix | Delete
while '.' in segments:
[324] Fix | Delete
segments.remove('.')
[325] Fix | Delete
while 1:
[326] Fix | Delete
i = 1
[327] Fix | Delete
n = len(segments) - 1
[328] Fix | Delete
while i < n:
[329] Fix | Delete
if (segments[i] == '..'
[330] Fix | Delete
and segments[i-1] not in ('', '..')):
[331] Fix | Delete
del segments[i-1:i+1]
[332] Fix | Delete
break
[333] Fix | Delete
i = i+1
[334] Fix | Delete
else:
[335] Fix | Delete
break
[336] Fix | Delete
if segments == ['', '..']:
[337] Fix | Delete
segments[-1] = ''
[338] Fix | Delete
elif len(segments) >= 2 and segments[-1] == '..':
[339] Fix | Delete
segments[-2:] = ['']
[340] Fix | Delete
return urlunparse((scheme, netloc, '/'.join(segments),
[341] Fix | Delete
params, query, fragment))
[342] Fix | Delete
[343] Fix | Delete
def urldefrag(url):
[344] Fix | Delete
"""Removes any existing fragment from URL.
[345] Fix | Delete
[346] Fix | Delete
Returns a tuple of the defragmented URL and the fragment. If
[347] Fix | Delete
the URL contained no fragments, the second element is the
[348] Fix | Delete
empty string.
[349] Fix | Delete
"""
[350] Fix | Delete
if '#' in url:
[351] Fix | Delete
s, n, p, a, q, frag = urlparse(url)
[352] Fix | Delete
defrag = urlunparse((s, n, p, a, q, ''))
[353] Fix | Delete
return defrag, frag
[354] Fix | Delete
else:
[355] Fix | Delete
return url, ''
[356] Fix | Delete
[357] Fix | Delete
try:
[358] Fix | Delete
unicode
[359] Fix | Delete
except NameError:
[360] Fix | Delete
def _is_unicode(x):
[361] Fix | Delete
return 0
[362] Fix | Delete
else:
[363] Fix | Delete
def _is_unicode(x):
[364] Fix | Delete
return isinstance(x, unicode)
[365] Fix | Delete
[366] Fix | Delete
# unquote method for parse_qs and parse_qsl
[367] Fix | Delete
# Cannot use directly from urllib as it would create a circular reference
[368] Fix | Delete
# because urllib uses urlparse methods (urljoin). If you update this function,
[369] Fix | Delete
# update it also in urllib. This code duplication does not existin in Python3.
[370] Fix | Delete
[371] Fix | Delete
_hexdig = '0123456789ABCDEFabcdef'
[372] Fix | Delete
_hextochr = dict((a+b, chr(int(a+b,16)))
[373] Fix | Delete
for a in _hexdig for b in _hexdig)
[374] Fix | Delete
_asciire = re.compile('([\x00-\x7f]+)')
[375] Fix | Delete
[376] Fix | Delete
def unquote(s):
[377] Fix | Delete
"""unquote('abc%20def') -> 'abc def'."""
[378] Fix | Delete
if _is_unicode(s):
[379] Fix | Delete
if '%' not in s:
[380] Fix | Delete
return s
[381] Fix | Delete
bits = _asciire.split(s)
[382] Fix | Delete
res = [bits[0]]
[383] Fix | Delete
append = res.append
[384] Fix | Delete
for i in range(1, len(bits), 2):
[385] Fix | Delete
append(unquote(str(bits[i])).decode('latin1'))
[386] Fix | Delete
append(bits[i + 1])
[387] Fix | Delete
return ''.join(res)
[388] Fix | Delete
[389] Fix | Delete
bits = s.split('%')
[390] Fix | Delete
# fastpath
[391] Fix | Delete
if len(bits) == 1:
[392] Fix | Delete
return s
[393] Fix | Delete
res = [bits[0]]
[394] Fix | Delete
append = res.append
[395] Fix | Delete
for item in bits[1:]:
[396] Fix | Delete
try:
[397] Fix | Delete
append(_hextochr[item[:2]])
[398] Fix | Delete
append(item[2:])
[399] Fix | Delete
except KeyError:
[400] Fix | Delete
append('%')
[401] Fix | Delete
append(item)
[402] Fix | Delete
return ''.join(res)
[403] Fix | Delete
[404] Fix | Delete
def parse_qs(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None,
[405] Fix | Delete
separator=None):
[406] Fix | Delete
"""Parse a query given as a string argument.
[407] Fix | Delete
[408] Fix | Delete
Arguments:
[409] Fix | Delete
[410] Fix | Delete
qs: percent-encoded query string to be parsed
[411] Fix | Delete
[412] Fix | Delete
keep_blank_values: flag indicating whether blank values in
[413] Fix | Delete
percent-encoded queries should be treated as blank strings.
[414] Fix | Delete
A true value indicates that blanks should be retained as
[415] Fix | Delete
blank strings. The default false value indicates that
[416] Fix | Delete
blank values are to be ignored and treated as if they were
[417] Fix | Delete
not included.
[418] Fix | Delete
[419] Fix | Delete
strict_parsing: flag indicating what to do with parsing errors.
[420] Fix | Delete
If false (the default), errors are silently ignored.
[421] Fix | Delete
If true, errors raise a ValueError exception.
[422] Fix | Delete
[423] Fix | Delete
max_num_fields: int. If set, then throws a ValueError if there
[424] Fix | Delete
are more than n fields read by parse_qsl().
[425] Fix | Delete
"""
[426] Fix | Delete
dict = {}
[427] Fix | Delete
for name, value in parse_qsl(qs, keep_blank_values, strict_parsing,
[428] Fix | Delete
max_num_fields, separator):
[429] Fix | Delete
if name in dict:
[430] Fix | Delete
dict[name].append(value)
[431] Fix | Delete
else:
[432] Fix | Delete
dict[name] = [value]
[433] Fix | Delete
return dict
[434] Fix | Delete
[435] Fix | Delete
class _QueryStringSeparatorWarning(RuntimeWarning):
[436] Fix | Delete
"""Warning for using default `separator` in parse_qs or parse_qsl"""
[437] Fix | Delete
[438] Fix | Delete
# The default "separator" for parse_qsl can be specified in a config file.
[439] Fix | Delete
# It's cached after first read.
[440] Fix | Delete
_QS_SEPARATOR_CONFIG_FILENAME = '/etc/python/urllib.cfg'
[441] Fix | Delete
_default_qs_separator = None
[442] Fix | Delete
[443] Fix | Delete
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None,
[444] Fix | Delete
separator=None):
[445] Fix | Delete
"""Parse a query given as a string argument.
[446] Fix | Delete
[447] Fix | Delete
Arguments:
[448] Fix | Delete
[449] Fix | Delete
qs: percent-encoded query string to be parsed
[450] Fix | Delete
[451] Fix | Delete
keep_blank_values: flag indicating whether blank values in
[452] Fix | Delete
percent-encoded queries should be treated as blank strings. A
[453] Fix | Delete
true value indicates that blanks should be retained as blank
[454] Fix | Delete
strings. The default false value indicates that blank values
[455] Fix | Delete
are to be ignored and treated as if they were not included.
[456] Fix | Delete
[457] Fix | Delete
strict_parsing: flag indicating what to do with parsing errors. If
[458] Fix | Delete
false (the default), errors are silently ignored. If true,
[459] Fix | Delete
errors raise a ValueError exception.
[460] Fix | Delete
[461] Fix | Delete
max_num_fields: int. If set, then throws a ValueError if there
[462] Fix | Delete
are more than n fields read by parse_qsl().
[463] Fix | Delete
[464] Fix | Delete
Returns a list, as G-d intended.
[465] Fix | Delete
"""
[466] Fix | Delete
[467] Fix | Delete
if (not separator or (not isinstance(separator, (str, bytes)))) and separator is not None:
[468] Fix | Delete
raise ValueError("Separator must be of type string or bytes.")
[469] Fix | Delete
[470] Fix | Delete
# Used when both "&" and ";" act as separators. (Need a non-string value.)
[471] Fix | Delete
_legacy = object()
[472] Fix | Delete
[473] Fix | Delete
if separator is None:
[474] Fix | Delete
global _default_qs_separator
[475] Fix | Delete
separator = _default_qs_separator
[476] Fix | Delete
envvar_name = 'PYTHON_URLLIB_QS_SEPARATOR'
[477] Fix | Delete
if separator is None:
[478] Fix | Delete
# Set default separator from environment variable
[479] Fix | Delete
separator = os.environ.get(envvar_name)
[480] Fix | Delete
config_source = 'environment variable'
[481] Fix | Delete
if separator is None:
[482] Fix | Delete
# Set default separator from the configuration file
[483] Fix | Delete
try:
[484] Fix | Delete
file = open(_QS_SEPARATOR_CONFIG_FILENAME)
[485] Fix | Delete
except EnvironmentError:
[486] Fix | Delete
pass
[487] Fix | Delete
else:
[488] Fix | Delete
with file:
[489] Fix | Delete
import ConfigParser
[490] Fix | Delete
config = ConfigParser.ConfigParser()
[491] Fix | Delete
config.readfp(file)
[492] Fix | Delete
separator = config.get('parse_qs', envvar_name)
[493] Fix | Delete
_default_qs_separator = separator
[494] Fix | Delete
config_source = _QS_SEPARATOR_CONFIG_FILENAME
[495] Fix | Delete
if separator is None:
[496] Fix | Delete
# The default is '&', but warn if not specified explicitly
[497] Fix | Delete
if ';' in qs:
[498] Fix | Delete
from warnings import warn
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function