6565logger = logging .getLogger (__name__ )
6666
6767tbl_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
7072class 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
0 commit comments