Source code for boltons.urlutils

# -*- coding: utf-8 -*-""":mod:`urlutils` is a module dedicated to one of software's mostversatile, well-aged, and beloved data structures: the URL, also knownas the `Uniform Resource Locator`_.Among other things, this module is a full reimplementation of URLs,without any reliance on the :mod:`urlparse` or :mod:`urllib` standardlibrary modules. The centerpiece and top-level interface of urlutilsis the :class:`URL` type. Also featured is the :func:`find_all_links`convenience function. Some low-level functions and constants are alsobelow.The implementations in this module are based heavily on `RFC 3986`_ and`RFC 3987`_, and incorporates details from several other RFCs and `W3Cdocuments`_... _Uniform Resource Locator: https://en.wikipedia.org/wiki/Uniform_Resource_Locator.. _RFC 3986: https://tools.ietf.org/html/rfc3986.. _RFC 3987: https://tools.ietf.org/html/rfc3987.. _W3C documents: https://www.w3.org/TR/uri-clarification/"""importreimportsocketimportstringfromunicodedataimportnormalizeunicode=type(u'')try:unichrexceptNameError:unichr=chr# The unreserved URI characters (per RFC 3986 Section 2.3)_UNRESERVED_CHARS=frozenset('~-._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ''abcdefghijklmnopqrstuvwxyz')# URL parsing regex (based on RFC 3986 Appendix B, with modifications)_URL_RE=re.compile(r'^((?P<scheme>[^:/?#]+):)?'r'((?P<_netloc_sep>//)(?P<authority>[^/?#]*))?'r'(?P<path>[^?#]*)'r'(\?(?P<query>[^#]*))?'r'(#(?P<fragment>.*))?')_HEX_CHAR_MAP=dict([((a+b).encode('ascii'),unichr(int(a+b,16)).encode('charmap'))forainstring.hexdigitsforbinstring.hexdigits])_ASCII_RE=re.compile('([\x00-\x7f]+)')# This port list painstakingly curated by hand searching through# https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml# and# https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtmlSCHEME_PORT_MAP={'acap':674,'afp':548,'dict':2628,'dns':53,'file':None,'ftp':21,'git':9418,'gopher':70,'http':80,'https':443,'imap':143,'ipp':631,'ipps':631,'irc':194,'ircs':6697,'ldap':389,'ldaps':636,'mms':1755,'msrp':2855,'msrps':None,'mtqp':1038,'nfs':111,'nntp':119,'nntps':563,'pop':110,'prospero':1525,'redis':6379,'rsync':873,'rtsp':554,'rtsps':322,'rtspu':5005,'sftp':22,'smb':445,'snmp':161,'ssh':22,'steam':None,'svn':3690,'telnet':23,'ventrilo':3784,'vnc':5900,'wais':210,'ws':80,'wss':443,'xmpp':None}# This list of schemes that don't use authorities is also from the link above.NO_NETLOC_SCHEMES=set(['urn','about','bitcoin','blob','data','geo','magnet','mailto','news','pkcs11','sip','sips','tel'])# As of Mar 11, 2017, there were 44 netloc schemes, and 13 non-netloc# RFC 3986 section 2.2, Reserved Characters_GEN_DELIMS=frozenset(u':/?#[]@')_SUB_DELIMS=frozenset(u"!$&'()*+,;=")_ALL_DELIMS=_GEN_DELIMS|_SUB_DELIMS_USERINFO_SAFE=_UNRESERVED_CHARS|_SUB_DELIMS_USERINFO_DELIMS=_ALL_DELIMS-_USERINFO_SAFE_PATH_SAFE=_UNRESERVED_CHARS|_SUB_DELIMS|set(u':@')_PATH_DELIMS=_ALL_DELIMS-_PATH_SAFE_FRAGMENT_SAFE=_UNRESERVED_CHARS|_PATH_SAFE|set(u'/?')_FRAGMENT_DELIMS=_ALL_DELIMS-_FRAGMENT_SAFE_QUERY_SAFE=_UNRESERVED_CHARS|_FRAGMENT_SAFE-set(u'&=+')_QUERY_DELIMS=_ALL_DELIMS-_QUERY_SAFEclassURLParseError(ValueError):"""Exception inheriting from :exc:`ValueError`, raised when failing to parse a URL. Mostly raised on invalid ports and IPv6 addresses. """passDEFAULT_ENCODING='utf8'defto_unicode(obj):try:returnunicode(obj)exceptUnicodeDecodeError:returnunicode(obj,encoding=DEFAULT_ENCODING)# regex from gruber via tornado# doesn't support ipv6# doesn't support mailto (netloc-less schemes)_FIND_ALL_URL_RE=re.compile(to_unicode(r"""\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()<>]|&amp;|&quot;)*(?:[^!"#$%'()*+,.:;<=>?@\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&amp;|&quot;)*\)))+)"""))

