|
1 | | -from typing import Any |
| 1 | +# Allow writing union types as X | Y in Python 3.9 |
| 2 | +from __future__ import annotations |
2 | 3 |
|
3 | | -from parse import Parser |
| 4 | +import re |
| 5 | +from dataclasses import dataclass |
4 | 6 |
|
5 | 7 |
|
6 | | -class PathParameter: |
7 | | - name = "PathParameter" |
8 | | - pattern = r"[^\/]*" |
| 8 | +@dataclass(frozen=True) |
| 9 | +class PathMatchResult: |
| 10 | + """Result of path parsing.""" |
9 | 11 |
|
10 | | - def __call__(self, text: str) -> str: |
11 | | - return text |
| 12 | + named: dict[str, str] |
12 | 13 |
|
13 | 14 |
|
14 | | -class PathParser(Parser): # type: ignore |
| 15 | +class PathParser: |
| 16 | + """Parses path patterns with parameters into regex and matches against URLs.""" |
15 | 17 |
|
16 | | - parse_path_parameter = PathParameter() |
| 18 | + _PARAM_PATTERN = r"[^/]*" |
17 | 19 |
|
18 | 20 | def __init__( |
19 | 21 | self, pattern: str, pre_expression: str = "", post_expression: str = "" |
20 | 22 | ) -> None: |
21 | | - extra_types = { |
22 | | - self.parse_path_parameter.name: self.parse_path_parameter |
23 | | - } |
24 | | - super().__init__(pattern, extra_types) |
25 | | - self._expression: str = ( |
26 | | - pre_expression + self._expression + post_expression |
27 | | - ) |
| 23 | + self.pattern = pattern |
| 24 | + self._group_to_name: dict[str, str] = {} |
| 25 | + |
| 26 | + regex_body = self._compile_template_to_regex(pattern) |
| 27 | + self._expression = f"{pre_expression}{regex_body}{post_expression}" |
| 28 | + self._compiled = re.compile(self._expression) |
| 29 | + |
| 30 | + def search(self, text: str) -> PathMatchResult | None: |
| 31 | + """Searches for a match in the given text.""" |
| 32 | + match = self._compiled.search(text) |
| 33 | + return self._to_result(match) |
| 34 | + |
| 35 | + def parse(self, text: str) -> PathMatchResult | None: |
| 36 | + """Parses the entire text for a match.""" |
| 37 | + match = self._compiled.fullmatch(text) |
| 38 | + return self._to_result(match) |
28 | 39 |
|
29 | | - def _handle_field(self, field: str) -> Any: |
30 | | - # handle as path parameter field |
31 | | - field = field[1:-1] |
32 | | - path_parameter_field = "{%s:PathParameter}" % field |
33 | | - return super()._handle_field(path_parameter_field) |
| 40 | + def _compile_template_to_regex(self, template: str) -> str: |
| 41 | + parts: list[str] = [] |
| 42 | + i = 0 |
| 43 | + group_index = 0 |
| 44 | + while i < len(template): |
| 45 | + start = template.find("{", i) |
| 46 | + if start == -1: |
| 47 | + parts.append(re.escape(template[i:])) |
| 48 | + break |
| 49 | + end = template.find("}", start + 1) |
| 50 | + if end == -1: |
| 51 | + raise ValueError(f"Unmatched '{{' in template: {template!r}") |
| 52 | + |
| 53 | + parts.append(re.escape(template[i:start])) |
| 54 | + param_name = template[start + 1 : end] |
| 55 | + group_name = f"g{group_index}" |
| 56 | + group_index += 1 |
| 57 | + self._group_to_name[group_name] = param_name |
| 58 | + parts.append(f"(?P<{group_name}>{self._PARAM_PATTERN})") |
| 59 | + i = end + 1 |
| 60 | + |
| 61 | + return "".join(parts) |
| 62 | + |
| 63 | + def _to_result( |
| 64 | + self, match: re.Match[str] | None |
| 65 | + ) -> PathMatchResult | None: |
| 66 | + if match is None: |
| 67 | + return None |
| 68 | + return PathMatchResult( |
| 69 | + named={ |
| 70 | + param_name: match.group(group_name) |
| 71 | + for group_name, param_name in self._group_to_name.items() |
| 72 | + }, |
| 73 | + ) |
0 commit comments