API Reference
Module url_matcher
- class Patterns(include: 'list[str]', exclude: 'list[str] | None' = None, priority: 'int' = 500)[source]
- class URLMatcher(data: Mapping[Any, Patterns] | Iterable[tuple[Any, Patterns]] | None = None)[source]
- __init__(data: Mapping[Any, Patterns] | Iterable[tuple[Any, Patterns]] | None = None)[source]
A class that matches URLs against a list of patterns, returning the identifier of the rule that matched the URL.
Example usage:
matcher = URLMatcher() matcher.add_or_update(1, Patterns(include=["example.com/product"])) matcher.add_or_update(2, Patterns(include=["other.com"])) assert matcher.match("http://example.com/product/a_product.html") == 1 assert matcher.match("http://other.com/a_different_page") == 2
- Parameters:
data – A map or a list of tuples with identifier, patterns pairs to initialize the object from
- match(url: str, *, include_universal: bool = True) Any | None[source]
Return the identifier of the rule that matches url, or
Noneif no rule matches it.When several rules match, the one that wins according to the conflict resolution criteria is returned.
If include_universal is false, rules with a universal include pattern are ignored.
- match_all(url: str, *, include_universal: bool = True) Iterator[Any][source]
Yield the identifier of every rule that matches url, best match first according to the conflict resolution criteria.
If include_universal is false, rules with a universal include pattern are ignored.
- match_universal() Iterator[Any][source]
Yield the identifier of every rule with a universal include pattern, best match first according to the conflict resolution criteria.