Skip to content

Commit

Permalink
Add scheme_type for ada 2.6.7 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles authored Sep 6, 2023
1 parent 3f2619e commit ff4c758
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 23 deletions.
6 changes: 4 additions & 2 deletions ada_url/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ada_url.ada_adapter import (
URL,
HostType,
SchemeType,
URL,
check_url,
idna,
idna_to_ascii,
Expand All @@ -12,8 +13,9 @@
)

__all__ = [
'URL',
'HostType',
'SchemeType',
'URL',
'check_url',
'idna',
'idna_to_ascii',
Expand Down
10 changes: 9 additions & 1 deletion ada_url/ada.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* auto-generated on 2023-08-30 11:44:21 -0400. Do not edit! */
/* auto-generated on 2023-09-05 16:55:45 -0400. Do not edit! */
/* begin file src/ada.cpp */
#include "ada.h"
/* begin file src/checkers.cpp */
Expand Down Expand Up @@ -15009,6 +15009,14 @@ uint8_t ada_get_host_type(ada_url result) noexcept {
return r->host_type;
}

uint8_t ada_get_scheme_type(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (!r) {
return 0;
}
return r->type;
}

bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (!r) {
Expand Down
6 changes: 3 additions & 3 deletions ada_url/ada.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* auto-generated on 2023-08-30 11:44:21 -0400. Do not edit! */
/* auto-generated on 2023-09-05 16:55:45 -0400. Do not edit! */
/* begin file include/ada.h */
/**
* @file ada.h
Expand Down Expand Up @@ -6926,14 +6926,14 @@ inline void url_search_params::sort() {
#ifndef ADA_ADA_VERSION_H
#define ADA_ADA_VERSION_H

#define ADA_VERSION "2.6.5"
#define ADA_VERSION "2.6.7"

namespace ada {

enum {
ADA_VERSION_MAJOR = 2,
ADA_VERSION_MINOR = 6,
ADA_VERSION_REVISION = 5,
ADA_VERSION_REVISION = 7,
};

} // namespace ada
Expand Down
53 changes: 44 additions & 9 deletions ada_url/ada_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'search',
'hash',
)
PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin', 'host_type')
PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin', 'host_type', 'scheme_type')

# These are the attributes that have corresponding ada_get_* functions
GET_ATTRIBUTES = frozenset(PARSE_ATTRIBUTES)
Expand Down Expand Up @@ -45,10 +45,6 @@ class HostType(IntEnum):
>>> from ada_url import HostType
>>> HostType.DEFAULT
<HostType.DEFAULT: 0>
>>> HostType.IPV4
<HostType.IPV4: 1>
>>> HostType.IPV6
<HostType.IPV6: 2>
"""

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


class SchemeType(IntEnum):
"""
Enum for `URL scheme types <https://url.spec.whatwg.org/#url-miscellaneous>`__.
* ``HTTP`` URLs like ``http://example.org`` are ``0``.
* ``NOT_SPECIAL`` URLs like ``git://example.og`` are ``1``.
* ``HTTPS`` URLs like ``https://example.org`` are ``2``.
* ``WS`` URLs like ``ws://example.org`` are ``3``.
* ``FTP`` URLs like ``ftp://example.org`` are ``4``.
* ``WSS`` URLs like ``wss://example.org`` are ``5``.
* ``FILE`` URLs like ``file://example`` are ``6``.
.. code-block:: python
>>> from ada_url import SchemeType
>>> SchemeType.HTTPS
<SchemeType.HTTPS: 2>
"""

HTTP = 0
NOT_SPECIAL = 1
HTTPS = 2
WS = 3
FTP = 4
WSS = 5
FILE = 6


class ParseAttributes(TypedDict, total=False):
href: str
username: str
Expand All @@ -70,6 +95,7 @@ class ParseAttributes(TypedDict, total=False):
hash: str
origin: str
host_type: HostType
scheme_type: SchemeType


def _get_obj(constructor, destructor, *args):
Expand Down Expand Up @@ -113,8 +139,10 @@ class URL:
* ``search``
* ``hash``
You can additionally read the ``origin`` and ``host_type`` attributes.
``host_type`` is a :class:`HostType` enum.
You can additionally read these attributes:
* ``origin``, which will be a ``str``
* ``host_type``, which will be a :class:`HostType` enum
* ``scheme_type``, which will be a :class:`SchemeType` enum
The class also exposes a static method that checks whether the input
*url* (and optional *base*) can be parsed:
Expand Down Expand Up @@ -143,6 +171,7 @@ class URL:
hash: str
origin: Final[str]
host_type: Final[HostType]
scheme_type: Final[SchemeType]

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

def __getattr__(self, attr: str) -> Union[str, HostType]:
def __getattr__(self, attr: str) -> Union[str, HostType, SchemeType]:
if attr in GET_ATTRIBUTES:
get_func = getattr(lib, f'ada_get_{attr}')
data = get_func(self.urlobj)
if attr == 'origin':
ret = _get_str(data)
lib.ada_free_owned_string(data)
elif attr == 'host_type':
ret = data
ret = HostType(data)
elif attr == 'scheme_type':
ret = SchemeType(data)
else:
ret = _get_str(data)

Expand Down Expand Up @@ -342,11 +373,13 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr
'hash': '#frag'
'origin': 'https://example.org:8080',
'host_type': 0
'scheme_type': 2
}
The names of the dictionary keys correspond to the components of the "URL class"
in the WHATWG URL spec.
``host_type`` is a :class:`HostType` enum.
``scheme_type`` is a :class:`SchemeType` enum.
Pass in a sequence of *attributes* to limit which keys are returned.
Expand Down Expand Up @@ -378,6 +411,8 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr
lib.ada_free_owned_string(data)
elif attr == 'host_type':
ret[attr] = HostType(data)
elif attr == 'scheme_type':
ret[attr] = SchemeType(data)
else:
ret[attr] = _get_str(data)

Expand Down
1 change: 1 addition & 0 deletions ada_url/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ ada_string ada_get_pathname(ada_url result);
ada_string ada_get_search(ada_url result);
ada_string ada_get_protocol(ada_url result);
uint8_t ada_get_host_type(ada_url result);
uint8_t ada_get_scheme_type(ada_url result);

// url_aggregator setters
// if ada_is_valid(result)) is false, the setters have no effect
Expand Down
15 changes: 8 additions & 7 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ API Documentation

.. automodule:: ada_url

.. autoclass:: URL
.. autoclass:: URL(url, base=None)
.. autoclass:: HostType()
.. autofunction:: check_url
.. autofunction:: join_url
.. autofunction:: normalize_url
.. autofunction:: parse_url
.. autofunction:: replace_url
.. autofunction:: idna
.. autoclass:: SchemeType()
.. autofunction:: check_url(s)
.. autofunction:: join_url(base_url, s)
.. autofunction:: normalize_url(s)
.. autofunction:: parse_url(s, [attributes])
.. autofunction:: replace_url(s, **kwargs)
.. autoclass:: idna

20 changes: 19 additions & 1 deletion tests/test_ada_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ada_url import (
HostType,
SchemeType,
URL,
check_url,
idna,
Expand Down Expand Up @@ -49,6 +50,22 @@ def test_class_host_type(self):
self.assertEqual(urlobj.host_type, int(expected))
self.assertEqual(urlobj.host_type, expected)

def test_class_scheme_type(self):
# host_type should return an IntEnum, which can be compared to a Python int
for url, expected in (
('http://localhost', SchemeType.HTTP),
('git://localhost', SchemeType.NOT_SPECIAL),
('https://localhost', SchemeType.HTTPS),
('ws://localhost', SchemeType.WS),
('ftp://localhost', SchemeType.FTP),
('wss://localhost', SchemeType.WSS),
('file://localhost', SchemeType.FILE),
):
with self.subTest(url=url):
urlobj = URL(url)
self.assertEqual(urlobj.scheme_type, int(expected))
self.assertEqual(urlobj.scheme_type, expected)

def test_copy_vs_deepcopy(self):
obj = URL('http://example.org:8080')
copied_obj = copy(obj)
Expand Down Expand Up @@ -291,7 +308,8 @@ def test_parse_url(self):
'search': '?q=1',
'hash': '#frag',
'origin': 'https://example.org:8080',
'host_type': 0,
'host_type': HostType(0),
'scheme_type': SchemeType(2),
}
self.assertEqual(actual, expected)

Expand Down

0 comments on commit ff4c758

Please sign in to comment.