|
| 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 | +""" |
| 16 | +rfc3986.api |
| 17 | +~~~~~~~~~~~ |
| 18 | +
|
| 19 | +This defines the simple API to rfc3986. This module defines 3 functions and |
| 20 | +provides access to the class ``URIReference``. |
| 21 | +""" |
| 22 | + |
| 23 | +from .uri import URIReference |
| 24 | +from .parseresult import ParseResult |
| 25 | + |
| 26 | + |
| 27 | +def 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 | + |
| 41 | +def is_valid_uri(uri, encoding='utf-8', **kwargs): |
| 42 | + """Determine if the URI given is valid. |
| 43 | +
|
| 44 | + This is a convenience function. You could use either |
| 45 | + ``uri_reference(uri).is_valid()`` or |
| 46 | + ``URIReference.from_string(uri).is_valid()`` to achieve the same result. |
| 47 | +
|
| 48 | + :param str uri: The URI to be validated. |
| 49 | + :param str encoding: The encoding of the string provided |
| 50 | + :param bool require_scheme: Set to ``True`` if you wish to require the |
| 51 | + presence of the scheme component. |
| 52 | + :param bool require_authority: Set to ``True`` if you wish to require the |
| 53 | + presence of the authority component. |
| 54 | + :param bool require_path: Set to ``True`` if you wish to require the |
| 55 | + presence of the path component. |
| 56 | + :param bool require_query: Set to ``True`` if you wish to require the |
| 57 | + presence of the query component. |
| 58 | + :param bool require_fragment: Set to ``True`` if you wish to require the |
| 59 | + presence of the fragment component. |
| 60 | + :returns: ``True`` if the URI is valid, ``False`` otherwise. |
| 61 | + :rtype: bool |
| 62 | + """ |
| 63 | + return URIReference.from_string(uri, encoding).is_valid(**kwargs) |
| 64 | + |
| 65 | + |
| 66 | +def normalize_uri(uri, encoding='utf-8'): |
| 67 | + """Normalize the given URI. |
| 68 | +
|
| 69 | + This is a convenience function. You could use either |
| 70 | + ``uri_reference(uri).normalize().unsplit()`` or |
| 71 | + ``URIReference.from_string(uri).normalize().unsplit()`` instead. |
| 72 | +
|
| 73 | + :param str uri: The URI to be normalized. |
| 74 | + :param str encoding: The encoding of the string provided |
| 75 | + :returns: The normalized URI. |
| 76 | + :rtype: str |
| 77 | + """ |
| 78 | + normalized_reference = URIReference.from_string(uri, encoding).normalize() |
| 79 | + return normalized_reference.unsplit() |
| 80 | + |
| 81 | + |
| 82 | +def urlparse(uri, encoding='utf-8'): |
| 83 | + """Parse a given URI and return a ParseResult. |
| 84 | +
|
| 85 | + This is a partial replacement of the standard library's urlparse function. |
| 86 | +
|
| 87 | + :param str uri: The URI to be parsed. |
| 88 | + :param str encoding: The encoding of the string provided. |
| 89 | + :returns: A parsed URI |
| 90 | + :rtype: :class:`~rfc3986.parseresult.ParseResult` |
| 91 | + """ |
| 92 | + return ParseResult.from_string(uri, encoding, strict=False) |
0 commit comments