Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag for skipping universal patterns. #16

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,16 @@ def test_match_all():
assert list(matcher.match_all("http://example.com/products")) == [1]
assert list(matcher.match_all("http://foo.example.com/products")) == [2, 1]
assert list(matcher.match_all("http://bar.example.com/products")) == [3, 4, 1]


def test_include_universal():
matcher = URLMatcher()
matcher.add_or_update(1, Patterns(include=["example.com"]))
matcher.add_or_update(2, Patterns(include=[]))
wRAR marked this conversation as resolved.
Show resolved Hide resolved
matcher.add_or_update(3, Patterns(include=["foo.example.com"]))
assert list(matcher.match_all("http://example.com")) == [1, 2]
assert list(matcher.match_all("http://example.com", include_universal=False)) == [1]
assert list(matcher.match_all("http://foo.example.com")) == [3, 1, 2]
assert list(matcher.match_all("http://foo.example.com", include_universal=False)) == [3, 1]
assert list(matcher.match_all("http://example.net")) == [2]
assert list(matcher.match_all("http://example.net", include_universal=False)) == [2]
17 changes: 12 additions & 5 deletions url_matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

from dataclasses import dataclass, field
from itertools import chain
from typing import Any, Dict, Iterable, Iterator, List, Mapping, Optional, Tuple, Union

from url_matcher.patterns import PatternMatcher, get_pattern_domain, hierarchical_str
Expand Down Expand Up @@ -151,14 +150,22 @@ def remove(self, identifier: Any):
def get(self, identifier: Any) -> Optional[Patterns]:
return self.patterns.get(identifier)

def match(self, url: str) -> Optional[Any]:
return next(self.match_all(url), None)
def match(self, url: str, *, include_universal=True) -> Optional[Any]:
return next(self.match_all(url, include_universal=include_universal), None)

def match_all(self, url: str) -> Iterator[Any]:
def match_all(self, url: str, *, include_universal=True) -> Iterator[Any]:
BurnzZ marked this conversation as resolved.
Show resolved Hide resolved
domain = get_domain(url)
for matcher in chain(self.matchers_by_domain.get(domain) or [], self.matchers_by_domain.get("") or []):
domain_matchers = self.matchers_by_domain.get(domain) or []
domain_match = False
for matcher in domain_matchers:
if matcher.match(url):
domain_match = True
yield matcher.identifier
if include_universal or not domain_match:
universal_matchers = self.matchers_by_domain.get("") or []
for matcher in universal_matchers:
if matcher.match(url):
yield matcher.identifier

def _sort_domain(self, domain: str):
"""
Expand Down