api.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. This defines the simple API to rfc3986. This module defines 3 functions and
  19. provides access to the class ``URIReference``.
  20. """
  21. from .uri import URIReference
  22. from .parseresult import ParseResult
  23. def uri_reference(uri, encoding='utf-8'):
  24. """Parse a URI string into a URIReference.
  25. This is a convenience function. You could achieve the same end by using
  26. ``URIReference.from_string(uri)``.
  27. :param str uri: The URI which needs to be parsed into a reference.
  28. :param str encoding: The encoding of the string provided
  29. :returns: A parsed URI
  30. :rtype: :class:`URIReference`
  31. """
  32. return URIReference.from_string(uri, encoding)
  33. def is_valid_uri(uri, encoding='utf-8', **kwargs):
  34. """Determine if the URI given is valid.
  35. This is a convenience function. You could use either
  36. ``uri_reference(uri).is_valid()`` or
  37. ``URIReference.from_string(uri).is_valid()`` to achieve the same result.
  38. :param str uri: The URI to be validated.
  39. :param str encoding: The encoding of the string provided
  40. :param bool require_scheme: Set to ``True`` if you wish to require the
  41. presence of the scheme component.
  42. :param bool require_authority: Set to ``True`` if you wish to require the
  43. presence of the authority component.
  44. :param bool require_path: Set to ``True`` if you wish to require the
  45. presence of the path component.
  46. :param bool require_query: Set to ``True`` if you wish to require the
  47. presence of the query component.
  48. :param bool require_fragment: Set to ``True`` if you wish to require the
  49. presence of the fragment component.
  50. :returns: ``True`` if the URI is valid, ``False`` otherwise.
  51. :rtype: bool
  52. """
  53. return URIReference.from_string(uri, encoding).is_valid(**kwargs)
  54. def normalize_uri(uri, encoding='utf-8'):
  55. """Normalize the given URI.
  56. This is a convenience function. You could use either
  57. ``uri_reference(uri).normalize().unsplit()`` or
  58. ``URIReference.from_string(uri).normalize().unsplit()`` instead.
  59. :param str uri: The URI to be normalized.
  60. :param str encoding: The encoding of the string provided
  61. :returns: The normalized URI.
  62. :rtype: str
  63. """
  64. normalized_reference = URIReference.from_string(uri, encoding).normalize()
  65. return normalized_reference.unsplit()
  66. def urlparse(uri, encoding='utf-8'):
  67. """Parse a given URI and return a ParseResult.
  68. This is a partial replacement of the standard library's urlparse function.
  69. :param str uri: The URI to be parsed.
  70. :param str encoding: The encoding of the string provided.
  71. :returns: A parsed URI
  72. :rtype: :class:`~rfc3986.parseresult.ParseResult`
  73. """
  74. return ParseResult.from_string(uri, encoding, strict=False)