-
Notifications
You must be signed in to change notification settings - Fork 63
/
import-from-fakevim-ts.py
executable file
·141 lines (124 loc) · 3.65 KB
/
import-from-fakevim-ts.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
import sys
import re
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import NamedTuple
class TsItem (NamedTuple):
name : str
fullLang : str
lang : str
place : str
def createTsItem(name):
fullLang = ""
lang = ""
place = ""
m = re.search("[^_]*_([a-zA-Z_]+)\.ts", name)
if m:
fullLang = m[1].lower()
lang = ""
place = ""
if "_" in fullLang:
lang = fullLang[:fullLang.index("_")]
place = fullLang[fullLang.index("_")+1:]
else:
lang = fullLang
return TsItem(name=name, fullLang=fullLang, lang=lang, place=place)
if len(sys.argv) != 3:
sys.stderr.write("invalid parameters\n")
sys.exit(-1)
sourceTsDir = sys.argv[1]
destTsDir = sys.argv[2]
destTsItems = []
p = Path(destTsDir)
for f in [x for x in p.glob("fakevim_*.ts")
if x.is_file()]:
destTsItems.append(createTsItem(f.name))
srcTsItems = []
p = Path(sourceTsDir)
for f in [x for x in p.glob("qtcreator_*.ts")
if x.is_file()]:
srcTsItems.append(createTsItem(f.name))
# Match src and dest files
matchTuples = []
for dest in destTsItems:
best = TsItem("", "", "", "")
for src in (x for x in srcTsItems if dest.lang == x.lang):
if dest.place == src.place:
best = src
break
best = src
if best.name:
matchTuples.append( (best, dest) )
# Process FakeVim context elements
def getContextName(context):
n = context[0]
assert n.tag == "name"
return n.text
def getMsgSource(msg):
for child in msg:
if child.tag != "source":
continue
return child.text
return None
def findMessage(context, msgSrc):
for msg in context:
if msg.tag != "message":
continue
srcText = getMsgSource(msg)
if srcText == msgSrc:
return msg
return None
def findTranslation(msg):
for e in msg:
if e.tag != "translation":
continue
return e
return None
def updateMessages(srcContext, destContext):
for destMsg in destContext:
if destMsg.tag != "message":
continue
foundMsgInSrc = findMessage(srcContext, getMsgSource(destMsg))
if foundMsgInSrc == None:
continue
srcTr = findTranslation(foundMsgInSrc)
assert srcTr != None
destTr = findTranslation(destMsg)
assert destTr != None
destTr.text = srcTr.text
destMsg.remove(destTr)
destMsg.append(srcTr)
print("updated: " + getMsgSource(destMsg))
class EditedTsFile:
def __init__(self, filePath):
self.tree = ET.parse(filePath)
self.root = self.tree.getroot()
def findContext(self, name):
for context in self.root:
if getContextName(context) == name:
return context
return None
def deleteContext(self, name):
for context in root:
if getContextName(context) == name:
self.root.remove(context)
def addContext(self, context):
self.root.append(context)
def dump(self):
ET.dump(self.root)
def write(self, path):
self.tree.write(path, encoding="UTF8")
for src, dest in matchTuples:
destTs = EditedTsFile(Path(destTsDir) / dest.name)
p = Path(sourceTsDir) / src.name
root = ET.parse(p).getroot()
for srcCtx in root:
name = getContextName(srcCtx)
if not name.startswith("FakeVim"):
continue
destContext = destTs.findContext("FakeVim")
if destContext == None:
continue
updateMessages(srcCtx, destContext)
destTs.write(Path("tmp") / dest.name)