Afegir partitures de llistes (cançoners)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user