+7
-25
@@ -7,7 +7,7 @@ import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
APPLICATION_ID = 0x56414D50
|
||||
SCHEMA_VERSION = 1
|
||||
SCHEMA_VERSION = 2
|
||||
COMMON_COLUMNS = """
|
||||
id TEXT PRIMARY KEY NOT NULL CHECK(length(trim(id)) BETWEEN 1 AND 160),
|
||||
title TEXT NOT NULL CHECK(length(trim(title)) BETWEEN 1 AND 200),
|
||||
@@ -20,17 +20,6 @@ COMMON_COLUMNS = """
|
||||
total_pages INTEGER NOT NULL CHECK(total_pages BETWEEN 1 AND 10000)
|
||||
"""
|
||||
|
||||
def human_label(value: str) -> str:
|
||||
result, capitalize = '', True
|
||||
for character in value:
|
||||
if character in '-_':
|
||||
result += ' '
|
||||
capitalize = True
|
||||
else:
|
||||
result += character.upper() if capitalize and 'a' <= character <= 'z' else character
|
||||
capitalize = False
|
||||
return result or 'Score'
|
||||
|
||||
def metadata(path: Path, root: Path) -> dict:
|
||||
if path.is_symlink() or not path.resolve().is_relative_to(root.resolve()):
|
||||
raise ValueError('score escapes the asset root')
|
||||
@@ -67,15 +56,12 @@ def populate(connection: sqlite3.Connection, root: Path) -> None:
|
||||
PRAGMA application_id = {APPLICATION_ID};
|
||||
PRAGMA user_version = {SCHEMA_VERSION};
|
||||
CREATE TABLE songs ({COMMON_COLUMNS}, part TEXT NOT NULL) STRICT;
|
||||
CREATE TABLE practice_exercises ({COMMON_COLUMNS},
|
||||
exercise_id TEXT NOT NULL,
|
||||
practice_type_id TEXT NOT NULL, practice_type_title TEXT NOT NULL,
|
||||
lesson_group_id TEXT NOT NULL, lesson_group_title TEXT NOT NULL) STRICT;
|
||||
CREATE TABLE practice_exercises ({COMMON_COLUMNS}, exercise_id TEXT NOT NULL) STRICT;
|
||||
CREATE INDEX songs_menu ON songs(title COLLATE NOCASE, composer COLLATE NOCASE, part COLLATE NOCASE);
|
||||
CREATE INDEX practice_menu ON practice_exercises(practice_type_id COLLATE NOCASE, lesson_group_id COLLATE NOCASE, source_path COLLATE NOCASE);
|
||||
CREATE INDEX practice_menu ON practice_exercises(exercise_id, id);
|
||||
''')
|
||||
identities, authored_keys = {}, {}
|
||||
for category, table, depth in (('songs', 'songs', 1), ('practice', 'practice_exercises', 4)):
|
||||
authored_keys = {}
|
||||
for category, table, depth in (('songs', 'songs', 1), ('practice', 'practice_exercises', 2)):
|
||||
folder = root / category
|
||||
if (folder.exists() or folder.is_symlink()) and not folder.is_dir():
|
||||
raise ValueError(f'{folder}: score asset directory not found')
|
||||
@@ -86,15 +72,11 @@ def populate(connection: sqlite3.Connection, root: Path) -> None:
|
||||
raise ValueError(f'unexpected {category} score path')
|
||||
row = metadata(path, root)
|
||||
if category == 'practice':
|
||||
identity = '-'.join(parts[:3])
|
||||
previous = identities.setdefault(identity, path.parent)
|
||||
if previous != path.parent:
|
||||
raise ValueError(f'practice exercise ID collision {identity!r}: {previous} and {path.parent}')
|
||||
identity = parts[0]
|
||||
if authored_keys.setdefault(identity, row['authored_key_signature']) != row['authored_key_signature']:
|
||||
raise ValueError(f'practice variants disagree on authored key: {identity}')
|
||||
row.pop('part')
|
||||
row.update(id=identity + '-' + path.stem, exercise_id=identity, practice_type_id=parts[0], practice_type_title=human_label(parts[0]) + ' Practice',
|
||||
lesson_group_id=parts[1], lesson_group_title=human_label(parts[1]))
|
||||
row.update(id=identity + '-' + path.stem, exercise_id=identity)
|
||||
connection.execute(f'INSERT INTO {table} ({", ".join(row)}) VALUES ({", ".join("?" for _ in row)})', tuple(row.values()))
|
||||
except (ValueError, KeyError, TypeError, sqlite3.Error) as error:
|
||||
raise ValueError(f'{path}: {error}') from error
|
||||
|
||||
Reference in New Issue
Block a user