From 8a878b4cc12bd13a9546265dddc486cb9bc93c1a Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Fri, 11 Oct 2024 22:04:12 +0200 Subject: [PATCH] Storing LaTeX renders in cache directory instead of temporary to store them for later use. + Makes reponening files instantaneous. + Improves performance of 'base state' objects (e.g. A = (1, 0) or f(x) = x). --- .../LogarithmPlotter/logarithmplotter.py | 2 +- .../LogarithmPlotter/util/helper.py | 2 +- .../LogarithmPlotter/util/latex.py | 27 +++++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/runtime-pyside6/LogarithmPlotter/logarithmplotter.py b/runtime-pyside6/LogarithmPlotter/logarithmplotter.py index 3264097..d620da8 100644 --- a/runtime-pyside6/LogarithmPlotter/logarithmplotter.py +++ b/runtime-pyside6/LogarithmPlotter/logarithmplotter.py @@ -164,7 +164,7 @@ def run(): app.installEventFilter(macos_file_open_handler) helper = Helper(pwd, tmpfile) - latex = Latex(tempdir) + latex = Latex() engine, js_globals = create_engine(helper, latex, dep_time) if len(engine.rootObjects()) == 0: # No root objects loaded diff --git a/runtime-pyside6/LogarithmPlotter/util/helper.py b/runtime-pyside6/LogarithmPlotter/util/helper.py index d2fe631..6db4570 100644 --- a/runtime-pyside6/LogarithmPlotter/util/helper.py +++ b/runtime-pyside6/LogarithmPlotter/util/helper.py @@ -42,7 +42,7 @@ def show_message(msg: str) -> None: Shows a GUI message if GUI messages are enabled """ if SHOW_GUI_MESSAGES: - QMessageBox.warning(None, "LogarithmPlotter", msg, QMessageBox.OK) + QMessageBox.warning(None, "LogarithmPlotter", msg) else: raise InvalidFileException(msg) diff --git a/runtime-pyside6/LogarithmPlotter/util/latex.py b/runtime-pyside6/LogarithmPlotter/util/latex.py index 2c650ce..8f75aa8 100644 --- a/runtime-pyside6/LogarithmPlotter/util/latex.py +++ b/runtime-pyside6/LogarithmPlotter/util/latex.py @@ -20,13 +20,23 @@ from PySide6.QtCore import QObject, Slot, Property, QCoreApplication from PySide6.QtGui import QImage, QColor from PySide6.QtWidgets import QMessageBox -from os import path, remove +from os import path, remove, environ, makedirs from string import Template -from tempfile import TemporaryDirectory from subprocess import Popen, TimeoutExpired, PIPE +from platform import system +from hashlib import sha512 from shutil import which from sys import argv +CACHE_PATH = { + "Linux": path.join(environ["XDG_CONFIG_HOME"], "LogarithmPlotter") + if "XDG_CONFIG_HOME" in environ else + path.join(path.expanduser("~"), ".cache", "LogarithmPlotter"), + "Windows": path.join(path.expandvars('%APPDATA%'), "LogarithmPlotter", "cache"), + "Darwin": path.join(path.expanduser("~"), "Library", "Caches", "LogarithmPlotter"), +}[system()] + + """ Searches for a valid Latex and DVIPNG (http://savannah.nongnu.org/projects/dvipng/) installation and collects the binary path in the DVIPNG_PATH variable. @@ -75,9 +85,10 @@ class Latex(QObject): dvipng to be installed on the system. """ - def __init__(self, tempdir: TemporaryDirectory): + def __init__(self): QObject.__init__(self) - self.tempdir = tempdir + self.tempdir = path.join(CACHE_PATH, "latex") + makedirs(self.tempdir, exist_ok=True) @Property(bool) def latexSupported(self) -> bool: @@ -117,7 +128,7 @@ class Latex(QObject): if self.latexSupported and not path.exists(export_path + ".png"): print("Rendering", latex_markup, export_path) # Generating file - latex_path = path.join(self.tempdir.name, str(markup_hash)) + latex_path = path.join(self.tempdir, str(markup_hash)) # If the formula is just recolored or the font is just changed, no need to recreate the DVI. if not path.exists(latex_path + ".dvi"): self.create_latex_doc(latex_path, latex_markup) @@ -148,8 +159,8 @@ class Latex(QObject): """ Standardizes export path for renders. """ - markup_hash = "render" + str(hash(latex_markup)) - export_path = path.join(self.tempdir.name, f'{markup_hash}_{int(font_size)}_{color.rgb()}') + markup_hash = "render" + str(sha512(latex_markup.encode()).hexdigest()) + export_path = path.join(self.tempdir, f'{markup_hash}_{int(font_size)}_{color.rgb()}') return markup_hash, export_path def create_latex_doc(self, export_path: str, latex_markup: str): @@ -193,7 +204,7 @@ class Latex(QObject): Runs a subprocess and handles exceptions and messages them to the user. """ cmd = " ".join(process) - proc = Popen(process, stdout=PIPE, stderr=PIPE, cwd=self.tempdir.name) + proc = Popen(process, stdout=PIPE, stderr=PIPE, cwd=self.tempdir) try: out, err = proc.communicate(timeout=2) # 2 seconds is already FAR too long. if proc.returncode != 0: