source: uri/README.rst@ 1276

Last change on this file since 1276 was 264, checked in by wouter, 3 years ago

#94 fix URI test code

File size: 6.1 KB
Line 
1build:
2
3python3 setup.py sdist
4
5
6
7This is a slightly modified version of ​​https://pypi.org/project/rfc3986/
8
9The main reason is that I need a simple URI class.
10
11THe directory hierarchy was shuffled to make it work with setup.py
12and a new uri.py class was added to the root of the project.
13A test file uriTest.py was added to tests folder.
14The doc dir was removed because setup.py seems insisting to including
15it in the distri which is not what we want.
16
17pip install https://tracinsy.ewi.tudelft.nl/pubtrac/Utilities/export/237/uri/dist/uri-1.0.0.tar.gz
18
19Below the original readme from rfc3986. You don't need it and can ignore it.
20
21
22rfc3986
23=======
24
25A Python implementation of `RFC 3986`_ including validation and authority
26parsing.
27
28Installation
29------------
30
31Use pip to install ``rfc3986`` like so::
32
33 pip install rfc3986
34
35License
36-------
37
38`Apache License Version 2.0`_
39
40Example Usage
41-------------
42
43The following are the two most common use cases envisioned for ``rfc3986``.
44
45Replacing ``urlparse``
46``````````````````````
47
48To parse a URI and receive something very similar to the standard library's
49``urllib.parse.urlparse``
50
51.. code-block:: python
52
53 from rfc3986 import urlparse
54
55 ssh = urlparse('ssh://user@git.openstack.org:29418/openstack/glance.git')
56 print(ssh.scheme) # => ssh
57 print(ssh.userinfo) # => user
58 print(ssh.params) # => None
59 print(ssh.port) # => 29418
60
61To create a copy of it with new pieces you can use ``copy_with``:
62
63.. code-block:: python
64
65 new_ssh = ssh.copy_with(
66 scheme='https'
67 userinfo='',
68 port=443,
69 path='/openstack/glance'
70 )
71 print(new_ssh.scheme) # => https
72 print(new_ssh.userinfo) # => None
73 # etc.
74
75Strictly Parsing a URI and Applying Validation
76``````````````````````````````````````````````
77
78To parse a URI into a convenient named tuple, you can simply:
79
80.. code-block:: python
81
82 from rfc3986 import uri_reference
83
84 example = uri_reference('http://example.com')
85 email = uri_reference('mailto:user@domain.com')
86 ssh = uri_reference('ssh://user@git.openstack.org:29418/openstack/keystone.git')
87
88With a parsed URI you can access data about the components:
89
90.. code-block:: python
91
92 print(example.scheme) # => http
93 print(email.path) # => user@domain.com
94 print(ssh.userinfo) # => user
95 print(ssh.host) # => git.openstack.org
96 print(ssh.port) # => 29418
97
98It can also parse URIs with unicode present:
99
100.. code-block:: python
101
102 uni = uri_reference(b'http://httpbin.org/get?utf8=\xe2\x98\x83') # ☃
103 print(uni.query) # utf8=%E2%98%83
104
105With a parsed URI you can also validate it:
106
107.. code-block:: python
108
109 if ssh.is_valid():
110 subprocess.call(['git', 'clone', ssh.unsplit()])
111
112You can also take a parsed URI and normalize it:
113
114.. code-block:: python
115
116 mangled = uri_reference('hTTp://exAMPLe.COM')
117 print(mangled.scheme) # => hTTp
118 print(mangled.authority) # => exAMPLe.COM
119
120 normal = mangled.normalize()
121 print(normal.scheme) # => http
122 print(mangled.authority) # => example.com
123
124But these two URIs are (functionally) equivalent:
125
126.. code-block:: python
127
128 if normal == mangled:
129 webbrowser.open(normal.unsplit())
130
131Your paths, queries, and fragments are safe with us though:
132
133.. code-block:: python
134
135 mangled = uri_reference('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')
136 normal = mangled.normalize()
137 assert normal == 'hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth'
138 assert normal == 'http://example.com/Some/reallY/biZZare/pAth'
139 assert normal != 'http://example.com/some/really/bizzare/path'
140
141If you do not actually need a real reference object and just want to normalize
142your URI:
143
144.. code-block:: python
145
146 from rfc3986 import normalize_uri
147
148 assert (normalize_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth') ==
149 'http://example.com/Some/reallY/biZZare/pAth')
150
151You can also very simply validate a URI:
152
153.. code-block:: python
154
155 from rfc3986 import is_valid_uri
156
157 assert is_valid_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')
158
159Requiring Components
160~~~~~~~~~~~~~~~~~~~~
161
162You can validate that a particular string is a valid URI and require
163independent components:
164
165.. code-block:: python
166
167 from rfc3986 import is_valid_uri
168
169 assert is_valid_uri('http://localhost:8774/v2/resource',
170 require_scheme=True,
171 require_authority=True,
172 require_path=True)
173
174 # Assert that a mailto URI is invalid if you require an authority
175 # component
176 assert is_valid_uri('mailto:user@example.com', require_authority=True) is False
177
178If you have an instance of a ``URIReference``, you can pass the same arguments
179to ``URIReference#is_valid``, e.g.,
180
181.. code-block:: python
182
183 from rfc3986 import uri_reference
184
185 http = uri_reference('http://localhost:8774/v2/resource')
186 assert uri.is_valid(require_scheme=True,
187 require_authority=True,
188 require_path=True)
189
190 # Assert that a mailto URI is invalid if you require an authority
191 # component
192 mailto = uri_reference('mailto:user@example.com')
193 assert uri.is_valid(require_authority=True) is False
194
195Alternatives
196------------
197
198- `rfc3987 <https://pypi.python.org/pypi/rfc3987/1.3.4>`_
199
200 This is a direct competitor to this library, with extra features,
201 licensed under the GPL.
202
203- `uritools <https://pypi.python.org/pypi/uritools/0.5.1>`_
204
205 This can parse URIs in the manner of RFC 3986 but provides no validation and
206 only recently added Python 3 support.
207
208- Standard library's `urlparse`/`urllib.parse`
209
210 The functions in these libraries can only split a URI (valid or not) and
211 provide no validation.
212
213Contributing
214------------
215
216This project follows and enforces the Python Software Foundation's `Code of
217Conduct <https://www.python.org/psf/codeofconduct/>`_.
218
219If you would like to contribute but do not have a bug or feature in mind, feel
220free to email Ian and find out how you can help.
221
222The git repository for this project is maintained at
223https://github.com/python-hyper/rfc3986
224
225.. _RFC 3986: http://tools.ietf.org/html/rfc3986
226.. _Apache License Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
Note: See TracBrowser for help on using the repository browser.