You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lexonomy_custom_editor/src/lib/structure_conversions.py

64 lines
2.1 KiB

from browser import window
__pragma__ ('noanno')
__pragma__ ('js', """
var fs = require('fs');
var conversion_csv = fs.readFileSync('build/conversions.csv', 'utf8');
""", None)
# above is magically read (browserify plugin) at compile time
# browserify is run from root, so we need build/ in path for this to work
# convert to useful structure
structure_conversions = None
def build_structure_conversions():
global structure_conversions
structure_conversions = []
structure_conversions_raw = [line.split("|") for line in conversion_csv.split("\n")]
for line in structure_conversions_raw:
if min(len(line[0]), len(line[1])) == 0:
continue
# header
if line[1] == "struktura":
continue
vto_name = line[2].strip()
vto_id = line[4].strip()
if 0 in (len(vto_name), len(vto_id)):
continue
vfrom = "^" + line[0].replace("?", "\?").replace("%s", "([a-zA-Z螚ȎŠ-]+)") + "$"
structure_conversions.append((__new__(RegExp(vfrom, 'u')), vto_name, vto_id))
def convert_structure(structure, type):
if structure_conversions is None:
build_structure_conversions()
for vfrom, vto_name, vto_id in structure_conversions:
match = structure.match(vfrom)
# fix for ids 65, 66, 67 which instead matched with 64
if match and vto_id == '64' and '-s' in type:
vto_name = 's0-vp-s0'
vto_id = '65'
elif match and vto_id == '64' and '-g' in type:
vto_name = 'gg-vp-gg'
vto_id = '66'
elif match and vto_id == '64' and '-r' in type:
vto_name = 'r-vp-r'
vto_id = '67'
if match:
# we need to remove replace alias here as we want to use javascript's one
__pragma__('noalias', 'replace')
result = structure.replace(vfrom, vto_name).strip()
__pragma__('alias', 'replace', "py_replace")
return result, vto_id
window.console.log("Unknown structure: ", structure)
return 'N/A', '/'