[docs]deffind_all_links(text,with_text=False,default_scheme='https',schemes=()):"""This function uses heuristics to searches plain text for strings that look like URLs, returning a :class:`list` of :class:`URL` objects. It supports limiting the accepted schemes, and returning interleaved text as well. >>> find_all_links('Visit https://boltons.rtfd.org!') [URL(u'https://boltons.rtfd.org')] >>> find_all_links('Visit https://boltons.rtfd.org!', with_text=True) [u'Visit ', URL(u'https://boltons.rtfd.org'), u'!'] Args: text (str): The text to search. with_text (bool): Whether or not to interleave plaintext blocks with the returned URL objects. Having all tokens can be useful for transforming the text, e.g., replacing links with HTML equivalents. Defaults to ``False``. default_scheme (str): Many URLs are written without the scheme component. This function can match a reasonable subset of those, provided *default_scheme* is set to a string. Set to ``False`` to disable matching scheme-less URLs. Defaults to ``'https'``. schemes (list): A list of strings that a URL's scheme must match in order to be included in the results. Defaults to empty, which matches all schemes. .. note:: Currently this function does not support finding IPv6 addresses or URLs with netloc-less schemes, like mailto. """text=to_unicode(text)prev_end,start,end=0,None,Noneret=[]_add=ret.appenddef_add_text(t):ifretandisinstance(ret[-1],unicode):ret[-1]+=telse:_add(t)formatchin_FIND_ALL_URL_RE.finditer(text):start,end=match.start(1),match.end(1)ifprev_end<startandwith_text:_add(text[prev_end:start])prev_end=endtry:cur_url_text=match.group(0)cur_url=URL(cur_url_text)ifnotcur_url.scheme:ifdefault_scheme:cur_url=URL(default_scheme+'://'+cur_url_text)else:_add_text(text[start:end])continueifschemesandcur_url.schemenotinschemes:_add_text(text[start:end])else:_add(cur_url)exceptURLParseError:# currently this should only be hit with broken port# strings. the regex above doesn't support ipv6 addressesifwith_text:_add_text(text[start:end])ifwith_text:tail=text[prev_end:]iftail:_add_text(tail)returnret

def_make_quote_map(safe_chars):ret={}# v is included in the dict for py3 mostly, because bytestrings# are iterables of ints, of course!fori,vinzip(range(256),range(256)):c=chr(v)ifcinsafe_chars:ret[c]=ret[v]=celse:ret[c]=ret[v]='%{0:02X}'.format(i)returnret_USERINFO_PART_QUOTE_MAP=_make_quote_map(_USERINFO_SAFE)_PATH_PART_QUOTE_MAP=_make_quote_map(_PATH_SAFE)_QUERY_PART_QUOTE_MAP=_make_quote_map(_QUERY_SAFE)_FRAGMENT_QUOTE_MAP=_make_quote_map(_FRAGMENT_SAFE)

[docs]defquote_path_part(text,full_quote=True):""" Percent-encode a single segment of a URL path. """iffull_quote:bytestr=normalize('NFC',to_unicode(text)).encode('utf8')returnu''.join([_PATH_PART_QUOTE_MAP[b]forbinbytestr])returnu''.join([_PATH_PART_QUOTE_MAP[t]iftin_PATH_DELIMSelsetfortintext])

[docs]defquote_query_part(text,full_quote=True):""" Percent-encode a single query string key or value. """iffull_quote:bytestr=normalize('NFC',to_unicode(text)).encode('utf8')returnu''.join([_QUERY_PART_QUOTE_MAP[b]forbinbytestr])returnu''.join([_QUERY_PART_QUOTE_MAP[t]iftin_QUERY_DELIMSelsetfortintext])

[docs]defquote_fragment_part(text,full_quote=True):"""Quote the fragment part of the URL. Fragments don't have subdelimiters, so the whole URL fragment can be passed. """iffull_quote:bytestr=normalize('NFC',to_unicode(text)).encode('utf8')returnu''.join([_FRAGMENT_QUOTE_MAP[b]forbinbytestr])returnu''.join([_FRAGMENT_QUOTE_MAP[t]iftin_FRAGMENT_DELIMSelsetfortintext])

[docs]defquote_userinfo_part(text,full_quote=True):"""Quote special characters in either the username or password section of the URL. Note that userinfo in URLs is considered deprecated in many circles (especially browsers), and support for percent-encoded userinfo can be spotty. """iffull_quote:bytestr=normalize('NFC',to_unicode(text)).encode('utf8')returnu''.join([_USERINFO_PART_QUOTE_MAP[b]forbinbytestr])returnu''.join([_USERINFO_PART_QUOTE_MAP[t]iftin_USERINFO_DELIMSelsetfortintext])

[docs]defunquote(string,encoding='utf-8',errors='replace'):"""Percent-decode a string, by replacing %xx escapes with their single-character equivalent. The optional *encoding* and *errors* parameters specify how to decode percent-encoded sequences into Unicode characters, as accepted by the :meth:`bytes.decode()` method. By default, percent-encoded sequences are decoded with UTF-8, and invalid sequences are replaced by a placeholder character. >>> unquote(u'abc%20def') u'abc def' """if'%'notinstring:string.splitreturnstringifencodingisNone:encoding='utf-8'iferrorsisNone:errors='replace'bits=_ASCII_RE.split(string)res=[bits[0]]append=res.appendforiinrange(1,len(bits),2):append(unquote_to_bytes(bits[i]).decode(encoding,errors))append(bits[i+1])return''.join(res)

defunquote_to_bytes(string):"""unquote_to_bytes('abc%20def') -> b'abc def'."""# Note: strings are encoded as UTF-8. This is only an issue if it contains# unescaped non-ASCII characters, which URIs should not.ifnotstring:# Is it a string-like object?string.splitreturnb''ifisinstance(string,unicode):string=string.encode('utf-8')bits=string.split(b'%')iflen(bits)==1:returnstring# import pdb;pdb.set_trace()res=[bits[0]]append=res.appendforiteminbits[1:]:try:append(_HEX_CHAR_MAP[item[:2]])append(item[2:])exceptKeyError:append(b'%')append(item)returnb''.join(res)

