@@ -4,75 +4,106 @@ ada-url
44
55This is ``ada_url ``, a Python library for parsing and joining URLs.
66
7+ Installation
8+ ------------
79
8- WHATWG URL compliance
9- ---------------------
10-
11- Unlike the standard library's ``urllib.parse `` module, this library is compliant with the WHATWG URL spec.
12-
13- .. code-block :: python
14-
15- urlstring = " https://www.GOoglé.com/./path/../path2/"
16-
17- import ada_url
18-
19- # prints www.xn--googl-fsa.com,
20- # the correctly parsed domain name according to WHATWG
21- print (ada_url.URL(urlstring).hostname)
22- # prints /path2/
23- # the correctly parsed pathname according to WHATWG
24- print (ada_url.URL(urlstring).pathname)
25-
26- import urllib
10+ Install from `PyPI <https://pypi.org/project/ada-url/ >`__:
2711
28- # prints www.googlé.com
29- print (urllib.parse.urlparse(urlstring).hostname)
30- # prints /./path/../path2/
31- print (urllib.parse.urlparse(urlstring).path)
12+ .. code-block :: sh
3213
14+ pip install boto3-helpers
3315
34- Examples
35- --------
16+ Usage examples
17+ --------------
3618
3719This package exposes a ``URL `` class that is intended to match the one described in the
3820`WHATWG URL spec <https://url.spec.whatwg.org/#url-class >`__.
3921
4022.. code-block :: python
4123
42- >> > import ada_url
43- >> > ada_url. URL(' https://example.org/path/../file.txt' ) as urlobj:
24+ >> > from ada_url import URL
25+ >> > URL(' https://example.org/path/../file.txt' ) as urlobj:
4426 >> > urlobj.host = ' example.com'
4527 >> > new_url = urlobj.href
4628 >> > new_url
4729 ' https://example.com/file.txt'
4830
49- It also provides some higher level functions for parsing and manipulating URLs.
31+ It also provides high level functions for parsing and manipulating URLs. Validating
32+ a URL:
5033
5134.. code-block :: python
5235
53- >> > import ada_url
54- >> > ada_url. check_url(' https://example.org' )
36+ >> > from ada_url import check_url
37+ >> > check_url(' https://example.org' )
5538 True
56- >> > ada_url.join_url(
57- ' https://example.org/dir/child.txt' , ' ../parent.txt'
58- )
59- ' https://example.org/parent.txt'
60- >> > ada_url.normalize_url(' https://example.org/dir/../parent.txt' )
61- ' https://example.org/parent.txt'
62- >> > ada_url.parse_url(' https://user:pass@example.org:80/api?q=1#2' )
39+ >> > check_url(' http://example:bougus' )
40+ False
41+
42+ Parsing a URL:
43+
44+ .. code-block :: python
45+
46+ >> > from ada_url import parse_url
47+ >> > parse_url(' https://user:pass@example.org:80/api?q=1#2' )
6348 {
6449 ' href' : ' https://user:pass@example.org:80/api?q=1#2' ,
6550 ' username' : ' user' ,
6651 ' password' : ' pass' ,
6752 ' protocol' : ' https:' ,
68- ' host' : ' example.org:80' ,
6953 ' port' : ' 80' ,
7054 ' hostname' : ' example.org' ,
55+ ' host' : ' example.org:80' ,
7156 ' pathname' : ' /api' ,
7257 ' search' : ' ?q=1' ,
73- ' hash' : ' #2'
58+ ' hash' : ' #2' ,
59+ ' origin' : ' https://example.org:80' ,
60+ ' host_type' : < HostType.DEFAULT : 0 > ,
61+ ' scheme_type' : < SchemeType.HTTPS : 2 >
7462 }
63+
64+ Replacing URL components:
65+
66+ .. code-block :: python
67+
68+ >> > from ada_url import replace_url
7569 >> > ada_url.replace_url(' http://example.org:80' , protocol = ' https:' )
7670 ' https://example.org/'
7771
78- You can find more documentation at `Read the Docs <https://ada-url.readthedocs.io >`__.
72+ Joining a URL with a relative fragment:
73+
74+ >>> from ada_url import join_url
75+ >>> join_url(' https://example.org/dir/child.txt' , ' ../parent.txt' )
76+ 'https://example.org/parent.txt'
77+
78+ WHATWG URL compliance
79+ ---------------------
80+
81+ This library is compliant with the WHATWG URL spec. This means, among other things,
82+ that it properly encodes IDNs and resolves paths:
83+
84+ .. code-block :: python
85+
86+ >> > from ada_url import URL
87+ >> > parsed_url = URL(' https://www.GOoglé.com/./path/../path2/' )
88+ >> > parsed_url.hostname
89+ ' www.xn--googl-fsa.com'
90+ >> > parsed_url.pathname
91+ ' /path2/'
92+
93+ Contrast that with the Python standard library's ``urlib.parse `` module:
94+
95+ .. code-block :: python
96+
97+ >> > from urllib.parse import urlparse
98+ >> > parsed_url = urlparse()
99+ >> > parsed_url.hostname
100+ ' www.googlé.com'
101+ >> > parsed_url.path
102+ ' /./path/../path2/'
103+
104+ More information
105+ ----------------
106+
107+ * ``ada-url `` is based on the `Ada <https://www.ada-url.com/ >`__ project.
108+ * A full API reference is available at `Read the Docs <https://ada-url.readthedocs.io >`__.
109+ * Source code is available at `GitHub <https://github.com/ada-url/ada-python >`__.
0 commit comments