urlpattern 0.3.2__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- urlpattern/__init__.py +5 -0
- urlpattern/__init__.pyi +109 -0
- urlpattern/py.typed +0 -0
- urlpattern/urlpattern.cp315-win_amd64.pyd +0 -0
- urlpattern-0.3.2.dist-info/METADATA +192 -0
- urlpattern-0.3.2.dist-info/RECORD +9 -0
- urlpattern-0.3.2.dist-info/WHEEL +4 -0
- urlpattern-0.3.2.dist-info/licenses/LICENSE +21 -0
- urlpattern-0.3.2.dist-info/sboms/urlpattern.cyclonedx.json +1983 -0
urlpattern/__init__.py
ADDED
urlpattern/__init__.pyi
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from typing import Optional, TypedDict, Union, overload
|
|
2
|
+
|
|
3
|
+
from typing_extensions import TypeAlias
|
|
4
|
+
|
|
5
|
+
URLPatternInput: TypeAlias = Union[str, URLPatternInit]
|
|
6
|
+
|
|
7
|
+
class URLPatternOptions(TypedDict, total=False):
|
|
8
|
+
ignoreCase: bool
|
|
9
|
+
|
|
10
|
+
class URLPattern:
|
|
11
|
+
@overload
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
input: str,
|
|
15
|
+
baseURL: str,
|
|
16
|
+
options: URLPatternOptions = {},
|
|
17
|
+
) -> None: ...
|
|
18
|
+
@overload
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
input: str,
|
|
22
|
+
baseURL: str,
|
|
23
|
+
options: None,
|
|
24
|
+
) -> None: ...
|
|
25
|
+
@overload
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
input: URLPatternInit,
|
|
29
|
+
baseURL: str,
|
|
30
|
+
options: URLPatternOptions = {},
|
|
31
|
+
) -> None: ...
|
|
32
|
+
@overload
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
input: URLPatternInit,
|
|
36
|
+
baseURL: str,
|
|
37
|
+
options: None,
|
|
38
|
+
) -> None: ...
|
|
39
|
+
@overload
|
|
40
|
+
def __init__(self, input: str, options: URLPatternOptions = {}) -> None: ...
|
|
41
|
+
@overload
|
|
42
|
+
def __init__(self, input: str, options: None) -> None: ...
|
|
43
|
+
@overload
|
|
44
|
+
def __init__(
|
|
45
|
+
self, input: URLPatternInit = {}, options: URLPatternOptions = {}
|
|
46
|
+
) -> None: ...
|
|
47
|
+
@overload
|
|
48
|
+
def __init__(self, input: URLPatternInit, options: None) -> None: ...
|
|
49
|
+
@overload
|
|
50
|
+
def test(self, input: str, baseURL: Optional[str] = None) -> bool: ...
|
|
51
|
+
@overload
|
|
52
|
+
def test(
|
|
53
|
+
self, input: URLPatternInit = {}, baseURL: Optional[str] = None
|
|
54
|
+
) -> bool: ...
|
|
55
|
+
@overload
|
|
56
|
+
def exec(
|
|
57
|
+
self, input: str, baseURL: Optional[str] = None
|
|
58
|
+
) -> Optional[URLPatternResult]: ...
|
|
59
|
+
@overload
|
|
60
|
+
def exec(
|
|
61
|
+
self, input: URLPatternInit = {}, baseURL: Optional[str] = None
|
|
62
|
+
) -> Optional[URLPatternResult]: ...
|
|
63
|
+
@property
|
|
64
|
+
def protocol(self) -> str: ...
|
|
65
|
+
@property
|
|
66
|
+
def username(self) -> str: ...
|
|
67
|
+
@property
|
|
68
|
+
def password(self) -> str: ...
|
|
69
|
+
@property
|
|
70
|
+
def hostname(self) -> str: ...
|
|
71
|
+
@property
|
|
72
|
+
def port(self) -> str: ...
|
|
73
|
+
@property
|
|
74
|
+
def pathname(self) -> str: ...
|
|
75
|
+
@property
|
|
76
|
+
def search(self) -> str: ...
|
|
77
|
+
@property
|
|
78
|
+
def hash(self) -> str: ...
|
|
79
|
+
@property
|
|
80
|
+
def hasRegExpGroups(self) -> bool: ...
|
|
81
|
+
|
|
82
|
+
class URLPatternInit(TypedDict, total=False):
|
|
83
|
+
protocol: str
|
|
84
|
+
username: str
|
|
85
|
+
password: str
|
|
86
|
+
hostname: str
|
|
87
|
+
port: str
|
|
88
|
+
pathname: str
|
|
89
|
+
search: str
|
|
90
|
+
hash: str
|
|
91
|
+
baseURL: str
|
|
92
|
+
|
|
93
|
+
class URLPatternResult(TypedDict):
|
|
94
|
+
inputs: list[URLPatternInput]
|
|
95
|
+
|
|
96
|
+
protocol: URLPatternComponentResult
|
|
97
|
+
username: URLPatternComponentResult
|
|
98
|
+
password: URLPatternComponentResult
|
|
99
|
+
hostname: URLPatternComponentResult
|
|
100
|
+
port: URLPatternComponentResult
|
|
101
|
+
pathname: URLPatternComponentResult
|
|
102
|
+
search: URLPatternComponentResult
|
|
103
|
+
hash: URLPatternComponentResult
|
|
104
|
+
|
|
105
|
+
class URLPatternComponentResult(TypedDict):
|
|
106
|
+
input: str
|
|
107
|
+
groups: dict[str, Optional[str]]
|
|
108
|
+
|
|
109
|
+
URLPatternCompatible: TypeAlias = Union[str, URLPatternInit, URLPattern]
|
urlpattern/py.typed
ADDED
|
File without changes
|
|
Binary file
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: urlpattern
|
|
3
|
+
Version: 0.3.2
|
|
4
|
+
Classifier: Development Status :: 4 - Beta
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
17
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
18
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
19
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
20
|
+
Classifier: Programming Language :: Rust
|
|
21
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Summary: An implementation of the URL Pattern Standard for Python written in Rust.
|
|
25
|
+
Keywords: urlpattern
|
|
26
|
+
Author-email: "방성범 (Bang Seongbeom)" <bangseongbeom@gmail.com>
|
|
27
|
+
Requires-Python: >=3.9
|
|
28
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
29
|
+
Project-URL: Homepage, https://github.com/bangseongbeom/python-urlpattern
|
|
30
|
+
Project-URL: Issues, https://github.com/bangseongbeom/python-urlpattern/issues
|
|
31
|
+
Project-URL: Repository, https://github.com/bangseongbeom/python-urlpattern.git
|
|
32
|
+
|
|
33
|
+
# urlpattern
|
|
34
|
+
|
|
35
|
+
[](https://pypi.org/project/urlpattern/)
|
|
36
|
+
[](https://pypi.org/project/urlpattern/)
|
|
37
|
+
[](https://github.com/astral-sh/ruff)
|
|
38
|
+
[](https://github.com/bangseongbeom/python-urlpattern/actions/workflows/CI.yml)
|
|
39
|
+
|
|
40
|
+
An implementation of [the URL Pattern Standard](https://urlpattern.spec.whatwg.org/) for Python written in Rust.
|
|
41
|
+
|
|
42
|
+
## Introduction
|
|
43
|
+
|
|
44
|
+
It's a thin wrapper of [denoland/rust-urlpattern](https://github.com/denoland/rust-urlpattern) with [PyO3](https://github.com/PyO3/pyo3) + [Maturin](https://github.com/PyO3/maturin).
|
|
45
|
+
|
|
46
|
+
It is useful on the server side when serving different pages based on the URL (a.k.a. routing). It provides pattern matching syntax like `/users/:id`, similar to [route parameters in Express](https://expressjs.com/en/guide/routing.html#route-parameters) or [Path-to-RegExp](https://github.com/pillarjs/path-to-regexp). You can use it as a foundation to build your own web server or framework.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
On Linux/UNIX or macOS:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install urlpattern
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
On Windows:
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
py -m pip install urlpattern
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
This library aims to expose an interface as close as possible to the URL Pattern Standard, but some differences are unavoidable because it is designed for Python, not JavaScript. For the exact details, please refer to [urlpattern.pyi](https://github.com/bangseongbeom/python-urlpattern/blob/main/urlpattern.pyi).
|
|
65
|
+
|
|
66
|
+
Most JavaScript examples from [Chrome for Developers](https://developer.chrome.com/docs/web-platform/urlpattern) and [MDN](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API) can be adapted to Python without much difficulty.
|
|
67
|
+
|
|
68
|
+
### `test`
|
|
69
|
+
|
|
70
|
+
```py
|
|
71
|
+
from urlpattern import URLPattern
|
|
72
|
+
|
|
73
|
+
pattern = URLPattern("https://example.com/admin/*")
|
|
74
|
+
print(pattern.test("https://example.com/admin/main/"))
|
|
75
|
+
print(pattern.test("https://example.com/main/"))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Output:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
True
|
|
82
|
+
False
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### `exec`
|
|
86
|
+
|
|
87
|
+
```py
|
|
88
|
+
from urlpattern import URLPattern
|
|
89
|
+
|
|
90
|
+
pattern = URLPattern({"pathname": "/users/:id/"})
|
|
91
|
+
result = pattern.exec({"pathname": "/users/4163/"})
|
|
92
|
+
print(result["pathname"]["groups"]["id"])
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Output:
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
4163
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### `baseURL`
|
|
102
|
+
|
|
103
|
+
```py
|
|
104
|
+
from urlpattern import URLPattern
|
|
105
|
+
|
|
106
|
+
pattern = URLPattern("b", "https://example.com/a/")
|
|
107
|
+
print(pattern.test("a/b", "https://example.com/"))
|
|
108
|
+
print(pattern.test("b", "https://example.com/a/"))
|
|
109
|
+
print(
|
|
110
|
+
pattern.test({"pathname": "b", "baseURL": "https://example.com/a/"})
|
|
111
|
+
)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Output:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
True
|
|
118
|
+
True
|
|
119
|
+
True
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### `ignoreCase`
|
|
123
|
+
|
|
124
|
+
```py
|
|
125
|
+
from urlpattern import URLPattern
|
|
126
|
+
|
|
127
|
+
pattern = URLPattern("https://example.com/test")
|
|
128
|
+
print(pattern.test("https://example.com/test"))
|
|
129
|
+
print(pattern.test("https://example.com/TeST"))
|
|
130
|
+
|
|
131
|
+
pattern = URLPattern("https://example.com/test", {"ignoreCase": True})
|
|
132
|
+
print(pattern.test("https://example.com/test"))
|
|
133
|
+
print(pattern.test("https://example.com/TeST"))
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Output:
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
True
|
|
140
|
+
False
|
|
141
|
+
True
|
|
142
|
+
True
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### A simple WSGI app
|
|
146
|
+
|
|
147
|
+
```py
|
|
148
|
+
from wsgiref.simple_server import make_server
|
|
149
|
+
|
|
150
|
+
from urlpattern import URLPattern
|
|
151
|
+
|
|
152
|
+
user_id_pattern = URLPattern({"pathname": "/users/:id"})
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def get_user_id(environ, start_response):
|
|
156
|
+
user_id = environ["result"]["pathname"]["groups"]["id"]
|
|
157
|
+
status = "200 OK"
|
|
158
|
+
response_headers = [("Content-type", "text/plain; charset=utf-8")]
|
|
159
|
+
start_response(status, response_headers)
|
|
160
|
+
return [f"{user_id=}".encode()]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def app(environ, start_response):
|
|
164
|
+
path = environ["PATH_INFO"]
|
|
165
|
+
method = environ["REQUEST_METHOD"]
|
|
166
|
+
|
|
167
|
+
if result := user_id_pattern.exec({"pathname": path}):
|
|
168
|
+
if method == "GET":
|
|
169
|
+
return get_user_id(environ | {"result": result}, start_response)
|
|
170
|
+
|
|
171
|
+
status = "404 Not Found"
|
|
172
|
+
response_headers = [("Content-type", "text/plain; charset=utf-8")]
|
|
173
|
+
start_response(status, response_headers)
|
|
174
|
+
return [b"Not Found"]
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
with make_server("", 8000, app) as httpd:
|
|
178
|
+
httpd.serve_forever()
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Limitations
|
|
182
|
+
|
|
183
|
+
Due to limitations in the dependency [denoland/rust-urlpattern](https://github.com/denoland/rust-urlpattern), it may not support all features specified in [the standard](https://urlpattern.spec.whatwg.org/).
|
|
184
|
+
|
|
185
|
+
Check `pytest.skip` in [`tests/test_lib.py`](https://github.com/bangseongbeom/python-urlpattern/blob/main/tests/test_lib.py).
|
|
186
|
+
|
|
187
|
+
## Why camelCase?
|
|
188
|
+
|
|
189
|
+
As seen in names like `baseURL` and `hasRegExpGroups`, this library does not follow Python's [PEP 8 naming conventions](https://peps.python.org/pep-0008/#function-and-variable-names). Instead, it follows [the standard](https://urlpattern.spec.whatwg.org/) naming as closely as possible.
|
|
190
|
+
|
|
191
|
+
Like [`xml.dom`](https://docs.python.org/3/library/xml.dom.html), Python wrappers around web standards typically preserve the original camelCase rather than converting names to snake_case, and this library follows that convention as well.
|
|
192
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
urlpattern/__init__.py,sha256=Yh9wWY3jN3l1xnfDMxdJFCrv7hLaidjhrUcnDS71DIU,123
|
|
2
|
+
urlpattern/__init__.pyi,sha256=RR0qqqJ4A3da6V29oFDcYpN0jKU_srjCitpzZmQOFIA,2979
|
|
3
|
+
urlpattern/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
urlpattern/urlpattern.cp315-win_amd64.pyd,sha256=oNz0FfnforZMcAgUevKJ2hOu7ebluqKg5pqKwPJiyHs,2385408
|
|
5
|
+
urlpattern-0.3.2.dist-info/METADATA,sha256=mXANIrEhe42N0ZltruBWIMU07HHSVKs8wVy3QQc2VhQ,6731
|
|
6
|
+
urlpattern-0.3.2.dist-info/WHEEL,sha256=Q5QdGvjBfn8x6ihg-B4VQuuGKk7DT67L9DVrwEH_qj8,97
|
|
7
|
+
urlpattern-0.3.2.dist-info/licenses/LICENSE,sha256=_qapitbsCkJk_1XOkLpKwq-BiN5jH65q65PF4jnvJhg,1102
|
|
8
|
+
urlpattern-0.3.2.dist-info/sboms/urlpattern.cyclonedx.json,sha256=4macG5mOCIf4xhGPGM6Py8J9nV57H0yTcz041dQpRa0,62480
|
|
9
|
+
urlpattern-0.3.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 방성범 (Bang Seongbeom)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|