[docs]defregister_scheme(text,uses_netloc=None,default_port=None):"""Registers new scheme information, resulting in correct port and slash behavior from the URL object. There are dozens of standard schemes preregistered, so this function is mostly meant for proprietary internal customizations or stopgaps on missing standards information. If a scheme seems to be missing, please `file an issue`_! Args: text (str): Text representing the scheme. (the 'http' in 'http://hatnote.com') uses_netloc (bool): Does the scheme support specifying a network host? For instance, "http" does, "mailto" does not. default_port (int): The default port, if any, for netloc-using schemes. .. _file an issue: https://github.com/mahmoud/boltons/issues """text=text.lower()ifdefault_portisnotNone:try:default_port=int(default_port)exceptValueError:raiseValueError('default_port expected integer or None, not %r'%(default_port,))ifuses_netlocisTrue:SCHEME_PORT_MAP[text]=default_portelifuses_netlocisFalse:ifdefault_portisnotNone:raiseValueError('unexpected default port while specifying'' non-netloc scheme: %r'%default_port)NO_NETLOC_SCHEMES.add(text)elifuses_netlocisnotNone:raiseValueError('uses_netloc expected True, False, or None')return

[docs]defresolve_path_parts(path_parts):"""Normalize the URL path by resolving segments of '.' and '..', resulting in a dot-free path. See RFC 3986 section 5.2.4, Remove Dot Segments. """# TODO: what to do with multiple slashesret=[]forpartinpath_parts:ifpart==u'.':passelifpart==u'..':ifretand(len(ret)>1orret[0]):# prevent unrootingret.pop()else:ret.append(part)iflist(path_parts[-1:])in([u'.'],[u'..']):ret.append(u'')returnret

classcachedproperty(object):"""The ``cachedproperty`` is used similar to :class:`property`, except that the wrapped method is only called once. This is commonly used to implement lazy attributes. After the property has been accessed, the value is stored on the instance itself, using the same name as the cachedproperty. This allows the cache to be cleared with :func:`delattr`, or through manipulating the object's ``__dict__``. """def__init__(self,func):self.__doc__=getattr(func,'__doc__')self.func=funcdef__get__(self,obj,objtype=None):ifobjisNone:returnselfvalue=obj.__dict__[self.func.__name__]=self.func(obj)returnvaluedef__repr__(self):cn=self.__class__.__name__return'<%s func=%s>'%(cn,self.func)

[docs]classURL(object):"""The URL is one of the most ubiquitous data structures in the virtual and physical landscape. From blogs to billboards, URLs are so common, that it's easy to overlook their complexity and power. There are 8 parts of a URL, each with its own semantics and special characters: * :attr:`~URL.scheme` * :attr:`~URL.username` * :attr:`~URL.password` * :attr:`~URL.host` * :attr:`~URL.port` * :attr:`~URL.path` * :attr:`~URL.query_params` (query string parameters) * :attr:`~URL.fragment` Each is exposed as an attribute on the URL object. RFC 3986 offers this brief structural summary of the main URL components:: foo://user:pass@example.com:8042/over/there?name=ferret#nose \_/ \_______/ \_________/ \__/\_________/ \_________/ \__/ | | | | | | | scheme userinfo host port path query fragment And here's how that example can be manipulated with the URL type: >>> url = URL('foo://example.com:8042/over/there?name=ferret#nose') >>> print(url.host) example.com >>> print(url.get_authority()) example.com:8042 >>> print(url.qp['name']) # qp is a synonym for query_params ferret URL's approach to encoding is that inputs are decoded as much as possible, and data remains in this decoded state until re-encoded using the :meth:`~URL.to_text()` method. In this way, it's similar to Python's current approach of encouraging immediate decoding of bytes to text. Note that URL instances are mutable objects. If an immutable representation of the URL is desired, the string from :meth:`~URL.to_text()` may be used. For an immutable, but almost-as-featureful, URL object, check out the `hyperlink package`_. .. _hyperlink package: https://github.com/mahmoud/hyperlink """# public attributes (for comparison, see __eq__):_cmp_attrs=('scheme','uses_netloc','username','password','family','host','port','path','query_params','fragment')def__init__(self,url=''):# TODO: encoding param. The encoding that underlies the# percent-encoding is always utf8 for IRIs, but can be Latin-1# for other usage schemes.ud=DEFAULT_PARSED_URLifurl:ifisinstance(url,URL):url=url.to_text()# better way to copy URLs?elifisinstance(url,bytes):try:url=url.decode(DEFAULT_ENCODING)exceptUnicodeDecodeErrorasude:raiseURLParseError('expected text or %s-encoded bytes.'' try decoding the url bytes and'' passing the result. (got: %s)'%(DEFAULT_ENCODING,ude))ud=parse_url(url)_e=u''self.scheme=ud['scheme']or_eself._netloc_sep=ud['_netloc_sep']or_eself.username=(unquote(ud['username'])if'%'in(ud['username']or_e)elseud['username']or_e)self.password=(unquote(ud['password'])if'%'in(ud['password']or_e)elseud['password']or_e)self.family=ud['family']ifnotud['host']:self.host=_eelse:try:self.host=ud['host'].encode("ascii")exceptUnicodeEncodeError:self.host=ud['host']# already non-ascii textelse:self.host=self.host.decode("idna")self.port=ud['port']self.path_parts=tuple([unquote(p)if'%'inpelsepforpin(ud['path']or_e).split(u'/')])self._query=ud['query']or_eself.fragment=(unquote(ud['fragment'])if'%'in(ud['fragment']or_e)elseud['fragment']or_e)# TODO: possibly use None as marker for empty vs missingreturn

