Added lyrics to scores

This commit is contained in:
marc
2025-04-05 15:10:56 +02:00
parent d5eb40e300
commit 211a1fbb05
7 changed files with 89 additions and 9 deletions

View File

@@ -1,7 +1,26 @@
import dataclasses
import re
from collections.abc import Iterable, Iterator
from typing import Callable
from folkugat_web.dal.sql.temes import lyrics as lyrics_dal
from folkugat_web.model import temes as model
from folkugat_web.utils import FnChain
def _sub(pattern: str, sub: str) -> Callable[[str], str]:
def _inner_sub(text: str) -> str:
return re.sub(pattern, sub, text)
return _inner_sub
def _clean_string(lyrics_str: str) -> str:
return (
FnChain.transform(lyrics_str) |
_sub(r" *", " ") |
_sub(r"\s*\n\s*", "\n") |
_sub(r"\n*", "\n")
).result()
def add_lyrics_to_tema(tema: model.Tema) -> model.Tema:
@@ -19,6 +38,7 @@ def get_lyric_by_id(lyric_id: int, tema_id: int | None = None) -> model.Lyrics |
def update_lyric(lyric: model.Lyrics):
lyric = dataclasses.replace(lyric, content=_clean_string(lyric.content))
lyrics_dal.update_lyric(lyric=lyric)