hgt.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import os
  2. import sys
  3. import re
  4. import warnings
  5. from typing import List, Tuple, Dict
  6. FileList = List[str]
  7. FlatList = Dict[str, Tuple[str]]
  8. class RunError(Exception):
  9. def __init__(self, message="RunError: run as module is not allowed."):
  10. self.message = message
  11. class ArgError(Exception):
  12. def __init__(self, message="ArgError: just 6 arg is allowed."):
  13. self.message = message
  14. if __name__ != '__main__':
  15. raise RunError()
  16. argv = sys.argv
  17. if len(argv) < 6:
  18. print(f"{len(argv)}")
  19. raise ArgError()
  20. output_dir = argv[1]
  21. export_h = argv[2]
  22. export = argv[3]
  23. translation = argv[4]
  24. input_dir = argv[5:]
  25. translation_output = os.path.join(output_dir, "tr")
  26. base_tr_file = os.path.join(translation, "base.py")
  27. try:
  28. os.makedirs(output_dir)
  29. except OSError:
  30. ...
  31. try:
  32. os.makedirs(translation_output)
  33. except OSError:
  34. ...
  35. base_tr = {}
  36. if os.path.exists(base_tr_file):
  37. with open(base_tr_file, "r", encoding="utf-8") as f:
  38. code = f.read()
  39. exec(code, base_tr, base_tr)
  40. def getFileFromPath(paths: List[str], file_type_: str) -> FileList:
  41. tmp: FileList = []
  42. for path in paths:
  43. for file_path, dir_names, file_names in os.walk(path):
  44. for names in file_names:
  45. file_type = os.path.splitext(names)[-1]
  46. if file_type == file_type_:
  47. tmp.append(os.path.join(file_path, names))
  48. return tmp
  49. file_list: FileList = getFileFromPath(input_dir, '.c')
  50. pattern = re.compile(r'HT_getText\(([\S]+),[\s]*(\"[^\"]*\")\)')
  51. flat_list: FlatList = {}
  52. for file in file_list:
  53. with open(file, "r", encoding="utf-8") as f:
  54. data = f.readline()
  55. while data:
  56. result: List[str] = pattern.findall(data)
  57. for i in result:
  58. tmp = i[1]
  59. if tmp == "\"<base-tr>\"":
  60. tmp = f"\"{base_tr.get(i[0], None)}\""
  61. default_ = tmp.replace('\n', '\\n').replace('\r', '')
  62. if i[0] in flat_list:
  63. if flat_list[i[0]][0] != default_ and default_ != '""' and flat_list[i[0]][0] != '""':
  64. # 若果是空字串则可以覆盖
  65. warnings.warn(f"Double define text: {i[0]}")
  66. continue
  67. elif default_ == '""':
  68. default_ = flat_list[i[0]][0]
  69. flat_list[i[0]] = (default_,) # 生成一个数组
  70. data = f.readline()
  71. with open(os.path.join(output_dir, f"_ht.py"), "w", encoding="utf-8") as fpy:
  72. with open(os.path.join(output_dir, f"_ht.c"), "w", encoding="utf-8") as fc:
  73. with open(os.path.join(output_dir, f"_ht.h"), "w", encoding="utf-8") as fh:
  74. head = f'''/*
  75. * File: _ht.c/_ht.c
  76. * The file is automatically generated by Huan-GetText
  77. * Encoding: utf-8
  78. */'''
  79. fc.write(head + '\n\n')
  80. fc.write(f"#include \"_ht.h\"\n")
  81. fc.write("#undef HT_getText\n")
  82. fh.write(head + '\n\n')
  83. fh.write("#ifndef HT_GETTEXT_H\n")
  84. fh.write("#define HT_GETTEXT_H\n")
  85. fh.write("#ifdef HT_getText\n")
  86. fh.write("#error \"Double define HT_getText\"\n")
  87. fh.write("#endif\n")
  88. fh.write(f"#include \"{export_h}.h\"\n")
  89. fh.write("#define HT_getText(name, ...) ((char *)(HT_TEXT_ ## name))\n")
  90. fh.write(f"{export} int HT_initGetText(char *lang);\n")
  91. fpy.write("# Example for tr file" + '\n\n')
  92. for i in flat_list:
  93. fc.write(f"{export} const char *HT_TEXT_{i} = {flat_list[i][0]};\n")
  94. fh.write(f"{export} const char *HT_TEXT_{i};\n")
  95. fpy.write(f"# {i}: str = \"\" # {flat_list[i][0]}\n")
  96. fc.write('''\n
  97. /* define initGetText */
  98. /* need dlfcn or dlfcn-win32 */
  99. #include "dlfcn.h"
  100. #include "stdlib.h"
  101. static void *handle = NULL;
  102. static void destructExit(void) {
  103. dlclose(handle);
  104. }
  105. int HT_initGetText(char *lang) {
  106. if (lang == NULL || handle != NULL)
  107. return 2;
  108. handle = dlopen(lang, RTLD_NOW);
  109. if (handle == NULL)
  110. return 1;
  111. atexit(destructExit);
  112. char **tmp;\n\n''')
  113. for i in flat_list:
  114. fc.write(f''' tmp = dlsym(handle, "HT__TEXT_{i}");\n''')
  115. fc.write(f''' if (tmp != NULL) HT_TEXT_{i} = *tmp;\n\n''')
  116. fc.write(' return 0;\n}\n')
  117. fh.write("#endif\n")
  118. for i in flat_list:
  119. print(f"TEXT: {i}")
  120. translation_list: FileList = getFileFromPath([translation], '.py')
  121. for t in translation_list:
  122. name = os.path.splitext(os.path.split(t)[-1])[0]
  123. if name == 'base':
  124. continue
  125. print(f"tr: {name}")
  126. with open(t, "r", encoding="utf-8") as f:
  127. code = f.read()
  128. var = {}
  129. for i in flat_list:
  130. var[i] = flat_list[i][0][1:-2] # [1:-2] 去除引号
  131. exec(code, var, var)
  132. with open(os.path.join(translation_output, f"{name}.c"), "w", encoding="utf-8") as fc:
  133. fc.write(f"#include \"{export_h}.h\"\n")
  134. for i in flat_list: # 根据 flat_list 生成变量
  135. var[i].strip() # 去除首尾不需要的符号
  136. res = var[i].replace('\n', '\\n').replace('\r', '')
  137. fc.write(f"{export} const char *const HT__TEXT_{i} = \"{res}\";\n")