[docs]@classmethoddeffrom_parts(cls,scheme=None,host=None,path_parts=(),query_params=(),fragment=u'',port=None,username=None,password=None):"""Build a new URL from parts. Note that the respective arguments are not in the order they would appear in a URL: Args: scheme (str): The scheme of a URL, e.g., 'http' host (str): The host string, e.g., 'hatnote.com' path_parts (tuple): The individual text segments of the path, e.g., ('post', '123') query_params (dict): An OMD, dict, or list of (key, value) pairs representing the keys and values of the URL's query parameters. fragment (str): The fragment of the URL, e.g., 'anchor1' port (int): The integer port of URL, automatic defaults are available for registered schemes. username (str): The username for the userinfo part of the URL. password (str): The password for the userinfo part of the URL. Note that this method does relatively little validation. :meth:`URL.to_text()` should be used to check if any errors are produced while composing the final textual URL. """ret=cls()ret.scheme=schemeret.host=hostret.path_parts=tuple(path_parts)or(u'',)ret.query_params.update(query_params)ret.fragment=fragmentret.port=portret.username=usernameret.password=passwordreturnret

[docs]@cachedpropertydefquery_params(self):"""The parsed form of the query string of the URL, represented as a :class:`~dictutils.OrderedMultiDict`. Also available as the handy alias ``qp``. >>> url = URL('http://boltons.readthedocs.io/?utm_source=doctest&python=great') >>> url.qp.keys() [u'utm_source', u'python'] """returnQueryParamDict.from_text(self._query)

qp=query_params@propertydefpath(self):"The URL's path, in text form."returnu'/'.join([quote_path_part(p,full_quote=False)forpinself.path_parts])@path.setterdefpath(self,path_text):self.path_parts=tuple([unquote(p)if'%'inpelsepforpinto_unicode(path_text).split(u'/')])return@propertydefuses_netloc(self):"""Whether or not a URL uses :code:`:` or :code:`://` to separate the scheme from the rest of the URL depends on the scheme's own standard definition. There is no way to infer this behavior from other parts of the URL. A scheme either supports network locations or it does not. The URL type's approach to this is to check for explicitly registered schemes, with common schemes like HTTP preregistered. This is the same approach taken by :mod:`urlparse`. URL adds two additional heuristics if the scheme as a whole is not registered. First, it attempts to check the subpart of the scheme after the last ``+`` character. This adds intuitive behavior for schemes like ``git+ssh``. Second, if a URL with an unrecognized scheme is loaded, it will maintain the separator it sees. >>> print(URL('fakescheme://test.com').to_text()) fakescheme://test.com >>> print(URL('mockscheme:hello:world').to_text()) mockscheme:hello:world """default=self._netloc_sepifself.schemeinSCHEME_PORT_MAP:returnTrueifself.schemeinNO_NETLOC_SCHEMES:returnFalseifself.scheme.split('+')[-1]inSCHEME_PORT_MAP:returnTruereturndefault@propertydefdefault_port(self):"""Return the default port for the currently-set scheme. Returns ``None`` if the scheme is unrecognized. See :func:`register_scheme` above. If :attr:`~URL.port` matches this value, no port is emitted in the output of :meth:`~URL.to_text()`. Applies the same '+' heuristic detailed in :meth:`URL.uses_netloc`. """try:returnSCHEME_PORT_MAP[self.scheme]exceptKeyError:returnSCHEME_PORT_MAP.get(self.scheme.split('+')[-1])

[docs]defnormalize(self,with_case=True):"""Resolve any "." and ".." references in the path, as well as normalize scheme and host casing. To turn off case normalization, pass ``with_case=False``. More information can be found in `Section 6.2.2 of RFC 3986`_. .. _Section 6.2.2 of RFC 3986: https://tools.ietf.org/html/rfc3986#section-6.2.2 """self.path_parts=resolve_path_parts(self.path_parts)ifwith_case:self.scheme=self.scheme.lower()self.host=self.host.lower()return

[docs]defnavigate(self,dest):"""Factory method that returns a _new_ :class:`URL` based on a given destination, *dest*. Useful for navigating those relative links with ease. The newly created :class:`URL` is normalized before being returned. >>> url = URL('http://boltons.readthedocs.io') >>> url.navigate('en/latest/') URL(u'http://boltons.readthedocs.io/en/latest/') Args: dest (str): A string or URL object representing the destination More information can be found in `Section 5 of RFC 3986`_. .. _Section 5 of RFC 3986: https://tools.ietf.org/html/rfc3986#section-5 """orig_dest=Noneifnotisinstance(dest,URL):dest,orig_dest=URL(dest),destifdest.schemeanddest.host:# absolute URLs replace everything, but don't make an# extra copy if we don't have toreturnURL(dest)iforig_destisNoneelsedestquery_params=dest.query_paramsifdest.path:ifdest.path.startswith(u'/'):# absolute pathnew_path_parts=list(dest.path_parts)else:# relative pathnew_path_parts=self.path_parts[:-1]+dest.path_partselse:new_path_parts=list(self.path_parts)ifnotquery_params:query_params=self.query_paramsret=self.from_parts(scheme=dest.schemeorself.scheme,host=dest.hostorself.host,port=dest.portorself.port,path_parts=new_path_parts,query_params=query_params,fragment=dest.fragment,username=dest.usernameorself.username,password=dest.passwordorself.password)ret.normalize()returnret

