Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 63b1a6f

Browse files
committed
Vendor rfc3986 package
1 parent 54fc80c commit 63b1a6f

File tree

8 files changed

+1206
-0
lines changed

8 files changed

+1206
-0
lines changed

hyper/packages/rfc3986/__init__.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
"""
17+
rfc3986
18+
=======
19+
20+
An implementation of semantics and validations described in RFC 3986. See
21+
http://rfc3986.rtfd.org/ for documentation.
22+
23+
:copyright: (c) 2014 Rackspace
24+
:license: Apache v2.0, see LICENSE for details
25+
"""
26+
27+
__title__ = 'rfc3986'
28+
__author__ = 'Ian Cordasco'
29+
__author_email__ = '[email protected]'
30+
__license__ = 'Apache v2.0'
31+
__copyright__ = 'Copyright 2014 Rackspace'
32+
__version__ = '0.3.0'
33+
34+
from .api import (URIReference, uri_reference, is_valid_uri, normalize_uri,
35+
urlparse)
36+
from .parseresult import ParseResult
37+
38+
__all__ = (
39+
'ParseResult',
40+
'URIReference',
41+
'is_valid_uri',
42+
'normalize_uri',
43+
'uri_reference',
44+
'urlparse',
45+
)

hyper/packages/rfc3986/api.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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)

hyper/packages/rfc3986/compat.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import sys
16+
17+
18+
if sys.version_info >= (3, 0):
19+
unicode = str # Python 3.x
20+
21+
22+
def to_str(b, encoding):
23+
if hasattr(b, 'decode') and not isinstance(b, unicode):
24+
b = b.decode('utf-8')
25+
return b
26+
27+
28+
def to_bytes(s, encoding):
29+
if hasattr(s, 'encode') and not isinstance(s, bytes):
30+
s = s.encode('utf-8')
31+
return s

hyper/packages/rfc3986/exceptions.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
class RFC3986Exception(Exception):
3+
pass
4+
5+
6+
class InvalidAuthority(RFC3986Exception):
7+
def __init__(self, authority):
8+
super(InvalidAuthority, self).__init__(
9+
"The authority ({0}) is not valid.".format(authority))
10+
11+
12+
class InvalidPort(RFC3986Exception):
13+
def __init__(self, port):
14+
super(InvalidPort, self).__init__(
15+
'The port ("{0}") is not valid.'.format(port))
16+
17+
18+
class ResolutionError(RFC3986Exception):
19+
def __init__(self, uri):
20+
super(ResolutionError, self).__init__(
21+
"{0} is not an absolute URI.".format(uri.unsplit()))

0 commit comments

Comments
 (0)