PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lang/updatelang.py

https://gitlab.com/gumiko/evol-tools
Python | 391 lines | 371 code | 14 blank | 6 comment | 35 complexity | 96912f47da292be0ca7a924d622ba884 MD5 | raw file
  1. #! /usr/bin/env python2.7
  2. # -*- coding: utf8 -*-
  3. #
  4. # Copyright (C) 2010-2015 Evol Online
  5. # Author: Andrei Karas (4144)
  6. import os
  7. import re
  8. defaultLang = "en"
  9. filt = re.compile(".+[.]txt", re.IGNORECASE)
  10. allStrings = set()
  11. strComments = dict()
  12. strre1 = re.compile("[\t +(]l[(][\"](?P<str>[^\"]+)[\"]")
  13. strre3 = re.compile("[\t +(]getitemlink[(][\"](?P<str>[^\"]+)[\"][)]")
  14. strre2 = re.compile("^[^/](.+)([^\t]+)[\t](script|shop|trader|cashshop)[\t](?P<str>[^\t]+)[\t]([\w\d]+),")
  15. strre4 = re.compile("[\t +(]lg[(][\"](?P<str>[^\"]+)[\"][)]")
  16. strre5 = re.compile("[\t +(]getitemname[(][\"](?P<str>[^\"]+)[\"][)]")
  17. strre6 = re.compile("[\t ]mesn[ ][\"](?P<str>[^\"]+)[\"]")
  18. strre7 = re.compile("[\t +(]lg[(][\"](?P<str1>[^\"]+)[\"],([ ]*)[\"](?P<str2>[^\"]+)[\"]")
  19. itemsplit = re.compile(",")
  20. langFiles = dict()
  21. oldLangFiles = dict()
  22. langs = set()
  23. itemNamesByName = dict()
  24. def addStr(text, comment, fileName, lineNum, addNoC):
  25. text = text.replace("\a", "\"");
  26. allStrings.add(text)
  27. if comment[-1:] == "\n":
  28. comment = comment[:-1]
  29. if text not in strComments:
  30. strComments[text] = set()
  31. strComments[text].add("#. code: " + comment.strip() + "\n")
  32. strComments[text].add("#: " + fileName + ":" + str(lineNum) + "\n")
  33. if addNoC == True:
  34. strComments[text].add("#, no-c-format\n")
  35. def collectScriptFileStrings(codeFile, file2):
  36. global itemNamesByName
  37. with open(file2, "r") as f:
  38. cnt = -1
  39. for line in f:
  40. cnt = cnt + 1
  41. line = line.replace("\\\"", "\a");
  42. m = strre1.findall(line)
  43. if len(m) > 0:
  44. for str in m:
  45. addStr(str, line, codeFile, cnt, True)
  46. m = strre4.findall(line)
  47. if len(m) > 0:
  48. for str in m:
  49. addStr(str + "#0", line, codeFile, cnt, True)
  50. addStr(str + "#1", line, codeFile, cnt, True)
  51. m = strre2.search(line)
  52. if m is not None and m.group("str")[0] != "#":
  53. addStr(m.group("str"), line, codeFile, cnt, True)
  54. m = strre3.findall(line)
  55. if len(m) > 0:
  56. for str in m:
  57. if str.lower() in itemNamesByName:
  58. addStr(itemNamesByName[str.lower()], line, codeFile, cnt, True)
  59. m = strre5.findall(line)
  60. if len(m) > 0:
  61. for str in m:
  62. if str.lower() in itemNamesByName:
  63. addStr(itemNamesByName[str.lower()], line, codeFile, cnt, True)
  64. m = strre6.findall(line)
  65. if len(m) > 0:
  66. for str in m:
  67. addStr(str, line, codeFile, cnt, True)
  68. m = strre7.findall(line)
  69. if len(m) > 0:
  70. for str in m:
  71. addStr(str[0] + "#0", line, codeFile, cnt, True)
  72. addStr(str[2] + "#1", line, codeFile, cnt, True)
  73. def collectScriptStrings(parentDir, relativeDir):
  74. files = os.listdir(parentDir)
  75. for file1 in files:
  76. if file1[0] == ".":
  77. continue
  78. file2 = os.path.abspath(parentDir + os.path.sep + file1)
  79. relativeDir2 = relativeDir + os.path.sep + file1
  80. if not os.path.isfile(file2):
  81. collectScriptStrings(file2, relativeDir2)
  82. elif filt.search(file1):
  83. codeFile = relativeDir + os.path.sep + file1
  84. collectScriptFileStrings(codeFile, file2)
  85. def collectMessages(messagesDir):
  86. with open(messagesDir, "r") as r:
  87. cnt = -1
  88. for line in r:
  89. cnt = cnt + 1
  90. if line[:2] == "//":
  91. continue
  92. idx = line.find(": ")
  93. if idx < 1:
  94. continue
  95. # msgId = line[:idx]
  96. msgStr = line[idx + 2:]
  97. if msgStr[-1:] == "\n":
  98. msgStr = msgStr[:-1]
  99. addStr(msgStr, line, "conf/messages.conf", cnt, False)
  100. def loadFiles(dir):
  101. with open(dir + "/langs.txt", "r") as f:
  102. for line in f:
  103. langs.add(line[:-1])
  104. for file in langs:
  105. langFiles[file] = parseFile(dir + "/lang_" + file + ".txt", True)
  106. oldLangFiles[file] = parseFile(dir + "/lang_" + file + ".old", False)
  107. def parseFile(name, readFirstLine):
  108. trans = dict()
  109. firstLine = None
  110. if os.path.exists(name):
  111. with open(name, "r") as f:
  112. line1 = "";
  113. line2 = "";
  114. for line in f:
  115. if readFirstLine is True and firstLine is None:
  116. firstLine = line
  117. continue
  118. if (line == ""):
  119. line1 = ""
  120. line2 = ""
  121. continue
  122. elif (line1 == ""):
  123. line1 = line[:-1]
  124. continue
  125. line2 = line[:-1]
  126. trans[line1] = line2
  127. line1 = ""
  128. line2 = ""
  129. elif readFirstLine:
  130. firstLine = "Copyright (C) 2010-2015 Evol Online\n"
  131. return (trans, firstLine)
  132. def loadPoFiles(podir):
  133. files = os.listdir(podir)
  134. for name in files:
  135. if name[-3:] == ".po":
  136. parsePoFile(name[:-3], podir + "/" + name)
  137. def parsePoFile(name, path):
  138. langFile = langFiles[name][0]
  139. with open(path, "r") as f:
  140. flag = 0
  141. line1 = ""
  142. line2 = ""
  143. for line in f:
  144. if flag == 0:
  145. idx = line.find ("msgid ")
  146. if idx == 0:
  147. line2 = ""
  148. line1 = line[len("msgid "):]
  149. line1 = line1[1:len(line1) - 2]
  150. flag = 1
  151. elif flag == 1:
  152. idx = line.find ("msgstr ")
  153. if idx == 0:
  154. line2 = line[len("msgstr "):]
  155. line2 = line2[1:len(line2) - 2]
  156. flag = 2
  157. if line == "\n":
  158. if flag == 2:
  159. if line1 != "":
  160. if line1 in langFile and line2 != "":
  161. langFile[line1] = line2.replace("\\\"", "\"");
  162. flag = 0
  163. idx = line.find ("\"")
  164. if idx == 0:
  165. line = line[1:len(line) - 2]
  166. if flag == 1:
  167. line1 = line1 + line
  168. elif flag == 2:
  169. line2 = line2 + line
  170. def addMissingLines():
  171. for trans in langFiles:
  172. newFile = langFiles[trans][0]
  173. oldFile = oldLangFiles[trans][0]
  174. for str in newFile:
  175. if str not in allStrings:
  176. # if newFile[str] != "":
  177. oldFile[str] = newFile[str]
  178. for str in oldFile:
  179. if str in newFile:
  180. del newFile[str]
  181. # print trans + ":moved to old: " + str
  182. for str in allStrings:
  183. for trans in langFiles:
  184. newFile = langFiles[trans][0]
  185. oldFile = oldLangFiles[trans][0]
  186. if str not in newFile:
  187. # print "lang: " + trans + ", str: " + str
  188. if trans == defaultLang:
  189. # print "newFile[str] = str"
  190. newFile[str] = str
  191. elif str in oldFile:
  192. newFile[str] = oldFile[str]
  193. # print "newFile[str] = oldFile[str]: " + newFile[str]
  194. else:
  195. # print "newFile[str] = """
  196. newFile[str] = ""
  197. # print trans + ":new string: " + str
  198. def sorting():
  199. for trans in langFiles:
  200. newFile = langFiles[trans]
  201. newFile = sortDict (newFile)
  202. langFiles[trans] = newFile
  203. oldFile = oldLangFiles[trans]
  204. oldFile = sortDict (oldFile)
  205. oldLangFiles[trans] = oldFile
  206. def sortDict(adict):
  207. d2 = []
  208. keys = adict[0].keys()
  209. keys.sort()
  210. for key in keys:
  211. d2.append ((key, adict[0][key]))
  212. return (d2, adict[1])
  213. def saveFiles(langDir, poDir):
  214. for trans in langFiles:
  215. writeLangFile (langDir + "/lang_" + trans + ".txt", langFiles[trans], trans, False)
  216. writeLangFile (langDir + "/lang_" + trans + ".old", oldLangFiles[trans], trans, True)
  217. writePoFile (poDir, langFiles[trans], trans)
  218. def writeLangFile(langDir, texts, trans, isold):
  219. with open (langDir, "w") as f:
  220. if texts[1] is not None:
  221. f.write(texts[1])
  222. for line in texts[0]:
  223. if not isold or (line[1] is not None and len(line[1]) > 0):
  224. f.write (line[0] + "\n")
  225. trLine = line[1]
  226. if trans == "en":
  227. if len(trLine) > 2 and (trLine[-2:] == "#0" or trLine[-2:] == "#1"):
  228. trLine = trLine[:-2]
  229. f.write (trLine + "\n\n")
  230. def writePoComments(w, comments):
  231. for line in comments:
  232. if line[:3] == "#. ":
  233. w.write (line)
  234. for line in comments:
  235. if line[:3] == "#: ":
  236. w.write (line)
  237. for line in comments:
  238. if line[:3] == "#, ":
  239. w.write (line)
  240. break
  241. def writePoFile(poDir, texts, trans):
  242. if trans == "en":
  243. langDir = poDir + "/" + trans + ".pot"
  244. else:
  245. langDir = poDir + "/" + trans + ".po"
  246. print langDir
  247. with open (langDir, "w") as w:
  248. w.write ("# " + texts[1] + "")
  249. w.write ("#\n\n")
  250. w.write ("msgid \"\"\n")
  251. w.write ("msgstr \"\"\n")
  252. w.write ("\"Project-Id-Version: EvolOnline\\n\"\n")
  253. w.write ("\"MIME-Version: 1.0\\n\"\n")
  254. w.write ("\"Content-Type: text/plain; charset=UTF-8\\n\"\n")
  255. w.write ("\"Content-Transfer-Encoding: 8bit\\n\"\n")
  256. w.write ("\n")
  257. for line in texts[0]:
  258. if line[0] in strComments.keys():
  259. writePoComments(w, strComments[line[0]])
  260. srcLine = line[0]
  261. srcLine = srcLine.replace("\\", "\\\\");
  262. srcLine = srcLine.replace("\"", "\\\"")
  263. w.write ("msgid \"" + srcLine + "\"\n")
  264. trLine = line[1]
  265. if trans == "en":
  266. trLine = ""
  267. trLine = trLine.replace("\\", "\\\\");
  268. trLine = trLine.replace("\"", "\\\"")
  269. w.write ("msgstr \"" + trLine + "\"\n\n")
  270. def stripQuotes(data):
  271. if len(data) == 0:
  272. return data
  273. if data[-1] == "\"":
  274. data = data[:-1]
  275. if data[0] == "\"":
  276. data = data[1:]
  277. if data[-1] == "'":
  278. data = data[:-1]
  279. if data[0] == "'":
  280. data = data[1:]
  281. return data
  282. def loadItemDb(dir):
  283. global itemNamesByName
  284. with open(dir + "/item_db.conf", "r") as f:
  285. cnt = -1
  286. for line in f:
  287. line = line.strip()
  288. idx = line.find("Name: \"")
  289. if idx != 0 or line[len(line) - 1] != "\"":
  290. continue
  291. addStr(line[idx + 7: len(line) - 1], line, "item_db.conf", cnt, True)
  292. if line == "{":
  293. itemId = ""
  294. itemName = ""
  295. for line in f:
  296. line = line.strip()
  297. if len(line) < 1:
  298. continue
  299. if line[0] == "}":
  300. if itemId != "" and itemName != "":
  301. itemNamesByName[itemName.lower()] = itemName
  302. break
  303. if len(line) > 6 and line[:5] == "Name:":
  304. itemName = stripQuotes(line[5:].strip())
  305. if len(line) > 4 and line[:3] == "Id:":
  306. itemId = stripQuotes(line[3:].strip())
  307. def dumpTranslations():
  308. for trans in oldLangFiles:
  309. print "old lang: " + trans
  310. newFile = oldLangFiles[trans][0]
  311. for line in newFile:
  312. print line
  313. if line in newFile:
  314. print newFile[line]
  315. print "\n"
  316. for trans in langFiles:
  317. print "new lang: " + trans
  318. newFile = langFiles[trans][0]
  319. for line in newFile:
  320. print line
  321. if line in newFile:
  322. print newFile[line]
  323. print "\n"
  324. def loadMobNames(dir):
  325. with open(dir + "/mob_db.conf", "r") as r:
  326. cnt = -1
  327. for line in r:
  328. cnt = cnt + 1
  329. if len(line) < 1 or line[0:2] == "//":
  330. continue
  331. line = line.strip()
  332. idx = line.find("Name: \"")
  333. if idx != 0 or line[len(line) - 1] != "\"":
  334. continue
  335. addStr(line[idx + 7: len(line) - 1], line, "mob_db.conf", cnt, True)
  336. rootPath = "../../server-data/"
  337. loadItemDb(rootPath + "db/re")
  338. loadMobNames(rootPath + "db/re")
  339. collectScriptStrings(rootPath + "/npc", "npc")
  340. collectScriptFileStrings("db/re/item_db.conf", rootPath + "/db/re/item_db.conf")
  341. collectMessages(rootPath + "/conf/messages.conf")
  342. loadFiles(rootPath + "/langs")
  343. addMissingLines()
  344. loadPoFiles("in");
  345. #dumpTranslations();
  346. sorting()
  347. saveFiles(rootPath + "/langs", "out")