[docs]defget_authority(self,full_quote=False,with_userinfo=False):"""Used by URL schemes that have a network location, :meth:`~URL.get_authority` combines :attr:`username`, :attr:`password`, :attr:`host`, and :attr:`port` into one string, the *authority*, that is used for connecting to a network-accessible resource. Used internally by :meth:`~URL.to_text()` and can be useful for labeling connections. >>> url = URL('ftp://user@ftp.debian.org:2121/debian/README') >>> print(url.get_authority()) ftp.debian.org:2121 >>> print(url.get_authority(with_userinfo=True)) user@ftp.debian.org:2121 Args: full_quote (bool): Whether or not to apply IDNA encoding. Defaults to ``False``. with_userinfo (bool): Whether or not to include username and password, technically part of the authority. Defaults to ``False``. """parts=[]_add=parts.appendifself.usernameandwith_userinfo:_add(quote_userinfo_part(self.username))ifself.password:_add(':')_add(quote_userinfo_part(self.password))_add('@')ifself.host:ifself.family==socket.AF_INET6:_add('[')_add(self.host)_add(']')eliffull_quote:_add(self.host.encode('idna').decode('ascii'))else:_add(self.host)# TODO: 0 port?ifself.portandself.port!=self.default_port:_add(':')_add(unicode(self.port))returnu''.join(parts)

[docs]defto_text(self,full_quote=False):"""Render a string representing the current state of the URL object. >>> url = URL('http://listen.hatnote.com') >>> url.fragment = 'en' >>> print(url.to_text()) http://listen.hatnote.com#en By setting the *full_quote* flag, the URL can either be fully quoted or minimally quoted. The most common characteristic of an encoded-URL is the presence of percent-encoded text (e.g., %60). Unquoted URLs are more readable and suitable for display, whereas fully-quoted URLs are more conservative and generally necessary for sending over the network. """scheme=self.schemepath=u'/'.join([quote_path_part(p,full_quote=full_quote)forpinself.path_parts])authority=self.get_authority(full_quote=full_quote,with_userinfo=True)query_string=self.query_params.to_text(full_quote=full_quote)fragment=quote_fragment_part(self.fragment,full_quote=full_quote)parts=[]_add=parts.appendifscheme:_add(scheme)_add(':')ifauthority:_add('//')_add(authority)elif(schemeandpath[:2]!='//'andself.uses_netloc):_add('//')ifpath:ifschemeandauthorityandpath[:1]!='/':_add('/')# TODO: i think this is here because relative paths# with absolute authorities = undefined_add(path)ifquery_string:_add('?')_add(query_string)iffragment:_add('#')_add(fragment)returnu''.join(parts)

[docs]defparse_url(url_text):"""\ Used to parse the text for a single URL into a dictionary, used internally by the :class:`URL` type. Note that "URL" has a very narrow, standards-based definition. While :func:`parse_url` may raise :class:`URLParseError` under a very limited number of conditions, such as non-integer port, a surprising number of strings are technically valid URLs. For instance, the text ``"url"`` is a valid URL, because it is a relative path. In short, do not expect this function to validate form inputs or other more colloquial usages of URLs. >>> res = parse_url('http://127.0.0.1:3000/?a=1') >>> sorted(res.keys()) # res is a basic dictionary ['_netloc_sep', 'authority', 'family', 'fragment', 'host', 'password', 'path', 'port', 'query', 'scheme', 'username'] """url_text=unicode(url_text)# raise TypeError('parse_url expected text, not %r' % url_str)um=_URL_RE.match(url_text)try:gs=um.groupdict()exceptAttributeError:raiseURLParseError('could not parse url: %r'%url_text)au_text=gs['authority']user,pw,hostinfo=None,None,au_textifau_text:userinfo,sep,hostinfo=au_text.rpartition('@')ifsep:# TODO: empty userinfo error?user,_,pw=userinfo.partition(':')host,port=None,Noneifhostinfo:host,sep,port_str=hostinfo.partition(u':')ifsep:ifu']'inport_str:host=hostinfo# wrong split, was an ipv6else:try:port=int(port_str)exceptValueError:ifport_str:# empty ports ok according to RFC 3986 6.2.3raiseURLParseError('expected integer for port, not %r'%port_str)port=Nonefamily,host=parse_host(host)gs['username']=usergs['password']=pwgs['family']=familygs['host']=hostgs['port']=portreturngs

