@@ -4,75 +4,106 @@ ada-url
4
4
5
5
This is ``ada_url ``, a Python library for parsing and joining URLs.
6
6
7
+ Installation
8
+ ------------
7
9
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/ >`__:
27
11
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
32
13
14
+ pip install boto3-helpers
33
15
34
- Examples
35
- --------
16
+ Usage examples
17
+ --------------
36
18
37
19
This package exposes a ``URL `` class that is intended to match the one described in the
38
20
`WHATWG URL spec <https://url.spec.whatwg.org/#url-class >`__.
39
21
40
22
.. code-block :: python
41
23
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:
44
26
>> > urlobj.host = ' example.com'
45
27
>> > new_url = urlobj.href
46
28
>> > new_url
47
29
' https://example.com/file.txt'
48
30
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:
50
33
51
34
.. code-block :: python
52
35
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' )
55
38
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:[email protected] :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:[email protected] :80/api?q=1#2' )
63
48
{
64
49
' href' : ' https://user:[email protected] :80/api?q=1#2' ,
65
50
' username' : ' user' ,
66
51
' password' : ' pass' ,
67
52
' protocol' : ' https:' ,
68
- ' host' : ' example.org:80' ,
69
53
' port' : ' 80' ,
70
54
' hostname' : ' example.org' ,
55
+ ' host' : ' example.org:80' ,
71
56
' pathname' : ' /api' ,
72
57
' 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 >
74
62
}
63
+
64
+ Replacing URL components:
65
+
66
+ .. code-block :: python
67
+
68
+ >> > from ada_url import replace_url
75
69
>> > ada_url.replace_url(' http://example.org:80' , protocol = ' https:' )
76
70
' https://example.org/'
77
71
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