78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
os.environ.setdefault(
|
|
"QTWEBENGINE_CHROMIUM_FLAGS",
|
|
"--disable-gpu --disable-gpu-compositing --disable-software-rasterizer --no-sandbox --log-level=3",
|
|
)
|
|
os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
from PySide6.QtCore import QSettings, QTimer
|
|
from PySide6.QtWidgets import QApplication, QMessageBox
|
|
|
|
from mdhelper.main_window import MainWindow
|
|
from mdhelper.pdf_export import PdfMargins
|
|
|
|
|
|
def main() -> int:
|
|
isolated_settings = tempfile.TemporaryDirectory(prefix="mdhelper-pdf-smoke-")
|
|
QSettings.setDefaultFormat(QSettings.Format.IniFormat)
|
|
QSettings.setPath(QSettings.Format.IniFormat, QSettings.Scope.UserScope, isolated_settings.name)
|
|
output_path = Path(sys.argv[1] if len(sys.argv) > 1 else PROJECT_ROOT / "tmp" / "pdfs" / "pdf_export_smoke.pdf")
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
output_path.unlink(missing_ok=True)
|
|
|
|
app = QApplication.instance() or QApplication([sys.argv[0]])
|
|
settings = QSettings("MDHelper", "MDHelper-PdfExportSmoke")
|
|
settings.clear()
|
|
warnings: list[str] = []
|
|
original_warning = QMessageBox.warning
|
|
QMessageBox.warning = staticmethod(lambda _parent, _title, message, *_args: warnings.append(str(message)))
|
|
|
|
window = MainWindow(project_root=PROJECT_ROOT, initial_path=PROJECT_ROOT / "sample.md")
|
|
window.settings = settings
|
|
original_probe_handler = window._on_pdf_probe_ready
|
|
|
|
def capture_probe(pdf_data: object) -> None:
|
|
output_path.with_suffix(".probe.pdf").write_bytes(bytes(pdf_data))
|
|
original_probe_handler(pdf_data)
|
|
|
|
window._on_pdf_probe_ready = capture_probe
|
|
timeout_ticks = 0
|
|
|
|
def start_export() -> None:
|
|
window.export_pdf_to_path(output_path, PdfMargins(top=15, bottom=15, left=15, right=15))
|
|
poll_export()
|
|
|
|
def poll_export() -> None:
|
|
nonlocal timeout_ticks
|
|
timeout_ticks += 1
|
|
if output_path.exists() and output_path.stat().st_size > 0 and getattr(window, "_pdf_export_page", None) is None:
|
|
app.exit(0)
|
|
return
|
|
if warnings or timeout_ticks >= 300:
|
|
for warning in warnings:
|
|
print(warning, file=sys.stderr)
|
|
app.exit(1)
|
|
return
|
|
QTimer.singleShot(100, poll_export)
|
|
|
|
QTimer.singleShot(0, start_export)
|
|
exit_code = app.exec()
|
|
QMessageBox.warning = original_warning
|
|
window.close()
|
|
return exit_code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|