|
14 | 14 | ) |
15 | 15 | PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin',) |
16 | 16 |
|
| 17 | +GET_ATTRIBUTES = frozenset(PARSE_ATTRIBUTES) |
| 18 | +SET_ATTRIBUTES = frozenset(URL_ATTRIBUTES) |
| 19 | + |
17 | 20 |
|
18 | 21 | def _get_str(x): |
19 | 22 | ret = ffi.string(x.data, x.length).decode('utf-8') if x.length else '' |
20 | 23 | return ret |
21 | 24 |
|
22 | 25 |
|
| 26 | +class URL: |
| 27 | + """ |
| 28 | + Parses a *url* (with an optional *base*) according to the |
| 29 | + WHATWG URL parsing standard. |
| 30 | +
|
| 31 | + .. code-block:: python |
| 32 | +
|
| 33 | + >>> from ada_url import URL |
| 34 | + >>> old_url = 'https://example.org:443/file.txt?q=1' |
| 35 | + >>> with URL(old_url) as urlobj: |
| 36 | + ... old_host = urlobj.host |
| 37 | + ... urlobj.host = 'example.com' |
| 38 | + ... new_url = urlobj.href |
| 39 | + >>> old_host |
| 40 | + 'example.org' |
| 41 | + >>> new_url |
| 42 | + 'https://example.com:443/file.txt?q=1' |
| 43 | +
|
| 44 | + Note that you should use this class as a context manager to ensure |
| 45 | + that resources are freed. If you use it without a ``with`` |
| 46 | + statement, call the ``close()`` method manually. |
| 47 | +
|
| 48 | + You can read and write the following attributes: |
| 49 | +
|
| 50 | + * ``href`` |
| 51 | + * ``protocol`` |
| 52 | + * ``username`` |
| 53 | + * ``password`` |
| 54 | + * ``host`` |
| 55 | + * ``hostname`` |
| 56 | + * ``port`` |
| 57 | + * ``pathname`` |
| 58 | + * ``search`` |
| 59 | +
|
| 60 | + You can additionally read the ``origin`` attribute. |
| 61 | +
|
| 62 | + The class also exposes a static method that checks whether the input |
| 63 | + *url* (and optional *base*) can be parsed: |
| 64 | +
|
| 65 | + .. code-block:: python |
| 66 | +
|
| 67 | + >>> url = 'https://example.org:443/file_1.txt' |
| 68 | + >>> base = 'file_2.txt' |
| 69 | + >>> URL.can_parse(url, base) |
| 70 | + True |
| 71 | +
|
| 72 | + See the `WHATWG docs <https://url.spec.whatwg.org/#url-class>`__ for |
| 73 | + more details on the URL class. |
| 74 | +
|
| 75 | + """ |
| 76 | + |
| 77 | + def __init__(self, url, base=None): |
| 78 | + url_bytes = url.encode('utf-8') |
| 79 | + |
| 80 | + if base is None: |
| 81 | + self.urlobj = lib.ada_parse(url_bytes, len(url_bytes)) |
| 82 | + else: |
| 83 | + base_bytes = base.encode('utf-8') |
| 84 | + self.urlobj = lib.ada_parse_with_base( |
| 85 | + url_bytes, len(url_bytes), base_bytes, len(base_bytes) |
| 86 | + ) |
| 87 | + |
| 88 | + if not lib.ada_is_valid(self.urlobj): |
| 89 | + raise ValueError('Invlid input') |
| 90 | + |
| 91 | + def __getattr__(self, attr): |
| 92 | + if attr in GET_ATTRIBUTES: |
| 93 | + get_func = getattr(lib, f'ada_get_{attr}') |
| 94 | + data = get_func(self.urlobj) |
| 95 | + ret = _get_str(data) |
| 96 | + if attr == 'origin': |
| 97 | + lib.ada_free_owned_string(data) |
| 98 | + |
| 99 | + return ret |
| 100 | + |
| 101 | + return super().__getattr__(self, attr) |
| 102 | + |
| 103 | + def __setattr__(self, attr, value): |
| 104 | + if attr in SET_ATTRIBUTES: |
| 105 | + try: |
| 106 | + value_bytes = value.encode() |
| 107 | + except Exception: |
| 108 | + raise ValueError(f'Invalid value for {attr}') from None |
| 109 | + |
| 110 | + set_func = getattr(lib, f'ada_set_{attr}') |
| 111 | + ret = set_func(self.urlobj, value_bytes, len(value_bytes)) |
| 112 | + if (ret is not None) and (not ret): |
| 113 | + raise ValueError(f'Invalid value for {attr}') from None |
| 114 | + |
| 115 | + return ret |
| 116 | + |
| 117 | + return super().__setattr__(attr, value) |
| 118 | + |
| 119 | + def close(self): |
| 120 | + lib.ada_free(self.urlobj) |
| 121 | + |
| 122 | + def __enter__(self, *args, **kwargs): |
| 123 | + return self |
| 124 | + |
| 125 | + def __exit__(self, *args, **kwargs): |
| 126 | + self.close() |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def can_parse(url, base=None): |
| 130 | + try: |
| 131 | + url_bytes = url.encode('utf-8') |
| 132 | + except Exception: |
| 133 | + return False |
| 134 | + |
| 135 | + if base is None: |
| 136 | + return lib.ada_can_parse(url_bytes, len(url_bytes)) |
| 137 | + |
| 138 | + try: |
| 139 | + base_bytes = base.encode('utf-8') |
| 140 | + except Exception: |
| 141 | + return False |
| 142 | + |
| 143 | + return lib.ada_can_parse_with_base( |
| 144 | + url_bytes, len(url_bytes), base_bytes, len(base_bytes) |
| 145 | + ) |
| 146 | + |
| 147 | + |
23 | 148 | def check_url(s): |
24 | 149 | """ |
25 | 150 | Returns ``True`` if *s* represents a valid URL, and ``False`` otherwise. |
|
0 commit comments