Merge pull request #16 from DARC-e-V/feature-download-svg
feature: download svgs instead of building them
This commit is contained in:
Vendored
+1
@@ -6,6 +6,7 @@
|
|||||||
"python.testing.unittestEnabled": false,
|
"python.testing.unittestEnabled": false,
|
||||||
"python.testing.pytestEnabled": true,
|
"python.testing.pytestEnabled": true,
|
||||||
"[python]": {
|
"[python]": {
|
||||||
|
"editor.formatOnSave": true,
|
||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.fixAll.ruff": "explicit",
|
"source.fixAll.ruff": "explicit",
|
||||||
"source.organizeImports.ruff": "explicit"
|
"source.organizeImports.ruff": "explicit"
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ dl.download_edition("N")
|
|||||||
|
|
||||||
# Download additional files:
|
# Download additional files:
|
||||||
dl.download_question_metadata()
|
dl.download_question_metadata()
|
||||||
dl.download_pictures()
|
|
||||||
dl.download_photos()
|
dl.download_photos()
|
||||||
|
dl.download_pictures()
|
||||||
dl.download_includes()
|
dl.download_includes()
|
||||||
dl.download_snippets()
|
dl.download_snippets()
|
||||||
dl.download_content()
|
dl.download_content()
|
||||||
|
|||||||
+9
-40
@@ -1,9 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
import multiprocessing
|
|
||||||
import os
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
from joblib import Memory, Parallel, delayed
|
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
from api.directus import DirectusAPI
|
from api.directus import DirectusAPI
|
||||||
@@ -102,45 +98,18 @@ class Download:
|
|||||||
|
|
||||||
def download_pictures(self):
|
def download_pictures(self):
|
||||||
self.config.p_data_pictures.mkdir(parents=True, exist_ok=True)
|
self.config.p_data_pictures.mkdir(parents=True, exist_ok=True)
|
||||||
# Symlink photos, so LaTeX can access these assets to render into graphics.
|
|
||||||
foto_link = self.config.p_data_pictures / "foto"
|
|
||||||
if not foto_link.is_symlink():
|
|
||||||
foto_link.symlink_to(self.config.p_data_photos)
|
|
||||||
|
|
||||||
memory = Memory("./cache", verbose=0)
|
pictures = self.api.get("items/pictures", {"limit": -1})
|
||||||
|
|
||||||
@memory.cache
|
for picture in tqdm(pictures, desc="Downloading pictures"):
|
||||||
def build_picture(picture):
|
# Save LaTeX sources to file
|
||||||
pid = picture["id"]
|
with (self.config.p_data_pictures / f"{picture['id']}include.tex").open("w") as file:
|
||||||
|
file.write(picture["latex"])
|
||||||
|
|
||||||
if (
|
# Download SVG
|
||||||
"\\begin{document}" in picture["latex"] or "\\begin{table}" in picture["latex"]
|
data = self.api.get_file(f"assets/{picture['picture']}")
|
||||||
): # Workaround, if the picture contains a document class it should be ignored!
|
with (self.config.p_data_pictures / f"{picture['id']}.svg").open("wb") as file:
|
||||||
return
|
file.write(data)
|
||||||
|
|
||||||
if self.config.no_latex:
|
|
||||||
shutil.copyfile("./assets/images/50ohm_gray.svg", self.config.p_data_pictures / f"{pid}.svg")
|
|
||||||
else:
|
|
||||||
with (self.config.p_data_pictures / f"img/{pid}include.tex").open("w") as file:
|
|
||||||
file.write(picture["latex"])
|
|
||||||
|
|
||||||
aux_file = self.config.p_data_photos / f"{pid}.tex"
|
|
||||||
with aux_file.open("w", encoding="utf-8") as file:
|
|
||||||
# Write the auxilary LaTeX file:
|
|
||||||
file.write(f"\\documentclass{{BNetzA-Fragenkatalog}}\\DARCimageOnly{{9cm}}{{{pid}include}}")
|
|
||||||
# Build the picture:
|
|
||||||
os.system(f"latexmk -lualatex -f -interaction=nonstopmode {aux_file} > /dev/null 2>&1")
|
|
||||||
# Convert the PDF to SVG:
|
|
||||||
os.system(
|
|
||||||
"pdftocairo -svg"
|
|
||||||
f"{self.config.p_data_pictures / f'{pid}.pdf'} - > {self.config.p_data_pictures / f'{pid}.svg'}"
|
|
||||||
)
|
|
||||||
|
|
||||||
pictures = self.api.get("items/pictures", params={"limit": -1})
|
|
||||||
|
|
||||||
Parallel(n_jobs=multiprocessing.cpu_count())(
|
|
||||||
delayed(build_picture)(picture) for picture in tqdm(pictures, desc="Build All Pictures")
|
|
||||||
)
|
|
||||||
|
|
||||||
def download_includes(self):
|
def download_includes(self):
|
||||||
includes = self.api.get("items/include", params={"limit": -1})
|
includes = self.api.get("items/include", params={"limit": -1})
|
||||||
|
|||||||
Reference in New Issue
Block a user