Skip to content

Commit ff4c758

Browse files
authored
Add scheme_type for ada 2.6.7 (#29)
1 parent 3f2619e commit ff4c758

File tree

7 files changed

+88
-23
lines changed

7 files changed

+88
-23
lines changed

ada_url/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ada_url.ada_adapter import (
2-
URL,
32
HostType,
3+
SchemeType,
4+
URL,
45
check_url,
56
idna,
67
idna_to_ascii,
@@ -12,8 +13,9 @@
1213
)
1314

1415
__all__ = [
15-
'URL',
1616
'HostType',
17+
'SchemeType',
18+
'URL',
1719
'check_url',
1820
'idna',
1921
'idna_to_ascii',

ada_url/ada.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on 2023-08-30 11:44:21 -0400. Do not edit! */
1+
/* auto-generated on 2023-09-05 16:55:45 -0400. Do not edit! */
22
/* begin file src/ada.cpp */
33
#include "ada.h"
44
/* begin file src/checkers.cpp */
@@ -15009,6 +15009,14 @@ uint8_t ada_get_host_type(ada_url result) noexcept {
1500915009
return r->host_type;
1501015010
}
1501115011

15012+
uint8_t ada_get_scheme_type(ada_url result) noexcept {
15013+
ada::result<ada::url_aggregator>& r = get_instance(result);
15014+
if (!r) {
15015+
return 0;
15016+
}
15017+
return r->type;
15018+
}
15019+
1501215020
bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
1501315021
ada::result<ada::url_aggregator>& r = get_instance(result);
1501415022
if (!r) {

ada_url/ada.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on 2023-08-30 11:44:21 -0400. Do not edit! */
1+
/* auto-generated on 2023-09-05 16:55:45 -0400. Do not edit! */
22
/* begin file include/ada.h */
33
/**
44
* @file ada.h
@@ -6926,14 +6926,14 @@ inline void url_search_params::sort() {
69266926
#ifndef ADA_ADA_VERSION_H
69276927
#define ADA_ADA_VERSION_H
69286928

6929-
#define ADA_VERSION "2.6.5"
6929+
#define ADA_VERSION "2.6.7"
69306930

69316931
namespace ada {
69326932

69336933
enum {
69346934
ADA_VERSION_MAJOR = 2,
69356935
ADA_VERSION_MINOR = 6,
6936-
ADA_VERSION_REVISION = 5,
6936+
ADA_VERSION_REVISION = 7,
69376937
};
69386938

69396939
} // namespace ada

ada_url/ada_adapter.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'search',
1616
'hash',
1717
)
18-
PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin', 'host_type')
18+
PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin', 'host_type', 'scheme_type')
1919

2020
# These are the attributes that have corresponding ada_get_* functions
2121
GET_ATTRIBUTES = frozenset(PARSE_ATTRIBUTES)
@@ -45,10 +45,6 @@ class HostType(IntEnum):
4545
>>> from ada_url import HostType
4646
>>> HostType.DEFAULT
4747
<HostType.DEFAULT: 0>
48-
>>> HostType.IPV4
49-
<HostType.IPV4: 1>
50-
>>> HostType.IPV6
51-
<HostType.IPV6: 2>
5248
5349
"""
5450

@@ -57,6 +53,35 @@ class HostType(IntEnum):
5753
IPV6 = 2
5854

5955

56+
class SchemeType(IntEnum):
57+
"""
58+
Enum for `URL scheme types <https://url.spec.whatwg.org/#url-miscellaneous>`__.
59+
60+
* ``HTTP`` URLs like ``http://example.org`` are ``0``.
61+
* ``NOT_SPECIAL`` URLs like ``git://example.og`` are ``1``.
62+
* ``HTTPS`` URLs like ``https://example.org`` are ``2``.
63+
* ``WS`` URLs like ``ws://example.org`` are ``3``.
64+
* ``FTP`` URLs like ``ftp://example.org`` are ``4``.
65+
* ``WSS`` URLs like ``wss://example.org`` are ``5``.
66+
* ``FILE`` URLs like ``file://example`` are ``6``.
67+
68+
.. code-block:: python
69+
70+
>>> from ada_url import SchemeType
71+
>>> SchemeType.HTTPS
72+
<SchemeType.HTTPS: 2>
73+
74+
"""
75+
76+
HTTP = 0
77+
NOT_SPECIAL = 1
78+
HTTPS = 2
79+
WS = 3
80+
FTP = 4
81+
WSS = 5
82+
FILE = 6
83+
84+
6085
class ParseAttributes(TypedDict, total=False):
6186
href: str
6287
username: str
@@ -70,6 +95,7 @@ class ParseAttributes(TypedDict, total=False):
7095
hash: str
7196
origin: str
7297
host_type: HostType
98+
scheme_type: SchemeType
7399

74100

75101
def _get_obj(constructor, destructor, *args):
@@ -113,8 +139,10 @@ class URL:
113139
* ``search``
114140
* ``hash``
115141
116-
You can additionally read the ``origin`` and ``host_type`` attributes.
117-
``host_type`` is a :class:`HostType` enum.
142+
You can additionally read these attributes:
143+
* ``origin``, which will be a ``str``
144+
* ``host_type``, which will be a :class:`HostType` enum
145+
* ``scheme_type``, which will be a :class:`SchemeType` enum
118146
119147
The class also exposes a static method that checks whether the input
120148
*url* (and optional *base*) can be parsed:
@@ -143,6 +171,7 @@ class URL:
143171
hash: str
144172
origin: Final[str]
145173
host_type: Final[HostType]
174+
scheme_type: Final[SchemeType]
146175

147176
def __init__(self, url: str, base: Optional[str] = None):
148177
url_bytes = url.encode('utf-8')
@@ -193,15 +222,17 @@ def __delattr__(self, attr: str):
193222
def __dir__(self) -> List[str]:
194223
return super().__dir__() + list(PARSE_ATTRIBUTES)
195224

196-
def __getattr__(self, attr: str) -> Union[str, HostType]:
225+
def __getattr__(self, attr: str) -> Union[str, HostType, SchemeType]:
197226
if attr in GET_ATTRIBUTES:
198227
get_func = getattr(lib, f'ada_get_{attr}')
199228
data = get_func(self.urlobj)
200229
if attr == 'origin':
201230
ret = _get_str(data)
202231
lib.ada_free_owned_string(data)
203232
elif attr == 'host_type':
204-
ret = data
233+
ret = HostType(data)
234+
elif attr == 'scheme_type':
235+
ret = SchemeType(data)
205236
else:
206237
ret = _get_str(data)
207238

@@ -342,11 +373,13 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr
342373
'hash': '#frag'
343374
'origin': 'https://example.org:8080',
344375
'host_type': 0
376+
'scheme_type': 2
345377
}
346378
347379
The names of the dictionary keys correspond to the components of the "URL class"
348380
in the WHATWG URL spec.
349381
``host_type`` is a :class:`HostType` enum.
382+
``scheme_type`` is a :class:`SchemeType` enum.
350383
351384
Pass in a sequence of *attributes* to limit which keys are returned.
352385
@@ -378,6 +411,8 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr
378411
lib.ada_free_owned_string(data)
379412
elif attr == 'host_type':
380413
ret[attr] = HostType(data)
414+
elif attr == 'scheme_type':
415+
ret[attr] = SchemeType(data)
381416
else:
382417
ret[attr] = _get_str(data)
383418

ada_url/ada_c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ ada_string ada_get_pathname(ada_url result);
6969
ada_string ada_get_search(ada_url result);
7070
ada_string ada_get_protocol(ada_url result);
7171
uint8_t ada_get_host_type(ada_url result);
72+
uint8_t ada_get_scheme_type(ada_url result);
7273

7374
// url_aggregator setters
7475
// if ada_is_valid(result)) is false, the setters have no effect

docs/index.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ API Documentation
5050

5151
.. automodule:: ada_url
5252

53-
.. autoclass:: URL
53+
.. autoclass:: URL(url, base=None)
5454
.. autoclass:: HostType()
55-
.. autofunction:: check_url
56-
.. autofunction:: join_url
57-
.. autofunction:: normalize_url
58-
.. autofunction:: parse_url
59-
.. autofunction:: replace_url
60-
.. autofunction:: idna
55+
.. autoclass:: SchemeType()
56+
.. autofunction:: check_url(s)
57+
.. autofunction:: join_url(base_url, s)
58+
.. autofunction:: normalize_url(s)
59+
.. autofunction:: parse_url(s, [attributes])
60+
.. autofunction:: replace_url(s, **kwargs)
61+
.. autoclass:: idna
6162

tests/test_ada_url.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from ada_url import (
55
HostType,
6+
SchemeType,
67
URL,
78
check_url,
89
idna,
@@ -49,6 +50,22 @@ def test_class_host_type(self):
4950
self.assertEqual(urlobj.host_type, int(expected))
5051
self.assertEqual(urlobj.host_type, expected)
5152

53+
def test_class_scheme_type(self):
54+
# host_type should return an IntEnum, which can be compared to a Python int
55+
for url, expected in (
56+
('http://localhost', SchemeType.HTTP),
57+
('git://localhost', SchemeType.NOT_SPECIAL),
58+
('https://localhost', SchemeType.HTTPS),
59+
('ws://localhost', SchemeType.WS),
60+
('ftp://localhost', SchemeType.FTP),
61+
('wss://localhost', SchemeType.WSS),
62+
('file://localhost', SchemeType.FILE),
63+
):
64+
with self.subTest(url=url):
65+
urlobj = URL(url)
66+
self.assertEqual(urlobj.scheme_type, int(expected))
67+
self.assertEqual(urlobj.scheme_type, expected)
68+
5269
def test_copy_vs_deepcopy(self):
5370
obj = URL('http://example.org:8080')
5471
copied_obj = copy(obj)
@@ -291,7 +308,8 @@ def test_parse_url(self):
291308
'search': '?q=1',
292309
'hash': '#frag',
293310
'origin': 'https://example.org:8080',
294-
'host_type': 0,
311+
'host_type': HostType(0),
312+
'scheme_type': SchemeType(2),
295313
}
296314
self.assertEqual(actual, expected)
297315

0 commit comments

Comments
 (0)