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 parse method to ParserProxy #576

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies = [
"tqdm>=4.66, <5",
"fastwarc>=0.14, <1",
"chardet>=5.2, <6",
"dill>=0.3, <1"
"dill>=0.3, <1",
"deprecated>=1.2.14, <2"
]

[project.urls]
Expand All @@ -53,6 +54,7 @@ dev = [
"types-python-dateutil>=2.8, <3",
"types-requests>=2.28, <3",
"types-colorama>=0.4, <1",
"types-deprecated>=1.2.9, <2"
]

[tool.mypy]
Expand Down
16 changes: 16 additions & 0 deletions src/fundus/parser/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import lxml.html
import more_itertools
from deprecated import deprecated
from lxml.etree import XPath

from fundus.logging import create_logger
Expand Down Expand Up @@ -289,7 +290,14 @@ def __init__(self):
mapping[validation_date] = _ParserCache(versioned_parser)
self._parser_mapping = mapping

@deprecated(
version="0.4.1",
reason=f"Calling <class: ParserProxy> is legacy behaviour. Use <method: parse> of <class: ParserProxy> instead",
)
def __call__(self, crawl_date: Optional[Union[datetime, date]] = None) -> BaseParser:
return self.get_parser(crawl_date)

def get_parser(self, crawl_date: Optional[Union[datetime, date]] = None) -> BaseParser:
if crawl_date is None:
return self._get_latest_cache()()

Expand Down Expand Up @@ -343,3 +351,11 @@ def _get_latest_cache(self) -> _ParserCache:
@property
def latest_version(self) -> Type[BaseParser]:
return self._get_latest_cache().factory

def parse(
self,
html: str,
crawl_date: Union[datetime, date],
error_handling: Literal["suppress", "catch", "raise"] = "raise",
) -> Dict[str, Any]:
return self.get_parser(crawl_date).parse(html, error_handling)
2 changes: 1 addition & 1 deletion src/fundus/publishers/base_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def search(
unique_attributes = set(attributes)
spec: Publisher
for publisher in cls:
if unique_attributes.issubset(publisher.parser().attributes().names) and (
if unique_attributes.issubset(publisher.parser.latest_version.attributes().names) and (
publisher.supports(source_types) if source_types else True
):
matched.append(publisher)
Expand Down
3 changes: 2 additions & 1 deletion src/fundus/scraping/scraper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Dict, Iterator, List, Literal, Optional, Type

import more_itertools
Expand Down Expand Up @@ -34,7 +35,7 @@ def scrape(
parser = self.parser_mapping[html.source_info.publisher]

try:
extraction = parser(html.crawl_date).parse(html.content, error_handling)
extraction = parser.parse(html.content, html.crawl_date, error_handling)

except Exception as error:
if error_handling == "raise":
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ def test_latest(self, proxy_with_two_versions_and_different_attrs):
assert parser_proxy.latest_version == parser_proxy.Later

def test_call(self, proxy_with_two_versions_and_different_attrs):
parser_proxy = proxy_with_two_versions_and_different_attrs()
assert type(parser_proxy()) == parser_proxy.latest_version
parser_proxy: ParserProxy = proxy_with_two_versions_and_different_attrs()
assert type(parser_proxy.get_parser()) == parser_proxy.latest_version

for versioned_parser in parser_proxy:
from_proxy = parser_proxy(versioned_parser.VALID_UNTIL)
from_proxy = parser_proxy.get_parser(versioned_parser.VALID_UNTIL)
assert isinstance(from_proxy, versioned_parser)
assert from_proxy == parser_proxy(versioned_parser.VALID_UNTIL)
assert from_proxy == parser_proxy.get_parser(versioned_parser.VALID_UNTIL)

def test_mapping(self, proxy_with_two_versions_and_different_attrs):
parser_proxy = proxy_with_two_versions_and_different_attrs()
Expand Down
2 changes: 1 addition & 1 deletion tests/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def load_html_test_file_mapping(publisher: Publisher) -> Dict[Type[BaseParser],
html_files = [HTMLTestFile.load(path) for path in html_paths]
html_mapping: Dict[Type[BaseParser], HTMLTestFile] = {}
for html_file in html_files:
versioned_parser = publisher.parser(html_file.crawl_date)
versioned_parser = publisher.parser.get_parser(html_file.crawl_date)
if html_mapping.get(type(versioned_parser)):
raise KeyError(f"Duplicate html files for {publisher.name!r} and version {type(versioned_parser).__name__}")
html_mapping[type(versioned_parser)] = html_file
Expand Down