source: uri/README.rst@ 262

Last change on this file since 262 was 261, checked in by wouter, 3 years ago

readme update

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