source: uri/rfc3986/api.py@ 262

Last change on this file since 262 was 230, checked in by wouter, 4 years ago

#91 clone https://pypi.org/project/rfc3986/

File size: 3.8 KB
Line 
1# -*- coding: utf-8 -*-
2# Copyright (c) 2014 Rackspace
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""
16Module containing the simple and functional API for rfc3986.
17
18This module defines functions and provides access to the public attributes
19and classes of rfc3986.
20"""
21
22from .iri import IRIReference
23from .parseresult import ParseResult
24from .uri import URIReference
25
26
27def uri_reference(uri, encoding="utf-8"):
28 """Parse a URI string into a URIReference.
29
30 This is a convenience function. You could achieve the same end by using
31 ``URIReference.from_string(uri)``.
32
33 :param str uri: The URI which needs to be parsed into a reference.
34 :param str encoding: The encoding of the string provided
35 :returns: A parsed URI
36 :rtype: :class:`URIReference`
37 """
38 return URIReference.from_string(uri, encoding)
39
40
41def iri_reference(iri, encoding="utf-8"):
42 """Parse a IRI string into an IRIReference.
43
44 This is a convenience function. You could achieve the same end by using
45 ``IRIReference.from_string(iri)``.
46
47 :param str iri: The IRI which needs to be parsed into a reference.
48 :param str encoding: The encoding of the string provided
49 :returns: A parsed IRI
50 :rtype: :class:`IRIReference`
51 """
52 return IRIReference.from_string(iri, encoding)
53
54
55def is_valid_uri(uri, encoding="utf-8", **kwargs):
56 """Determine if the URI given is valid.
57
58 This is a convenience function. You could use either
59 ``uri_reference(uri).is_valid()`` or
60 ``URIReference.from_string(uri).is_valid()`` to achieve the same result.
61
62 :param str uri: The URI to be validated.
63 :param str encoding: The encoding of the string provided
64 :param bool require_scheme: Set to ``True`` if you wish to require the
65 presence of the scheme component.
66 :param bool require_authority: Set to ``True`` if you wish to require the
67 presence of the authority component.
68 :param bool require_path: Set to ``True`` if you wish to require the
69 presence of the path component.
70 :param bool require_query: Set to ``True`` if you wish to require the
71 presence of the query component.
72 :param bool require_fragment: Set to ``True`` if you wish to require the
73 presence of the fragment component.
74 :returns: ``True`` if the URI is valid, ``False`` otherwise.
75 :rtype: bool
76 """
77 return URIReference.from_string(uri, encoding).is_valid(**kwargs)
78
79
80def normalize_uri(uri, encoding="utf-8"):
81 """Normalize the given URI.
82
83 This is a convenience function. You could use either
84 ``uri_reference(uri).normalize().unsplit()`` or
85 ``URIReference.from_string(uri).normalize().unsplit()`` instead.
86
87 :param str uri: The URI to be normalized.
88 :param str encoding: The encoding of the string provided
89 :returns: The normalized URI.
90 :rtype: str
91 """
92 normalized_reference = URIReference.from_string(uri, encoding).normalize()
93 return normalized_reference.unsplit()
94
95
96def urlparse(uri, encoding="utf-8"):
97 """Parse a given URI and return a ParseResult.
98
99 This is a partial replacement of the standard library's urlparse function.
100
101 :param str uri: The URI to be parsed.
102 :param str encoding: The encoding of the string provided.
103 :returns: A parsed URI
104 :rtype: :class:`~rfc3986.parseresult.ParseResult`
105 """
106 return ParseResult.from_string(uri, encoding, strict=False)
Note: See TracBrowser for help on using the repository browser.