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 article rules. #11

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ The rules can be imported via:

It can then be used to configure the ``DUD_LOAD_RULE_PATHS`` setting of
``duplicate-url-discarder``.

``RULE_PATHS`` contains all files shipped in this package. If you want to
reduce the number of loaded rules to improve performance you can instead
use one or more of the following variables:

* ``RULE_PATHS_COMMON``: rules not specific to any data type.
* ``RULE_PATHS_ARTICLE``: rules for article websites.
* ``RULE_PATHS_PRODUCT``: rules for e-commerce websites.

As all of them are lists, you can combine them (e.g.
``RULE_PATHS_COMMON + RULE_PATHS_PRODUCT``).
19 changes: 19 additions & 0 deletions duplicate_url_discarder_rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,27 @@
from pathlib import Path
from typing import List, Optional

__all__ = [
"RULE_PATHS",
"RULE_PATHS_COMMON",
"RULE_PATHS_ARTICLE",
"RULE_PATHS_PRODUCT",
]

_current_path = Path(__file__).parent.resolve()
RULE_PATHS: Optional[List[str]] = glob(str(_current_path / "**/*.json"), recursive=True)

RULE_PATHS_COMMON: List[str] = []
RULE_PATHS_ARTICLE: List[str] = []
RULE_PATHS_PRODUCT: List[str] = []

for path in RULE_PATHS:
filename = Path(path).name
if filename == "article.json":
RULE_PATHS_ARTICLE.append(path)
elif filename == "product.json":
RULE_PATHS_PRODUCT.append(path)
else:
RULE_PATHS_COMMON.append(path)

__version__ = "2024.10.10"
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from duplicate_url_discarder_rules import RULE_PATHS


def test_query_removal_except_main_rules():
def test_query_removal_except_product_rules():
rule_path = [
path for path in RULE_PATHS if path.endswith("queryRemovalExcept/main.json")
path for path in RULE_PATHS if path.endswith("queryRemovalExcept/product.json")
]
assert len(rule_path) == 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from duplicate_url_discarder_rules import RULE_PATHS


def test_subpath_removal_main_rules():
def test_subpath_removal_product_rules():
rule_path = [
path for path in RULE_PATHS if path.endswith("subpathRemoval/main.json")
path for path in RULE_PATHS if path.endswith("subpathRemoval/product.json")
]
assert len(rule_path) == 1

Expand Down
10 changes: 9 additions & 1 deletion tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from url_matcher import Patterns

from duplicate_url_discarder_rules import RULE_PATHS
from duplicate_url_discarder_rules import RULE_PATHS, RULE_PATHS_ARTICLE, RULE_PATHS_PRODUCT, RULE_PATHS_COMMON


def test_rule_validity():
Expand Down Expand Up @@ -37,3 +37,11 @@ def test_rule_validity():
assert (
json.dumps(data, indent=2, sort_keys=True) == raw_text.rstrip()
), f"Check {path} for formatting issue"


def test_rules_concat():
all_rules = RULE_PATHS_COMMON + RULE_PATHS_ARTICLE + RULE_PATHS_PRODUCT
assert isinstance(all_rules, list)
for path in all_rules:
assert isinstance(path, str)
assert set(all_rules) == set(RULE_PATHS)