"""# What follows is the OrderedMultiDict from dictutils.py, circa# 20161021, used for the QueryParamDict, toward the bottom."""fromcollectionsimportKeysView,ValuesView,ItemsViewtry:fromitertoolsimportizip_longestexceptImportError:fromitertoolsimportzip_longestasizip_longesttry:fromtypeutilsimportmake_sentinel_MISSING=make_sentinel(var_name='_MISSING')exceptImportError:_MISSING=object()PREV,NEXT,KEY,VALUE,SPREV,SNEXT=range(6)classOrderedMultiDict(dict):"""A MultiDict is a dictionary that can have multiple values per key and the OrderedMultiDict (OMD) is a MultiDict that retains original insertion order. Common use cases include: * handling query strings parsed from URLs * inverting a dictionary to create a reverse index (values to keys) * stacking data from multiple dictionaries in a non-destructive way The OrderedMultiDict constructor is identical to the built-in :class:`dict`, and overall the API is constitutes an intuitive superset of the built-in type: >>> omd = OrderedMultiDict() >>> omd['a'] = 1 >>> omd['b'] = 2 >>> omd.add('a', 3) >>> omd.get('a') 3 >>> omd.getlist('a') [1, 3] Some non-:class:`dict`-like behaviors also make an appearance, such as support for :func:`reversed`: >>> list(reversed(omd)) ['b', 'a'] Note that unlike some other MultiDicts, this OMD gives precedence to the most recent value added. ``omd['a']`` refers to ``3``, not ``1``. >>> omd OrderedMultiDict([('a', 1), ('b', 2), ('a', 3)]) >>> omd.poplast('a') 3 >>> omd OrderedMultiDict([('a', 1), ('b', 2)]) >>> omd.pop('a') 1 >>> omd OrderedMultiDict([('b', 2)]) Note that calling :func:`dict` on an OMD results in a dict of keys to *lists* of values: >>> from pprint import pprint as pp # ensuring proper key ordering >>> omd = OrderedMultiDict([('a', 1), ('b', 2), ('a', 3)]) >>> pp(dict(omd)) {'a': [1, 3], 'b': [2]} Note that modifying those lists will modify the OMD. If you want a safe-to-modify or flat dictionary, use :meth:`OrderedMultiDict.todict()`. >>> pp(omd.todict()) {'a': 3, 'b': 2} >>> pp(omd.todict(multi=True)) {'a': [1, 3], 'b': [2]} With ``multi=False``, items appear with the keys in to original insertion order, alongside the most-recently inserted value for that key. >>> OrderedMultiDict([('a', 1), ('b', 2), ('a', 3)]).items(multi=False) [('a', 3), ('b', 2)] """def__init__(self,*args,**kwargs):iflen(args)>1:raiseTypeError('%s expected at most 1 argument, got %s'%(self.__class__.__name__,len(args)))super(OrderedMultiDict,self).__init__()self._clear_ll()ifargs:self.update_extend(args[0])ifkwargs:self.update(kwargs)def_clear_ll(self):try:_map=self._mapexceptAttributeError:_map=self._map={}self.root=[]_map.clear()self.root[:]=[self.root,self.root,None]def_insert(self,k,v):root=self.rootcells=self._map.setdefault(k,[])last=root[PREV]cell=[last,root,k,v]last[NEXT]=root[PREV]=cellcells.append(cell)defadd(self,k,v):"""Add a single value *v* under a key *k*. Existing values under *k* are preserved. """values=super(OrderedMultiDict,self).setdefault(k,[])self._insert(k,v)values.append(v)defaddlist(self,k,v):"""Add an iterable of values underneath a specific key, preserving any values already under that key. >>> omd = OrderedMultiDict([('a', -1)]) >>> omd.addlist('a', range(3)) >>> omd OrderedMultiDict([('a', -1), ('a', 0), ('a', 1), ('a', 2)]) Called ``addlist`` for consistency with :meth:`getlist`, but tuples and other sequences and iterables work. """self_insert=self._insertvalues=super(OrderedMultiDict,self).setdefault(k,[])forsubvinv:self_insert(k,subv)values.extend(v)defget(self,k,default=None):"""Return the value for key *k* if present in the dictionary, else *default*. If *default* is not given, ``None`` is returned. This method never raises a :exc:`KeyError`. To get all values under a key, use :meth:`OrderedMultiDict.getlist`. """returnsuper(OrderedMultiDict,self).get(k,[default])[-1]defgetlist(self,k,default=_MISSING):"""Get all values for key *k* as a list, if *k* is in the dictionary, else *default*. The list returned is a copy and can be safely mutated. If *default* is not given, an empty :class:`list` is returned. """try:returnsuper(OrderedMultiDict,self).__getitem__(k)[:]exceptKeyError:ifdefaultis_MISSING:return[]returndefaultdefclear(self):"Empty the dictionary."super(OrderedMultiDict,self).clear()self._clear_ll()defsetdefault(self,k,default=_MISSING):"""If key *k* is in the dictionary, return its value. If not, insert *k* with a value of *default* and return *default*. *default* defaults to ``None``. See :meth:`dict.setdefault` for more information. """ifnotsuper(OrderedMultiDict,self).__contains__(k):self[k]=Noneifdefaultis_MISSINGelsedefaultreturnself[k]defcopy(self):"Return a shallow copy of the dictionary."returnself.__class__(self.iteritems(multi=True))@classmethoddeffromkeys(cls,keys,default=None):"""Create a dictionary from a list of keys, with all the values set to *default*, or ``None`` if *default* is not set. """returncls([(k,default)forkinkeys])defupdate(self,E,**F):"""Add items from a dictionary or iterable (and/or keyword arguments), overwriting values under an existing key. See :meth:`dict.update` for more details. """# E and F are throwback names to the dict() __doc__ifEisself:returnself_add=self.addifisinstance(E,OrderedMultiDict):forkinE:ifkinself:delself[k]fork,vinE.iteritems(multi=True):self_add(k,v)elifhasattr(E,'keys'):forkinE.keys():self[k]=E[k]else:seen=set()seen_add=seen.addfork,vinE:ifknotinseenandkinself:delself[k]seen_add(k)self_add(k,v)forkinF:self[k]=F[k]returndefupdate_extend(self,E,**F):"""Add items from a dictionary, iterable, and/or keyword arguments without overwriting existing items present in the dictionary. Like :meth:`update`, but adds to existing keys instead of overwriting them. """ifEisself:iterator=iter(E.items())elifisinstance(E,OrderedMultiDict):iterator=E.iteritems(multi=True)elifhasattr(E,'keys'):iterator=((k,E[k])forkinE.keys())else:iterator=Eself_add=self.addfork,viniterator:self_add(k,v)def__setitem__(self,k,v):ifsuper(OrderedMultiDict,self).__contains__(k):self._remove_all(k)self._insert(k,v)super(OrderedMultiDict,self).__setitem__(k,[v])def__getitem__(self,k):returnsuper(OrderedMultiDict,self).__getitem__(k)[-1]def__delitem__(self,k):super(OrderedMultiDict,self).__delitem__(k)self._remove_all(k)def__eq__(self,other):ifselfisother:returnTruetry:iflen(other)!=len(self):returnFalseexceptTypeError:returnFalseifisinstance(other,OrderedMultiDict):selfi=self.iteritems(multi=True)otheri=other.iteritems(multi=True)zipped_items=izip_longest(selfi,otheri,fillvalue=(None,None))for(selfk,selfv),(otherk,otherv)inzipped_items:ifselfk!=otherkorselfv!=otherv:returnFalseifnot(next(selfi,_MISSING)is_MISSINGandnext(otheri,_MISSING)is_MISSING):# leftovers (TODO: watch for StopIteration?)returnFalsereturnTrueelifhasattr(other,'keys'):forselfkinself:try:other[selfk]==self[selfk]exceptKeyError:returnFalsereturnTruereturnFalsedef__ne__(self,other):returnnot(self==other)defpop(self,k,default=_MISSING):"""Remove all values under key *k*, returning the most-recently inserted value. Raises :exc:`KeyError` if the key is not present and no *default* is provided. """try:returnself.popall(k)[-1]exceptKeyError:ifdefaultis_MISSING:raiseKeyError(k)returndefaultdefpopall(self,k,default=_MISSING):"""Remove all values under key *k*, returning them in the form of a list. Raises :exc:`KeyError` if the key is not present and no *default* is provided. """super_self=super(OrderedMultiDict,self)ifsuper_self.__contains__(k):self._remove_all(k)ifdefaultis_MISSING:returnsuper_self.pop(k)returnsuper_self.pop(k,default)defpoplast(self,k=_MISSING,default=_MISSING):"""Remove and return the most-recently inserted value under the key *k*, or the most-recently inserted key if *k* is not provided. If no values remain under *k*, it will be removed from the OMD. Raises :exc:`KeyError` if *k* is not present in the dictionary, or the dictionary is empty. """ifkis_MISSING:ifself:k=self.root[PREV][KEY]else:raiseKeyError('empty %r'%type(self))try:self._remove(k)exceptKeyError:ifdefaultis_MISSING:raiseKeyError(k)returndefaultvalues=super(OrderedMultiDict,self).__getitem__(k)v=values.pop()ifnotvalues:super(OrderedMultiDict,self).__delitem__(k)returnvdef_remove(self,k):values=self._map[k]cell=values.pop()cell[PREV][NEXT],cell[NEXT][PREV]=cell[NEXT],cell[PREV]ifnotvalues:delself._map[k]def_remove_all(self,k):values=self._map[k]whilevalues:cell=values.pop()cell[PREV][NEXT],cell[NEXT][PREV]=cell[NEXT],cell[PREV]delself._map[k]defiteritems(self,multi=False):"""Iterate over the OMD's items in insertion order. By default, yields only the most-recently inserted value for each key. Set *multi* to ``True`` to get all inserted items. """root=self.rootcurr=root[NEXT]ifmulti:whilecurrisnotroot:yieldcurr[KEY],curr[VALUE]curr=curr[NEXT]else:forkeyinself.iterkeys():yieldkey,self[key]defiterkeys(self,multi=False):"""Iterate over the OMD's keys in insertion order. By default, yields each key once, according to the most recent insertion. Set *multi* to ``True`` to get all keys, including duplicates, in insertion order. """root=self.rootcurr=root[NEXT]ifmulti:whilecurrisnotroot:yieldcurr[KEY]curr=curr[NEXT]else:yielded=set()yielded_add=yielded.addwhilecurrisnotroot:k=curr[KEY]ifknotinyielded:yielded_add(k)yieldkcurr=curr[NEXT]defitervalues(self,multi=False):"""Iterate over the OMD's values in insertion order. By default, yields the most-recently inserted value per unique key. Set *multi* to ``True`` to get all values according to insertion order. """fork,vinself.iteritems(multi=multi):yieldvdeftodict(self,multi=False):"""Gets a basic :class:`dict` of the items in this dictionary. Keys are the same as the OMD, values are the most recently inserted values for each key. Setting the *multi* arg to ``True`` is yields the same result as calling :class:`dict` on the OMD, except that all the value lists are copies that can be safely mutated. """ifmulti:returndict([(k,self.getlist(k))forkinself])returndict([(k,self[k])forkinself])defsorted(self,key=None,reverse=False):"""Similar to the built-in :func:`sorted`, except this method returns a new :class:`OrderedMultiDict` sorted by the provided key function, optionally reversed. Args: key (callable): A callable to determine the sort key of each element. The callable should expect an **item** (key-value pair tuple). reverse (bool): Set to ``True`` to reverse the ordering. >>> omd = OrderedMultiDict(zip(range(3), range(3))) >>> omd.sorted(reverse=True) OrderedMultiDict([(2, 2), (1, 1), (0, 0)]) Note that the key function receives an **item** (key-value tuple), so the recommended signature looks like: >>> omd = OrderedMultiDict(zip('hello', 'world')) >>> omd.sorted(key=lambda i: i[1]) # i[0] is the key, i[1] is the val OrderedMultiDict([('o', 'd'), ('l', 'l'), ('e', 'o'), ('h', 'w')]) """cls=self.__class__returncls(sorted(self.iteritems(),key=key,reverse=reverse))defsortedvalues(self,key=None,reverse=False):"""Returns a copy of the :class:`OrderedMultiDict` with the same keys in the same order as the original OMD, but the values within each keyspace have been sorted according to *key* and *reverse*. Args: key (callable): A single-argument callable to determine the sort key of each element. The callable should expect an **item** (key-value pair tuple). reverse (bool): Set to ``True`` to reverse the ordering. >>> omd = OrderedMultiDict() >>> omd.addlist('even', [6, 2]) >>> omd.addlist('odd', [1, 5]) >>> omd.add('even', 4) >>> omd.add('odd', 3) >>> somd = omd.sortedvalues() >>> somd.getlist('even') [2, 4, 6] >>> somd.keys(multi=True) == omd.keys(multi=True) True >>> omd == somd False >>> somd OrderedMultiDict([('even', 2), ('even', 4), ('odd', 1), ('odd', 3), ('even', 6), ('odd', 5)]) As demonstrated above, contents and key order are retained. Only value order changes. """try:superself_iteritems=super(OrderedMultiDict,self).iteritems()exceptAttributeError:superself_iteritems=super(OrderedMultiDict,self).items()# (not reverse) because they pop off in reverse order for reinsertionsorted_val_map=dict([(k,sorted(v,key=key,reverse=(notreverse)))fork,vinsuperself_iteritems])ret=self.__class__()forkinself.iterkeys(multi=True):ret.add(k,sorted_val_map[k].pop())returnretdefinverted(self):"""Returns a new :class:`OrderedMultiDict` with values and keys swapped, like creating dictionary transposition or reverse index. Insertion order is retained and all keys and values are represented in the output. >>> omd = OMD([(0, 2), (1, 2)]) >>> omd.inverted().getlist(2) [0, 1] Inverting twice yields a copy of the original: >>> omd.inverted().inverted() OrderedMultiDict([(0, 2), (1, 2)]) """returnself.__class__((v,k)fork,vinself.iteritems(multi=True))defcounts(self):"""Returns a mapping from key to number of values inserted under that key. Like :py:class:`collections.Counter`, but returns a new :class:`OrderedMultiDict`. """# Returns an OMD because Counter/OrderedDict may not be# available, and neither Counter nor dict maintain order.super_getitem=super(OrderedMultiDict,self).__getitem__returnself.__class__((k,len(super_getitem(k)))forkinself)defkeys(self,multi=False):"""Returns a list containing the output of :meth:`iterkeys`. See that method's docs for more details. """returnlist(self.iterkeys(multi=multi))defvalues(self,multi=False):"""Returns a list containing the output of :meth:`itervalues`. See that method's docs for more details. """returnlist(self.itervalues(multi=multi))defitems(self,multi=False):"""Returns a list containing the output of :meth:`iteritems`. See that method's docs for more details. """returnlist(self.iteritems(multi=multi))def__iter__(self):returnself.iterkeys()def__reversed__(self):root=self.rootcurr=root[PREV]lengths={}lengths_sd=lengths.setdefaultget_values=super(OrderedMultiDict,self).__getitem__whilecurrisnotroot:k=curr[KEY]vals=get_values(k)iflengths_sd(k,1)==len(vals):yieldklengths[k]+=1curr=curr[PREV]def__repr__(self):cn=self.__class__.__name__kvs=', '.join([repr((k,v))fork,vinself.iteritems(multi=True)])return'%s([%s])'%(cn,kvs)defviewkeys(self):"OMD.viewkeys() -> a set-like object providing a view on OMD's keys"returnKeysView(self)defviewvalues(self):"OMD.viewvalues() -> an object providing a view on OMD's values"returnValuesView(self)defviewitems(self):"OMD.viewitems() -> a set-like object providing a view on OMD's items"returnItemsView(self)try:# try to import the built-in one anywaysfromboltons.dictutilsimportOrderedMultiDictexceptImportError:passOMD=OrderedMultiDict

