Package: rfc3987
This module provides regular expressions according toRFC 3986 “Uniform
Resource Identifier (URI): Generic Syntax”andRFC 3987 “Internationalized
Resource Identifiers (IRIs)”, and
utilities for composition and relative resolution of references.APImatch(string, rule=’IRI_reference’)Convenience function for checking ifstringmatches a specific rule.Returns a match object or None:>>> assert match('%C7X', 'pct_encoded') is None
>>> assert match('%C7', 'pct_encoded')
>>> assert match('%c7', 'pct_encoded')parse(string, rule=’IRI_reference’)Parsesstringaccording toruleinto a dict of subcomponents.Ifruleis None, parse an IRI_referencewithout validation.Ifregexis available, any rule is supported; withre,rulemust be
‘IRI_reference’ or some special case thereof (‘IRI’, ‘absolute_IRI’,
‘irelative_ref’, ‘irelative_part’, ‘URI_reference’, ‘URI’, ‘absolute_URI’,
‘relative_ref’, ‘relative_part’).>>> d = parse('http://tools.ietf.org/html/rfc3986#appendix-A',
...           rule='URI')
>>> assert all([ d['scheme'] == 'http',
...              d['authority'] == 'tools.ietf.org',
...              d['path'] == '/html/rfc3986',
...              d['query'] == None,
...              d['fragment'] == 'appendix-A' ])compose(**parts)Returns an URIcomposedfrom named parts.resolve(base, uriref, strict=True, return_parts=False)ResolvesanURI referencerelative to abaseURI.Test cases:>>> base = resolve.test_cases_base
>>> for relative, resolved in resolve.test_cases.items():
...     assert resolve(base, relative) == resolvedIfreturn_partsis True, returns a dict of named parts instead of
a string.Examples:>>> assert resolve('urn:rootless', '../../name') == 'urn:name'
>>> assert resolve('urn:root/less', '../../name') == 'urn:/name'
>>> assert resolve('http://a/b', 'http:g') == 'http:g'
>>> assert resolve('http://a/b', 'http:g', strict=False) == 'http://a/g'patternsA dict of regular expressions with useful group names.
Compilable (withregexonly) without need for any particular compilation
flag.[bmp_][u]patterns[_no_names]Alternative versions ofpatterns.
[u]nicode strings without group names for theremodule.
BMP only for narrow builds.get_compiled_pattern(rule, flags=0)Returns a compiled pattern object for a rule name or template string.Usage for validation:>>> uri = get_compiled_pattern('^%(URI)s$')
>>> assert uri.match('http://tools.ietf.org/html/rfc3986#appendix-A')
>>> assert not get_compiled_pattern('^%(relative_ref)s$').match('#f#g')
>>> from unicodedata import lookup
>>> smp = 'urn:' + lookup('OLD ITALIC LETTER A')  # U+00010300
>>> assert not uri.match(smp)
>>> m = get_compiled_pattern('^%(IRI)s$').match(smp)On narrow builds, non-BMP characters are (incorrectly) excluded:>>> assert NARROW_BUILD == (not m)For parsing, some subcomponents are captured in named groups (only ifregexis available, otherwise seeparse):>>> match = uri.match('http://tools.ietf.org/html/rfc3986#appendix-A')
>>> d = match.groupdict()
>>> if REGEX:
...     assert all([ d['scheme'] == 'http',
...                  d['authority'] == 'tools.ietf.org',
...                  d['path'] == '/html/rfc3986',
...                  d['query'] == None,
...                  d['fragment'] == 'appendix-A' ])

>>> for r in patterns.keys():
...     assert get_compiled_pattern(r)format_patterns(**names)Returns a dict of patterns (regular expressions) keyed byrule names for URIsandrule names for IRIs.See also the module level dicts of patterns, andget_compiled_pattern.To wrap a rule in a named capture group, pass it as keyword argument:
rule_name=’group_name’. By default, the formatted patterns contain no
named groups.Patterns arestrinstances (be it in python 2.x or 3.x) containing ASCII
characters only.Caveats:withre, named capture groups cannot occur on multiple branches of an
alternationwithrebefore python 3.3,\uand\Uescapes must be
preprocessed (seeissue3665)on narrow builds, character ranges beyond BMP are not supportedDependenciesSome features requireregex.This package’s docstrings are tested on python 2.6, 2.7, and 3.2 to 3.6.
Note that in python<=3.2, characters beyond the Basic Multilingual Plane are
not supported on narrow builds (seeissue12729).Release notesversion 1.3.8:fixed deprecated escape sequenceversion 1.3.6:fixed a bug in IPv6 pattern:>>> assert match('::0:0:0:0:0.0.0.0', 'IPv6address')version 1.3.4:allowed for lower case percent encodingversion 1.3.3:fixed a bug inresolvewhich left “../” at the beginning of some pathsversion 1.3.2:convenience functionmatchpatterns restricted to the BMP for narrow buildsadapted doctests for python 3.3compatibility with python 2.6 (thanks to Thijs Janssen)version 1.3.1:somerecompatibility: get_compiled_pattern, parsedroppedregexfrom setup.py requirementsversion 1.3.0:python 3.x compatibilityformat_patternsversion 1.2.1:compose, resolveSupportThis is free software. You may show your appreciation with adonation.