Skip to content

Commit

Permalink
add meta function to clear caches (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
adbar committed Jul 18, 2022
1 parent 542b422 commit 42644fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
34 changes: 34 additions & 0 deletions htmldate/meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Meta-functions to be applied module-wide.
"""

try:
from charset_normalizer.cd import encoding_languages
from charset_normalizer.md import is_suspiciously_successive_range
from charset_normalizer.utils import is_accentuated
# prevent possible changes in function names
except ImportError:
pass

from .core import compare_reference
from .extractors import try_date_expr
from .settings import MAX_FILE_SIZE, MIN_FILE_SIZE
from .validators import date_validator, filter_ymd_candidate


def reset_caches() -> None:
"""Reset all known LRU caches used to speed-up processing.
This may release some memory."""
# htmldate
compare_reference.cache_clear()
date_validator.cache_clear()
filter_ymd_candidate.cache_clear()
try_date_expr.cache_clear()
# charset_normalizer
try:
encoding_languages.cache_clear()
is_suspiciously_successive_range.cache_clear()
is_accentuated.cache_clear()
# prevent possible changes in function names
except NameError:
pass
10 changes: 10 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
regex_parse,
try_date_expr,
)
from htmldate.meta import reset_caches
from htmldate.settings import MIN_DATE, LATEST_POSSIBLE
from htmldate.utils import (
decode_response,
Expand Down Expand Up @@ -216,6 +217,11 @@ def test_sanity():
mytree, ".//p", OUTPUTFORMAT, True, MIN_DATE, LATEST_POSSIBLE
)
assert result is None
mytree = html.fromstring("<html><body><p>1999/03/05</p></body></html>")
result = examine_date_elements(
mytree, ".//p", OUTPUTFORMAT, True, MIN_DATE, LATEST_POSSIBLE
)
assert result is not None
# wrong field values in output format
assert output_format_validator("%Y-%m-%d") is True
assert output_format_validator("%M-%Y") is True
Expand All @@ -229,6 +235,10 @@ def test_sanity():
)
)
assert len(discarded) == 1
# reset caches: examine_date_elements used above
old_values = try_date_expr.cache_info()
reset_caches()
assert try_date_expr.cache_info() != old_values


def test_no_date():
Expand Down

0 comments on commit 42644fc

Please sign in to comment.