2024-01-17 18:08:30 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-17 18:54:27 -06:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2024-01-17 18:08:30 -06:00
|
|
|
import pytest
|
|
|
|
from html5lib import HTMLParser
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2024-03-16 12:06:58 -05:00
|
|
|
from collections.abc import Callable, Generator
|
2024-01-17 18:08:30 -06:00
|
|
|
from pathlib import Path
|
2024-03-16 12:06:58 -05:00
|
|
|
from xml.etree.ElementTree import Element
|
2024-01-17 18:08:30 -06:00
|
|
|
|
2024-03-16 12:06:58 -05:00
|
|
|
etree_cache: dict[Path, Element] = {}
|
2024-01-17 18:08:30 -06:00
|
|
|
|
|
|
|
|
2024-03-16 12:06:58 -05:00
|
|
|
def _parse(fname: Path) -> Element:
|
2024-01-17 18:08:30 -06:00
|
|
|
if fname in etree_cache:
|
|
|
|
return etree_cache[fname]
|
|
|
|
with fname.open('rb') as fp:
|
|
|
|
etree = HTMLParser(namespaceHTMLElements=False).parse(fp)
|
|
|
|
etree_cache[fname] = etree
|
|
|
|
return etree
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='package')
|
2024-03-16 12:06:58 -05:00
|
|
|
def cached_etree_parse() -> Generator[Callable[[Path], Element], None, None]:
|
2024-01-17 18:08:30 -06:00
|
|
|
yield _parse
|
|
|
|
etree_cache.clear()
|