選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

107 行
3.8KB

  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. Module containing the simple and functional API for rfc3986.
  17. This module defines functions and provides access to the public attributes
  18. and classes of rfc3986.
  19. """
  20. from .iri import IRIReference
  21. from .parseresult import ParseResult
  22. from .uri import URIReference
  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 iri_reference(iri, encoding='utf-8'):
  34. """Parse a IRI string into an IRIReference.
  35. This is a convenience function. You could achieve the same end by using
  36. ``IRIReference.from_string(iri)``.
  37. :param str iri: The IRI which needs to be parsed into a reference.
  38. :param str encoding: The encoding of the string provided
  39. :returns: A parsed IRI
  40. :rtype: :class:`IRIReference`
  41. """
  42. return IRIReference.from_string(iri, encoding)
  43. def is_valid_uri(uri, encoding='utf-8', **kwargs):
  44. """Determine if the URI given is valid.
  45. This is a convenience function. You could use either
  46. ``uri_reference(uri).is_valid()`` or
  47. ``URIReference.from_string(uri).is_valid()`` to achieve the same result.
  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. def normalize_uri(uri, encoding='utf-8'):
  65. """Normalize the given URI.
  66. This is a convenience function. You could use either
  67. ``uri_reference(uri).normalize().unsplit()`` or
  68. ``URIReference.from_string(uri).normalize().unsplit()`` instead.
  69. :param str uri: The URI to be normalized.
  70. :param str encoding: The encoding of the string provided
  71. :returns: The normalized URI.
  72. :rtype: str
  73. """
  74. normalized_reference = URIReference.from_string(uri, encoding).normalize()
  75. return normalized_reference.unsplit()
  76. def urlparse(uri, encoding='utf-8'):
  77. """Parse a given URI and return a ParseResult.
  78. This is a partial replacement of the standard library's urlparse function.
  79. :param str uri: The URI to be parsed.
  80. :param str encoding: The encoding of the string provided.
  81. :returns: A parsed URI
  82. :rtype: :class:`~rfc3986.parseresult.ParseResult`
  83. """
  84. return ParseResult.from_string(uri, encoding, strict=False)