Skip to content

Commit 4ac131c

Browse files
authored
Merge pull request #288 from lcnetdev/aliases
Use language aliases.
2 parents e390354 + 8a3cc0a commit 4ac131c

File tree

11 files changed

+111
-61
lines changed

11 files changed

+111
-61
lines changed

scriptshifter/rest_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def handle_exception(e: ApiError):
7373
def index():
7474
return render_template(
7575
"index.html",
76-
languages=list_tables(),
76+
languages=sorted(
77+
list_tables().items(),
78+
key=lambda k: k[1]["label"]
79+
),
7780
version_info=(GIT_TAG, GIT_COMMIT),
7881
feedback_form=SMTP_HOST is not None or FEEDBACK_PATH is not None)
7982

scriptshifter/tables/__init__.py

Lines changed: 53 additions & 9 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

@@ -568,9 +594,13 @@ def get_language(lang):
568594

569595
def get_lang_general(conn, lang):
570596
""" Language general attributes. """
597+
ref_q = "SELECT id, ref_id FROM tbl_language WHERE name = ?"
598+
ref_data = conn.execute(ref_q, (lang,)).fetchone()
599+
lang_id = ref_data[1] if ref_data[1] else ref_data[0]
600+
571601
lang_q = conn.execute(
572602
"""SELECT id, name, label, features, marc_code, description
573-
FROM tbl_language WHERE name = ?""", (lang,))
603+
FROM tbl_language WHERE id = ?""", (lang_id,))
574604
lang_data = lang_q.fetchone()
575605

576606
if not lang_data:
@@ -579,7 +609,7 @@ def get_lang_general(conn, lang):
579609
return {
580610
"id": lang_data[0],
581611
"data": {
582-
"name": lang_data[1],
612+
"name": lang,
583613
"label": lang_data[2],
584614
"has_s2r": bool(lang_data[3] & FEAT_S2R),
585615
"has_r2s": bool(lang_data[3] & FEAT_R2S),
@@ -591,6 +621,7 @@ def get_lang_general(conn, lang):
591621

592622

593623
def get_lang_normalize(conn, lang_id):
624+
lang_id = _get_ref(conn, lang_id)
594625
qry = conn.execute(
595626
"""SELECT src, dest FROM tbl_normalize
596627
WHERE lang_id = ?""",
@@ -602,6 +633,7 @@ def get_lang_ignore(conn, lang_id):
602633
"""
603634
Ignore list as a tuple.
604635
"""
636+
lang_id = _get_ref(conn, lang_id)
605637
qry = conn.execute(
606638
"""SELECT rule, features FROM tbl_ignore
607639
WHERE lang_id = ?""",
@@ -618,6 +650,7 @@ def get_lang_map(conn, lang_id, t_dir):
618650
619651
Generator of tuples (source, destination).
620652
"""
653+
lang_id = _get_ref(conn, lang_id)
621654
qry = conn.execute(
622655
"""SELECT src, dest FROM tbl_trans_map
623656
WHERE lang_id = ? AND dir = ?
@@ -630,6 +663,7 @@ def get_lang_map(conn, lang_id, t_dir):
630663

631664
def get_lang_options(conn, lang_id):
632665
""" Language options as a tuple of dictionaries. """
666+
lang_id = _get_ref(conn, lang_id)
633667
qry = conn.execute(
634668
"""SELECT name, label, description, dtype, options, default_v
635669
FROM tbl_option
@@ -650,6 +684,7 @@ def get_lang_options(conn, lang_id):
650684

651685

652686
def get_lang_hooks(conn, lang_id, t_dir):
687+
lang_id = _get_ref(conn, lang_id)
653688
""" Language hooks in sorting order. """
654689
hooks = defaultdict(list)
655690

@@ -672,9 +707,18 @@ def get_lang_hooks(conn, lang_id, t_dir):
672707

673708

674709
def get_lang_dcap(conn, lang_id):
710+
lang_id = _get_ref(conn, lang_id)
675711
qry = conn.execute(
676712
"""SELECT rule
677713
FROM tbl_double_cap WHERE lang_id = ?""",
678714
(lang_id,))
679715

680716
return tuple(row[0] for row in qry)
717+
718+
719+
def _get_ref(conn, lang_id):
720+
ref_data = conn.execute(
721+
"""SELECT ref_id FROM tbl_language WHERE id = ?""",
722+
(lang_id,)).fetchone()
723+
724+
return ref_data[0] if ref_data and ref_data[0] else lang_id

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)