Caching en el generador de cancioneros

This commit is contained in:
Carlos Galindo 2025-05-19 18:56:05 +02:00
commit eefbf5449b
3 changed files with 39 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
*.pyc *.pyc
public/ public/
audios/ audios/
songs.dat

View file

@ -22,6 +22,7 @@ public/index.html: $(ALL_TEMPLATES) $(PY_SRC)
--latex latex/cancionero.tex \ --latex latex/cancionero.tex \
--audios audios \ --audios audios \
--other-latex latex/canciones/ \ --other-latex latex/canciones/ \
--use-cache \
--title "$(NAME)" \ --title "$(NAME)" \
--author "$(AUTHOR)" \ --author "$(AUTHOR)" \
--links "$(SLO_LINKS)" --links "$(SLO_LINKS)"
@ -44,3 +45,4 @@ public/%: res/static/%
clean: clean:
rm -rf public rm -rf public
rm -f songs.dat

View file

@ -1,6 +1,7 @@
import argparse import argparse
import os import os
import re import re
import pickle
from django.conf import settings from django.conf import settings
from django.template import Engine, Context from django.template import Engine, Context
@ -45,7 +46,7 @@ def extra_put(extra: list, index: int, the_type: str, data: dict|None = None):
class SongLoader: class SongLoader:
'''Parses LaTeX files to build a collection of song objects.''' '''Parses LaTeX files to build a collection of song objects.'''
def __init__(self, latex_file: str, audio_dir: str | None = None): def __init__(self, latex_file: str | None = None, audio_dir: str | None = None):
'''Initializes and populates a LaTeX reader.''' '''Initializes and populates a LaTeX reader.'''
self.index: int = 1 self.index: int = 1
self.category: str | None = None self.category: str | None = None
@ -53,7 +54,16 @@ class SongLoader:
self.songs: list[Song] = [] self.songs: list[Song] = []
if audio_dir: if audio_dir:
self.audio_dir = audio_dir self.audio_dir = audio_dir
self.scan(latex_file) if latex_file:
self.scan(latex_file)
def populate_with_songs(self, songs):
for s in songs:
self.songs.append(s)
if s.category not in self.categories:
self.categories.append(s.category)
if s.number < self.index:
self.index = s.number
def scan(self, latex_file: str) -> None: def scan(self, latex_file: str) -> None:
'''Reads through an index file and scans each song, with the same numbers. '''Reads through an index file and scans each song, with the same numbers.
@ -345,14 +355,35 @@ def create_argparser():
parser.add_argument("--title", required=True, nargs=1, help="The title of the webpage") parser.add_argument("--title", required=True, nargs=1, help="The title of the webpage")
parser.add_argument("--author", required=True, nargs=1, help="The author for the footer") parser.add_argument("--author", required=True, nargs=1, help="The author for the footer")
parser.add_argument("--links", required=False, nargs=1, default=["false"]) parser.add_argument("--links", required=False, nargs=1, default=["false"])
parser.add_argument("--use-cache", required=False, action="store_true",
help="Whether to use a cache file if available (to use if songs haven't changed)")
return parser return parser
def cache_or_parse(args):
loader = None
if args.use_cache:
try:
with open("songs.dat", "rb") as f:
loader = SongLoader()
loader.populate_with_songs(pickle.load(f))
except OSError:
pass
if not loader:
loader = SongLoader(args.latex[0], args.audios[0])
if args.other_latex:
loader.scan_others(args.other_latex[0], args.other_index[0])
try:
with open("songs.dat", "wb") as f:
pickle.dump(loader.songs, f)
except OSError:
pass
return loader
if __name__ == '__main__': if __name__ == '__main__':
args = create_argparser().parse_args() args = create_argparser().parse_args()
loader = SongLoader(args.latex[0], args.audios[0]) loader = cache_or_parse(args)
if args.other_latex:
loader.scan_others(args.other_latex[0], int(args.other_index[0]))
settings.configure(USE_TZ=False, USE_I18N=False) settings.configure(USE_TZ=False, USE_I18N=False)
e = Engine(dirs=["res/templates/"]) e = Engine(dirs=["res/templates/"])
loader.generate_html(args.output_dir[0], e, { loader.generate_html(args.output_dir[0], e, {