Enable writing to the DB

This commit is contained in:
Luke Murphy 2020-02-29 23:24:43 +00:00
parent c0e0b48061
commit 63befa27da
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
1 changed files with 18 additions and 1 deletions

View File

@ -1,11 +1,28 @@
"""User facing API."""
from re import IGNORECASE, compile
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from calibrestekje.bindings import * # noqa
def title_sort(title):
"""Title sorting SQLAlchemy view function."""
regex = r"^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+"
title_pat = compile(regex, IGNORECASE)
match = title_pat.search(title)
if match:
prep = match.group(1)
title = title.replace(prep, "") + ", " + prep
return title.strip()
def init_session(url: str):
"""Initialise a SQLAlchemy session against a Calibre database."""
return Session(create_engine(url))
engine = create_engine(url)
session = Session(engine)
connection = session.connection()
connection.connection.create_function("title_sort", 1, title_sort)
return session