Added lilypond edition
This commit is contained in:
@@ -9,6 +9,7 @@ import magic
|
||||
from fastapi import HTTPException, UploadFile
|
||||
from folkugat_web.config import db
|
||||
from folkugat_web.dal.sql.temes import links as links_dal
|
||||
from folkugat_web.dal.sql.temes import scores as scores_dal
|
||||
from folkugat_web.log import logger
|
||||
|
||||
|
||||
@@ -46,10 +47,7 @@ async def store_file(tema_id: int, upload_file: UploadFile) -> str:
|
||||
check_mimetype(mimetype)
|
||||
|
||||
extension = mimetypes.guess_extension(mimetype) or ""
|
||||
filename = str(uuid.uuid4().hex) + extension
|
||||
filedir = db.DB_FILES_DIR / str(tema_id)
|
||||
filedir.mkdir(exist_ok=True)
|
||||
filepath = filedir / filename
|
||||
filepath = create_tema_filename(tema_id=tema_id, extension=extension)
|
||||
|
||||
with open(filepath, "wb") as f:
|
||||
_ = f.write(await upload_file.read())
|
||||
@@ -57,15 +55,34 @@ async def store_file(tema_id: int, upload_file: UploadFile) -> str:
|
||||
return get_db_file_path(filepath)
|
||||
|
||||
|
||||
def create_tema_filename(tema_id: int, extension: str = "") -> Path:
|
||||
filename = str(uuid.uuid4().hex) + extension
|
||||
filedir = db.DB_FILES_DIR / str(tema_id)
|
||||
filedir.mkdir(exist_ok=True)
|
||||
filepath = filedir / filename
|
||||
return filepath
|
||||
|
||||
|
||||
def create_tmp_filename(extension: str = "") -> Path:
|
||||
filename = str(uuid.uuid4().hex) + extension
|
||||
filedir = db.DB_FILES_DIR / "tmp"
|
||||
filedir.mkdir(exist_ok=True)
|
||||
filepath = filedir / filename
|
||||
return filepath
|
||||
|
||||
|
||||
def list_files(tema_id: str) -> list[str]:
|
||||
filedir = db.DB_FILES_DIR / str(tema_id)
|
||||
return [get_db_file_path(f) for f in filedir.iterdir()]
|
||||
|
||||
|
||||
def get_orphan_files() -> Iterator[Path]:
|
||||
alive_files = {link.url for link in links_dal.get_links()}
|
||||
link_urls = {link.url for link in links_dal.get_links()}
|
||||
score_pdf_urls = {score.pdf_url for score in scores_dal.get_scores() if score.pdf_url is not None}
|
||||
score_img_urls = {score.img_url for score in scores_dal.get_scores() if score.img_url is not None}
|
||||
alive_urls = link_urls | score_pdf_urls | score_img_urls
|
||||
return filter(
|
||||
lambda p: p.is_file() and get_db_file_path(p) not in alive_files,
|
||||
lambda p: p.is_file() and get_db_file_path(p) not in alive_urls,
|
||||
db.DB_FILES_DIR.rglob("*"),
|
||||
)
|
||||
|
||||
|
||||
70
folkugat_web/services/lilypond.py
Normal file
70
folkugat_web/services/lilypond.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
import aiofiles
|
||||
from folkugat_web.model.lilypond import RenderError
|
||||
from folkugat_web.model.temes import Tema
|
||||
from folkugat_web.services import files as files_service
|
||||
from folkugat_web.templates import templates
|
||||
|
||||
SCORE_BEGINNING = "% --- SCORE BEGINNING --- %"
|
||||
|
||||
RenderFormat = Literal["png"] | Literal["pdf"]
|
||||
|
||||
|
||||
def source_to_single_score(source: str, tema: Tema) -> str:
|
||||
return templates.get_template("lilypond/single_score.ly").render(
|
||||
score_beginning=SCORE_BEGINNING,
|
||||
score_source=source,
|
||||
tema=tema,
|
||||
)
|
||||
|
||||
|
||||
async def render(source: str, fmt: RenderFormat, output_filename: Path | None = None) -> tuple[Path | None, list[RenderError]]:
|
||||
input_filename = files_service.create_tmp_filename(extension=".ly")
|
||||
async with aiofiles.open(input_filename, "w") as f:
|
||||
_ = await f.write(source)
|
||||
|
||||
output_filename = output_filename or files_service.create_tmp_filename()
|
||||
|
||||
if fmt == "png":
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"lilypond", "--png", "-o", output_filename, input_filename,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
else:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"lilypond", "-o", output_filename, input_filename,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
|
||||
_stdout, stderr = await proc.communicate()
|
||||
|
||||
if proc.returncode:
|
||||
esc_input_filename = re.escape(str(input_filename))
|
||||
error_re = re.compile(rf"{esc_input_filename}:(?P<line>\d+):(?P<pos>\d+): error: (?P<error>.*)$")
|
||||
error_matches = filter(None, map(error_re.match, stderr.decode("utf-8").splitlines()))
|
||||
errors = map(lambda m: RenderError.from_dict(m.groupdict()), error_matches)
|
||||
line_offset = source.splitlines().index(SCORE_BEGINNING) + 1
|
||||
errors_offset = [dataclasses.replace(e, line=e.line - line_offset) for e in errors if e.line > line_offset]
|
||||
return None, errors_offset
|
||||
|
||||
if fmt == "png":
|
||||
# Check if the output generated multiple pages
|
||||
pages = sorted(output_filename.parent.rglob(f"{output_filename.name}*.png"))
|
||||
if len(pages) > 1:
|
||||
output_filename = pages[0]
|
||||
for page in pages[1:]:
|
||||
os.remove(page)
|
||||
else:
|
||||
output_filename = output_filename.with_suffix(".png")
|
||||
elif fmt == "pdf":
|
||||
output_filename = output_filename.with_suffix(".pdf")
|
||||
|
||||
return output_filename, []
|
||||
@@ -5,6 +5,7 @@ from folkugat_web.log import logger
|
||||
from folkugat_web.model import playlists
|
||||
from folkugat_web.services.temes import links as links_service
|
||||
from folkugat_web.services.temes import query as temes_query
|
||||
from folkugat_web.services.temes import scores as scores_service
|
||||
|
||||
|
||||
def add_temes_to_playlist(playlist: playlists.Playlist) -> playlists.Playlist:
|
||||
@@ -26,6 +27,7 @@ def add_tema_to_tema_in_set(tema_in_set: playlists.TemaInSet) -> playlists.TemaI
|
||||
logger.error("fCould not load tune in set: {tema_in_set}")
|
||||
else:
|
||||
_ = links_service.add_links_to_tema(tema_in_set.tema)
|
||||
_ = scores_service.add_scores_to_tema(tema_in_set.tema)
|
||||
return tema_in_set
|
||||
|
||||
|
||||
|
||||
56
folkugat_web/services/temes/scores.py
Normal file
56
folkugat_web/services/temes/scores.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from collections.abc import Iterable, Iterator
|
||||
|
||||
from fastapi import HTTPException
|
||||
from folkugat_web.dal.sql.temes import scores as scores_dal
|
||||
from folkugat_web.model import temes as model
|
||||
from folkugat_web.services import lilypond
|
||||
from folkugat_web.services.temes import properties as properties_service
|
||||
from folkugat_web.services.temes import query as temes_q
|
||||
from folkugat_web.utils import FnChain
|
||||
|
||||
|
||||
def add_scores_to_tema(tema: model.Tema) -> model.Tema:
|
||||
if tema.id is not None:
|
||||
tema.scores = list(scores_dal.get_scores(tema_id=tema.id))
|
||||
return tema
|
||||
|
||||
|
||||
def add_scores_to_temes(temes: Iterable[model.Tema]) -> Iterator[model.Tema]:
|
||||
return map(add_scores_to_tema, temes)
|
||||
|
||||
|
||||
def get_score_by_id(score_id: int, tema_id: int | None = None) -> model.Score | None:
|
||||
return next(iter(scores_dal.get_scores(score_id=score_id, tema_id=tema_id)), None)
|
||||
|
||||
|
||||
def update_score(score: model.Score):
|
||||
scores_dal.update_score(score=score)
|
||||
|
||||
|
||||
def delete_score(score_id: int, tema_id: int | None = None):
|
||||
scores_dal.delete_score(score_id=score_id, tema_id=tema_id)
|
||||
|
||||
|
||||
def create_score(tema_id: int) -> model.Score:
|
||||
new_score = model.Score(
|
||||
id=None,
|
||||
tema_id=tema_id,
|
||||
title="",
|
||||
source="",
|
||||
errors=[],
|
||||
img_url=None,
|
||||
pdf_url=None,
|
||||
hidden=True,
|
||||
)
|
||||
return scores_dal.insert_score(score=new_score)
|
||||
|
||||
|
||||
def build_single_tune_full_source(tema_id: int, source: str) -> str:
|
||||
tema = temes_q.get_tema_by_id(tema_id)
|
||||
if not tema:
|
||||
raise HTTPException(status_code=404, detail="Could not find tema!")
|
||||
tema = (
|
||||
FnChain.transform(tema) |
|
||||
properties_service.add_properties_to_tema
|
||||
).result()
|
||||
return lilypond.source_to_single_score(source=source, tema=tema)
|
||||
Reference in New Issue
Block a user