Kaynağa Gözat

WIP database setup.

scossu 11 ay önce
ebeveyn
işleme
7c2c862511

+ 8 - 0
scriptshifter/__init__.py

@@ -9,6 +9,14 @@ env = load_dotenv()
 
 APP_ROOT = path.dirname(path.realpath(__file__))
 
+"""
+SQLite database path.
+
+This DB stores all the runtime transliteration data.
+"""
+DB_PATH = environ.get(
+        "DB_PATH", path.join(APP_ROOT, "data", "scriptshifter.db"))
+
 """
 SMTP server for sending email. For a dummy server that just echoes the
 messages, run: `python -m smtpd -n -c DebuggingServer localhost:1025`

+ 5 - 3
scriptshifter/hooks/greek/__init__.py

@@ -6,9 +6,9 @@ from logging import getLogger
 from scriptshifter.exceptions import CONT
 
 
-# Suffixed by ʹ
 # Indices are positions in the numeric string from the right
 DIGITS = {
+    # Suffixed by ʹ (U+0374)
     1: {  # Units
         "α": 1,
         "β": 2,
@@ -45,7 +45,7 @@ DIGITS = {
         "ω": 8,
         "ϡ": 9,
     },
-    # Prefixed by ͵
+    # Prefixed by ͵ (U+0375)
     4: {
         "α": 1,
         "β": 2,
@@ -119,15 +119,17 @@ def parse_numeral(ctx):
     # transliterated characters.
     if ctx.src[ctx.cur] == NUM_SUFFIX:
         # Move back up to 3 positions.
+        breakpoint()
         for i in range(1, 4):
             cur = ctx.cur - i
             if cur >= 0:
                 num_tk = ctx.src[cur]  # Number to be parsed
-                if ctx.src[cur] in DIGITS[i]:
+                if num_tk in DIGITS[i]:
                     # Not yet reached word boundary.
                     ctx.dest_ls[-i] = str(DIGITS[i][num_tk])
                 else:
                     if ctx.src[cur] != " ":  # Word boundary.
+                        continue
                         # Something's wrong.
                         ctx.warnings.append(
                                 f"Character `{ctx.src[cur] }` at position "

+ 39 - 1
scriptshifter/tables/__init__.py

@@ -1,9 +1,10 @@
 import logging
 import re
+import sqlite3
 
 from functools import cache
 from importlib import import_module
-from os import environ, path, access, R_OK
+from os import R_OK, access, environ, makedirs, path, unlink
 
 from yaml import load
 try:
@@ -11,6 +12,7 @@ try:
 except ImportError:
     from yaml import Loader
 
+from scriptshifter import DB_PATH
 from scriptshifter.exceptions import BREAK, ConfigError
 
 
@@ -52,6 +54,10 @@ TOKEN_WB_MARKER = "%"
 BOW = 1 << 1
 EOW = 1 << 0
 
+# Feature flags used in database tables.
+FEAT_S2R = 1 << 0       # Has S2R
+FEAT_R2S = 1 << 1       # Has R2S
+FEAT_CASEI = 1 << 2     # Case-insensitive script.
 
 logger = logging.getLogger(__name__)
 
@@ -123,6 +129,38 @@ class Token(str):
         return hash(self.content)
 
 
+def init_db():
+    """
+    Populate database with language data.
+
+    This operation removes any preexisting database.
+
+    All tables in the index file (`./data/index.yml`) will be parsed
+    (including inheritance rules) and loaded into the designated DB.
+
+    This must be done only once at bootstrap. To update individual tables,
+    see populate_table(), which this function calls iteratively.
+    """
+    # Remove preexisting DB and create parent diretories if necessary.
+    makedirs(path.dirname(DB_PATH), exist_ok=True)
+    if path.isfile(DB_PATH):
+        unlink(DB_PATH)
+
+    conn = sqlite3.connect(DB_PATH)
+
+    # Initialize schema.
+    with open(path.join(path.dirname(DEFAULT_TABLE_DIR), "init.sql")) as fh:
+        conn.execute(fh.read())
+
+    # Populate tables.
+    for tname in list_tables().keys():
+        populate_table(tname)
+
+
+def populate_table(tname):
+    data = load_table(tname)
+
+
 @cache
 def list_tables():
     """