This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathinstall-dicts.py
executable file
·98 lines (89 loc) · 3.46 KB
/
install-dicts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
import sys
import xml.etree.ElementTree as ET
import os
import shutil
import subprocess
def getName(elem):
return elem.attrib['{http://openoffice.org/2001/registry}name']
def getType(elem):
return elem.attrib['{http://openoffice.org/2001/registry}type']
spell_dest = "hunspell"
hyph_dest = "hyphen"
thes_dest = "mythes"
def parseProps(elem, origin):
props = {}
for prop in elem.getchildren():
prop_name = getName(prop)
prop_type = getType(prop)
res = None
if prop_type == "xs:string":
res = ""
for i in prop.getchildren()[0].itertext():
res = res + i.replace("%origin%", origin)
elif prop_type == "oor:string-list":
res = []
for i in prop.getchildren()[0].itertext():
res = res + i.replace("%origin%", origin).split()
else:
print("Unknown type %s" % prop_type)
props[prop_name] = res;
return props
def handle_file(filename):
tree = ET.parse(filename)
lang = os.path.basename(os.path.dirname(filename)).split("_")[0]
root = tree.getroot()
dicts = root.getchildren()[0].getchildren()[0].getchildren()
for dict in dicts:
name = getName(dict)
props = parseProps(dict, os.path.dirname(filename))
format = props['Format']
print ("Installing %s dictionary %s for lang %s:" % (format, name, lang))
if format == 'DICT_SPELL':
dest = spell_dest
prefix = ""
suffix = ""
elif format == 'DICT_HYPH':
dest = hyph_dest
prefix = "hyph_"
suffix = ""
elif format == 'DICT_THES':
dest = thes_dest
prefix = "th_"
suffix = "_v2"
else:
print ("Unknown format %s" % format)
continue
full_dest = "/usr/share/runtime/locale/" + lang + "/" + dest + "/"
symlink_dest = "/usr/share/" + dest + "/"
os.makedirs(full_dest, exist_ok = True)
os.makedirs(symlink_dest, exist_ok = True)
for file in props['Locations']:
if format == 'DICT_THES' and file.endswith(".dat") and os.path.isfile(file):
idxname = file.replace(".dat", ".idx")
if not os.path.isfile(idxname):
print (" Generate %s from %s" % (idxname, file))
f_in = open(file, "r")
f_out = open(idxname, "w")
subprocess.run(["./th_gen_idx.pl"], stdin=f_in, stdout=f_out)
if not (idxname in props['Locations']):
props['Locations'].append(idxname)
for file in props['Locations']:
if not os.path.isfile(file):
continue
f, ext = os.path.splitext(file)
basename = os.path.basename(file)
dirname = os.path.dirname(file)
if lang != "en":
full_dest_file = full_dest + basename
else:
full_dest_file = symlink_dest + basename
print (" copy %s to %s" % (file, full_dest_file))
shutil.copyfile(file, full_dest_file)
for loc in props['Locales']:
symlink = symlink_dest + prefix + loc.replace("-", "_")+suffix+ext
if symlink != full_dest_file:
print (" symlink %s to %s" % (symlink, full_dest_file))
os.symlink(full_dest_file, symlink)
for i in sys.argv[1:]:
handle_file(i)