feat: enhance production readiness with environment configuration, dependency checks, and sitemap updates
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""HTML to PDF conversion service."""
|
||||
import os
|
||||
import logging
|
||||
from importlib.metadata import PackageNotFoundError, version
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -10,6 +11,54 @@ class HtmlToPdfError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _parse_version_parts(raw_version: str | None) -> tuple[int, ...]:
|
||||
"""Parse a package version into comparable integer parts."""
|
||||
if not raw_version:
|
||||
return ()
|
||||
|
||||
parts: list[int] = []
|
||||
for token in raw_version.replace("-", ".").split("."):
|
||||
digits = "".join(ch for ch in token if ch.isdigit())
|
||||
if not digits:
|
||||
break
|
||||
parts.append(int(digits))
|
||||
return tuple(parts)
|
||||
|
||||
|
||||
def _get_installed_version(package_name: str) -> str | None:
|
||||
"""Return installed package version, if available."""
|
||||
try:
|
||||
return version(package_name)
|
||||
except PackageNotFoundError:
|
||||
return None
|
||||
|
||||
|
||||
def _get_dependency_mismatch_error() -> str | None:
|
||||
"""
|
||||
Detect the known WeasyPrint/pydyf incompatibility before conversion starts.
|
||||
|
||||
WeasyPrint 61.x instantiates pydyf.PDF with constructor arguments, while
|
||||
pydyf 0.11+ moved these parameters to PDF.write(). That mismatch raises:
|
||||
"PDF.__init__() takes 1 positional argument but 3 were given".
|
||||
"""
|
||||
weasyprint_version = _get_installed_version("weasyprint")
|
||||
pydyf_version = _get_installed_version("pydyf")
|
||||
if not weasyprint_version or not pydyf_version:
|
||||
return None
|
||||
|
||||
if (
|
||||
_parse_version_parts(weasyprint_version) < (62,)
|
||||
and _parse_version_parts(pydyf_version) >= (0, 11)
|
||||
):
|
||||
return (
|
||||
"Installed HTML-to-PDF dependencies are incompatible: "
|
||||
f"WeasyPrint {weasyprint_version} with pydyf {pydyf_version}. "
|
||||
"Reinstall backend dependencies after pinning pydyf<0.11."
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def html_to_pdf(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
@@ -30,6 +79,10 @@ def html_to_pdf(
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
dependency_error = _get_dependency_mismatch_error()
|
||||
if dependency_error:
|
||||
raise HtmlToPdfError(dependency_error)
|
||||
|
||||
from weasyprint import HTML
|
||||
|
||||
HTML(filename=input_path).write_pdf(output_path)
|
||||
@@ -67,6 +120,10 @@ def html_string_to_pdf(
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
dependency_error = _get_dependency_mismatch_error()
|
||||
if dependency_error:
|
||||
raise HtmlToPdfError(dependency_error)
|
||||
|
||||
from weasyprint import HTML
|
||||
|
||||
HTML(string=html_content).write_pdf(output_path)
|
||||
|
||||
Reference in New Issue
Block a user