Add tool for update spell/hyphen dictionaries info

This commit is contained in:
Oleg Korshul
2025-05-26 22:44:54 +03:00
parent 088d8fa9dd
commit 572e2e20da

84
dictionaries/update.py Normal file
View File

@ -0,0 +1,84 @@
import os
import glob
import json
import subprocess
import platform
curDirectory = os.path.dirname(os.path.realpath(__file__))
dictionatiesDirectory = curDirectory + "/../../dictionaries"
all_dictionaties = {}
for dir in glob.glob(dictionatiesDirectory + "/*"):
if not os.path.isdir(dir):
continue
dictionaryName = os.path.basename(dir)
configFile = dictionatiesDirectory + "/" + dictionaryName + "/" + dictionaryName + ".json"
if not os.path.isfile(configFile):
continue
isHyphen = False
hyphenFile = dictionatiesDirectory + "/" + dictionaryName + "/hyph_" + dictionaryName + ".dic"
if os.path.isfile(hyphenFile):
isHyphen = True
with open(configFile, 'r', encoding='utf-8') as file:
data = json.loads(file.read())
for lang in data["codes"]:
all_dictionaties[str(lang)] = {
"name": dictionaryName,
"hyphen": isHyphen
}
all_dictionaties_content = json.dumps(all_dictionaties, separators=(",", ":"), sort_keys=True)
sdkjsDirectory = dictionatiesDirectory + "/../sdkjs"
filesReplace = [
sdkjsDirectory + "/common/spell/spell.js"
]
sdkjsDirectory = dictionatiesDirectory + "/../sdkjs"
for dir in glob.glob(sdkjsDirectory + "/*"):
if not os.path.isdir(dir):
continue
editorName = os.path.basename(dir)
testFile = sdkjsDirectory + "/" + editorName + "/sdk-all-min.js"
if (os.path.isfile(testFile)):
filesReplace.append(testFile)
testFile = sdkjsDirectory + "/" + editorName + "/sdk-all.js"
if (os.path.isfile(testFile)):
filesReplace.append(testFile)
for file in filesReplace:
if not os.path.isfile(file):
continue
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
startFind = "spellcheckGetLanguages"
startReplace = "function(){"
endFind = "};"
startIndex = content.find(startFind)
if startIndex == -1:
continue
startReplaceIndex = content.find(startReplace, startIndex)
if startReplaceIndex == -1:
continue
endIndex = content.find(endFind, startIndex)
if endIndex == -1:
continue
if content[endIndex + 2:endIndex + 4] == "};":
endIndex += 2
content = content[:startReplaceIndex + len(startReplace)] + "return " + all_dictionaties_content + endFind + content[endIndex + len(endFind):]
with open(file, 'w', encoding='utf-8') as f:
f.write(content)
testDevelopVersion = sdkjsDirectory + "/.git"
if not os.path.isdir(testDevelopVersion):
print("Not in develop version, skipping x2t cache update")
x2tBin = curDirectory + "/../FileConverter/bin/x2t"
if ("windows" == platform.system().lower()):
x2tBin += ".exe"
subprocess.call([x2tBin, "-create-js-cache"], stderr=subprocess.STDOUT, shell=True)
command = x2tBin + " -create-js-cache"
subprocess.call(command, stderr=subprocess.STDOUT, shell=True)