Parse, Serialize, and Tokenize Resource Indicators
ri provides tools for working with standard IRIs or URIs. However, it is not strict. It does not require exact formatting for a parse operation to succeed; rather, the validation of the output is left to the user.
The module refers to IRI and URI strings as Resource Indicators. The distinction is made as the module deals with a slight generalization where constraints are not enforced or checked.
Entry Points
Types
A parsed indicator is designated a type. The set of possible types is inspired by the URI and IRI standards:
collections
scheme_chars0
scheme_chars = '-.+0123456789'
reserved_chars0
reserved_chars = "%!*'();:@&=+$,/?#[]"
_pct_encode0
_pct_encode = '%%%0.2X'.__mod__
_decode_parts0
_decode_parts(string)
decode_percent_escapes0
decode_percent_escapes(string)
strict0
strict()
Enable strict serializations. When performed on a cached import, this will effect the entire process.
In order to isolate the effect to a certain part of the process, the module should be created from the loader and strict should be called relative to the local copy.
_uri_escape0
_uri_escape = (lambda x: ''.join(map(_pct_encode, x.encode('utf-8'))))
_mktrans0
_mktrans = (lambda z: str.maketrans({x:_pct_encode(x) for x in [ord(y) for y in z] + list(range(0,33))}))
_percent_translations0
_percent_translations = _mktrans("%")
_user_percent_translations0
_user_percent_translations = _mktrans("%@:/?#")
_password_percent_translations0
_password_percent_translations = _mktrans("%@/?#")
_host_percent_translations0
_host_percent_translations = \
_address_percent_translations = \
_port_percent_translations = \
_primary_percent_translations = _mktrans("%/?#")
_fragment_percent_translations0
_fragment_percent_translations = _mktrans("%?#")
_query_key_percent_translations0
_query_key_percent_translations = _mktrans("%&#=")
_query_value_percent_translations0
_query_value_percent_translations = _mktrans("%&#")
Parts0
Parts = collections.namedtuple("Parts",
('type', 'scheme', 'netloc', 'path', 'query', 'fragment')
)
split296%
split(iri)
Split an IRI into its base components based on the markers:
(: | ://), /, ?, #
Returns the top-level parts of the IRI as a namedtuple.
splitParameters
join_path0
join_path(p)
Join a list of paths(strings) on "/" after escaping them.
unsplit_path0
unsplit_path = join_path
split_path0
split_path(p)
Return a list of unescaped strings split on "/".
Set fieldproc to str if the components' percent escapes should not be decoded.
join292%
join(t)
Make an RI from a split RI(5-tuple)
unsplit0
unsplit = join
remap_ipv4_address370%
remap_ipv4_address(string)
Recognize IPv4 hosts as addresses.
split_netloc197%
split_netloc(netloc)
Split a network location into a 4-tuple, (user, password, host, port).
Set fieldproc to str if the components' percent escapes should not be decoded.
join_netloc0
join_netloc(t)
Create a netloc fragment from the given tuple(user, password, host, address, port).
unsplit_netloc0
unsplit_netloc = join_netloc
parse_query0
parse_query(query)
structure0
structure(t)
Create a dictionary from a split RI(5-tuple).
Set fieldproc to str if the components' percent escapes should not be decoded.
construct_query0
construct_query(x)
Given a sequence of (key, value) pairs, construct.
construct0
construct(x)
Construct a RI tuple(5-tuple) from a dictionary object.
parse0
parse(iri)
Parse an RI into a dictionary object. Synonym for structure(split(x)).
Set fieldproc to str if the components' percent escapes should not be decoded.
serialize0
serialize(x)
Return an RI from a dictionary object. Synonym for join(construct(x)).
http100%
http(struct)
Return the HTTP Request-URI suitable for submission with an HTTP request.
context_tokens200%
context_tokens(scheme, type, user, password, host, address, port)
Format the authority fields of a Resource Indicator.
query_tokens220%
query_tokens(query)
fragment_tokens30%
fragment_tokens(fragment)
path_tokens280%
path_tokens(root, path)
tokens50%
tokens(struct)
Construct an iterator producing Resource Indicator Tokens. The items are pairs providing the type and the exact text to be used to reconstruct the Resource Indicator parsed into the given struct.