65 lines
1.5 KiB
Python
Executable File
65 lines
1.5 KiB
Python
Executable File
#!/usr/bin/pyhton3
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
from lib.translate import translate
|
|
|
|
|
|
def scan_files(path) -> list:
|
|
"""
|
|
Scans dirs and subdirs in PATH to gind *.lua files
|
|
:param path: set path to scan
|
|
:return: list of files in dir and subdir
|
|
"""
|
|
list_files = []
|
|
for root, dirs, files in os.walk(path):
|
|
for file in files:
|
|
if '.lua' in file: list_files.append(f'{root}/{file}')
|
|
|
|
return list_files
|
|
|
|
|
|
def parsing_file(f):
|
|
with open(f) as _f:
|
|
_cached = _f.read()
|
|
|
|
_list = _cached.split('\n')
|
|
_to_translate = []
|
|
|
|
for _str in _list:
|
|
if _str.find('%_t') != -1 or _str.find('%_T') != -1:
|
|
_to_translate.append(_str)
|
|
|
|
return _to_translate
|
|
|
|
|
|
def pars_file(name: str) -> list:
|
|
"""
|
|
:param name: name of file
|
|
:return:
|
|
"""
|
|
list_to_translate: list = []
|
|
for _str in parsing_file(name):
|
|
string_to_translate = re.findall('"(.*[^/])(?: /\* (.*)\*/)?"%_[t|T]', _str)
|
|
list_to_translate.append(string_to_translate)
|
|
return list_to_translate
|
|
|
|
|
|
def all_to_translate(_dir):
|
|
_files = scan_files(_dir)
|
|
for _file in _files:
|
|
_strings = pars_file(_file)
|
|
if len(_strings) > 0: _file, to_file = '\n', _strings, '\n', len(_strings), '\n\n\n'
|
|
with open('test.txt', 'w') as f:
|
|
f.write(to_file)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) > 1:
|
|
arg = sys.argv[1]
|
|
else:
|
|
arg = r'C:\Users\riksl\AppData\Roaming\Avorion\mods\WPE2'
|
|
|
|
all_to_translate(arg)
|