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 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
Next Next commit
implement design
  • Loading branch information
MaxDall committed Aug 5, 2024
commit 0254ee630d283f6edd326b8d75e2d1b24f06314e
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