Afegir partitures de llistes (cançoners)

This commit is contained in:
marc
2025-12-21 15:40:11 +01:00
parent 089e61fb0b
commit ac79785cf0
16 changed files with 238 additions and 6 deletions

View File

@@ -100,6 +100,10 @@ def get_set_filename(filename: str) -> Path:
return db.DB_FILES_SET_DIR / filename
def get_playlist_filename(filename: str) -> Path:
return db.DB_FILES_PLAYLIST_DIR / filename
@asynccontextmanager
async def tmp_file(content: str):
input_filename = create_tmp_filename(extension=".ly")

View File

@@ -4,6 +4,7 @@ from fastapi import HTTPException
from folkugat_web.model import playlists as playlists_model
from folkugat_web.model import temes as model
from folkugat_web.model.lilypond import score as lilypond_model
from folkugat_web.services import playlists as playlists_service
from folkugat_web.services.temes import lyrics as lyrics_service
from folkugat_web.services.temes import properties as properties_service
from folkugat_web.services.temes import query as temes_q
@@ -96,3 +97,20 @@ def set_from_set(set_entry: playlists_model.Set) -> lilypond_model.LilypondSet:
title=set_title,
tunes=tunes
)
def playlist_from_playlist(playlist: playlists_model.Playlist) -> lilypond_model.LilypondPlaylist:
"""
The playlist is assumed to be enriched with tunes
"""
lilypond_sets = []
for set_entry in playlist.sets:
lilypond_set = set_from_set(set_entry)
if lilypond_set.tunes and all(map(playlists_service._elegible_for_set_score, lilypond_set.tunes)):
lilypond_sets.append(lilypond_set)
playlist_title = playlist.name or "Llista"
return lilypond_model.LilypondPlaylist(
title=playlist_title,
sets=lilypond_sets
)

View File

@@ -1,4 +1,4 @@
from folkugat_web.model.lilypond.score import LilypondSet, LilypondTune
from folkugat_web.model.lilypond.score import LilypondPlaylist, LilypondSet, LilypondTune
from folkugat_web.templates import templates
SCORE_BEGINNING = "% --- SCORE BEGINNING --- %"
@@ -23,3 +23,10 @@ def set_source(tune_set: LilypondSet) -> str:
score_beginning=SCORE_BEGINNING,
tune_set=tune_set,
)
def playlist_source(playlist: LilypondPlaylist) -> str:
return templates.get_template("lilypond/playlist.ly").render(
score_beginning=SCORE_BEGINNING,
playlist=playlist,
)

View File

@@ -170,3 +170,49 @@ async def add_set_score_to_set(tune_set: playlists.Set) -> playlists.Set:
)
else:
return tune_set
async def get_or_create_playlist_score(playlist: playlists.Playlist) -> playlists.PlaylistScore | None:
# The playlist is assumed to be enriched with tunes
if not playlist.sets:
return None
lilypond_playlist = lilypond_build.playlist_from_playlist(playlist)
if not lilypond_playlist.sets:
return None
playlist_score_hash = lilypond_playlist.hash().hex()
pdf_filepath = files_service.get_playlist_filename(f"{playlist_score_hash}.pdf")
if not pdf_filepath.exists():
# No score exists, so we need to create it
playlist_source = lilypond_source.playlist_source(playlist=lilypond_playlist)
out_filepath = files_service.get_playlist_filename(playlist_score_hash)
async with files_service.tmp_file(content=playlist_source) as source_filepath:
if not pdf_filepath.exists():
pdf_result = await lilypond_render.render_file(
input_file=source_filepath,
output=lilypond_render.RenderOutput.PDF,
output_file=out_filepath,
)
if pdf_result.error is not None:
return None
if pdf_result.result is None:
raise RuntimeError("This shouldn't happen")
pdf_filepath = pdf_result.result
return playlists.PlaylistScore(
img_url=None, # Only PDF generation for now
pdf_url=files_service.get_db_file_path(pdf_filepath),
)
async def add_playlist_score_to_playlist(playlist: playlists.Playlist) -> playlists.Playlist:
if score := await get_or_create_playlist_score(playlist=playlist):
return dataclasses.replace(
playlist,
playlist_score=score,
)
else:
return playlist

View File

@@ -184,8 +184,8 @@ def _create_session_playlists(session_id: int):
setlist_name, slowjam_name = _get_playlist_names(session=session)
setlist_playlist_id = playlists_write.create_playlist(name=setlist_name, con=None)
slowjam_playlist_id = playlists_write.create_playlist(name=slowjam_name, con=None)
setlist_playlist_id = playlists_write.create_playlist(name=setlist_name)
slowjam_playlist_id = playlists_write.create_playlist(name=slowjam_name)
with get_connection() as con:
session_playlists.insert_playlist(