[docs]classQueryParamDict(OrderedMultiDict):"""A subclass of :class:`~dictutils.OrderedMultiDict` specialized for representing query string values. Everything is fully unquoted on load and all parsed keys and values are strings by default. As the name suggests, multiple values are supported and insertion order is preserved. >>> qp = QueryParamDict.from_text(u'key=val1&key=val2&utm_source=rtd') >>> qp.getlist('key') [u'val1', u'val2'] >>> qp['key'] u'val2' >>> qp.add('key', 'val3') >>> qp.to_text() 'key=val1&key=val2&utm_source=rtd&key=val3' See :class:`~dictutils.OrderedMultiDict` for more API features. """

[docs]@classmethoddeffrom_text(cls,query_string):""" Parse *query_string* and return a new :class:`QueryParamDict`. """pairs=parse_qsl(query_string,keep_blank_values=True)returncls(pairs)

[docs]defto_text(self,full_quote=False):""" Render and return a query string. Args: full_quote (bool): Whether or not to percent-quote special characters or leave them decoded for readability. """ret_list=[]fork,vinself.iteritems(multi=True):key=quote_query_part(to_unicode(k),full_quote=full_quote)ifvisNone:ret_list.append(key)else:val=quote_query_part(to_unicode(v),full_quote=full_quote)ret_list.append(u'='.join((key,val)))returnu'&'.join(ret_list)