Skip to content

Commit 796f1a9

Browse files
committed
Build tables with aliases.
1 parent 465b095 commit 796f1a9

9 files changed

Lines changed: 86 additions & 57 deletions

File tree

scriptshifter/tables/__init__.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
logger = logging.getLogger(__name__)
6666

6767
tbl_index = None # Module-level index of all scripts.
68+
proc_aliases = set() # Set of alias tables already created.
69+
aliases = {} # Map of language to alias.
6870

6971

7072
class Token(str):
@@ -165,7 +167,9 @@ def init_db():
165167
conn.executescript(fh.read())
166168

167169
# Populate tables.
168-
global tbl_index
170+
global tbl_index, proc_aliases, aliases
171+
proc_aliases = set()
172+
aliases = {}
169173
with open(path.join(path.dirname(TABLE_DIR), "index.yml")) as fh:
170174
tbl_index = load(fh, Loader=Loader)
171175
try:
@@ -205,6 +209,10 @@ def populate_table(conn, tname, tdata):
205209
"""
206210
logger.info(f"Populating table: {tname}")
207211

212+
check_q = "SELECT id FROM tbl_language WHERE name = ?"
213+
if conn.execute(check_q, (tname,)).fetchone():
214+
return
215+
208216
res = conn.execute(
209217
"""INSERT INTO tbl_language (
210218
name, label, marc_code, description
@@ -217,6 +225,20 @@ def populate_table(conn, tname, tdata):
217225
tid = res.lastrowid
218226

219227
data = load_table(tname)
228+
if "alias_of" in data:
229+
# If an alias, insert the alias ID.
230+
ref_name = data["alias_of"]
231+
logger.info(f"{tname} is an alias of {ref_name}.")
232+
ref_data = conn.execute(check_q, (ref_name,)).fetchone()
233+
# Check if the ref table has already been populated.
234+
if not ref_data:
235+
populate_table(conn, ref_name, tbl_index[ref_name])
236+
ref_data = conn.execute(check_q, (ref_name,)).fetchone()
237+
ref_id = ref_data[0]
238+
conn.execute(
239+
"UPDATE tbl_language SET ref_id = ? WHERE id = ?",
240+
(ref_id, tid))
241+
220242
flags = 0
221243
if "script_to_roman" in data:
222244
flags |= FEAT_S2R
@@ -340,16 +362,20 @@ def load_table(tname):
340362
The table file is parsed into an in-memory configuration that contains
341363
the language & script metadata and parsing rules.
342364
"""
365+
if "alias_of" in tbl_index.get(tname, {}):
366+
conf_name = tbl_index[tname]["alias_of"]
367+
aliases[tname] = conf_name
343368

344-
try:
345-
fname = path.join(TABLE_DIR, tbl_index[tname]["conf"])
346-
except KeyError:
347-
# If no `conf` key is provided, use the conventional table name + .yml.
348-
fname = path.join(TABLE_DIR, tname + ".yml")
369+
return {"alias_of": conf_name}
370+
371+
else:
372+
# If no `alias_of` key is provided, use the regular table name + .yml.
373+
conf_name = tname
374+
375+
fname = path.join(TABLE_DIR, conf_name + ".yml")
349376
if not access(fname, R_OK):
350377
raise ValueError(
351378
f"No transliteration table `{fname}` found for {tname}!")
352-
353379
with open(fname) as fh:
354380
tdata = load(fh, Loader=Loader)
355381

scriptshifter/tables/data/assamese.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ general:
66
version: 1.0.0
77
date: 2025-11-30
88
parents:
9-
- _ignore_base
10-
- _bengali_base
9+
- bengali
1110

1211
roman_to_script:
1312
map:
File renamed without changes.

scriptshifter/tables/data/bodo_bengali.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ general:
66
version: 1.0.0
77
date: 2026-01-08
88
parents:
9-
- _bengali_base
9+
- bengali
1010

1111
roman_to_script:
1212
map:

scriptshifter/tables/data/_gurmukhi_base.yml renamed to scriptshifter/tables/data/gurmukhi_generic.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
general:
3-
name: Gurmukhi base
3+
name: Gurmukhi (generic)
44
case_sensitive: false
55
description: Bidirectional base mapping for the Gurmukhi script.
66
version: 1.0.0

scriptshifter/tables/data/manipuri_bengali.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ general:
66
version: 0.0.0
77
date: 2025-12-23
88
parents:
9-
- _bengali_base
9+
- bengali
1010

1111
roman_to_script:
1212
map:

scriptshifter/tables/data/panjabi_gurmukhi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ general:
66
version: 1.0.0
77
date: 2025-12-23
88
parents:
9-
- _gurmukhi_base
9+
- gurmukhi_generic
1010

1111
roman_to_script:
1212
hooks:

0 commit comments

Comments
 (0)