from __future__ import print_function
import os, re, shutil, json
import argparse
from generate_utils import OutFileGreen as OutFile
p = argparse.ArgumentParser()
p.add_argument('--files', nargs='*', help='to implement')
p.add_argument('-l', '--langs', nargs='+')
p.add_argument('--config', nargs='?', default={})
p.add_argument('--print-only', '--dry-run', action="store_true", help="doesn't create file, only print file concerned")
a = args = p.parse_args()
def warning(*args):
print('\033[33m' + '\aWarning:', *args, '\033[0m')
def info(*args):
print('\033[32m' + 'Info:', *args, '\033[0m')
def error(*args):
print('\033[31m' + '\aError:', *args, '\033[0m')
LANGS = args.langs
assert args.print_only or len(LANGS) == 2, "currently only 2 langs"
if args.config:
with open(args.config) as f:
config = json.load(f)
else:
config = {}
fileinfos = config.get('fileinfos', {})
RE = re.compile('''
^(.*) # filename
([._-])multilang # multilang tag
(?:(?:\\||_as_)([^.]*))? # name in other lang(s)
([.][^.]*)$
''', re.X)
RE_TR_TAG = re.compile('{{(.*?)}}', re.DOTALL)
TR_TAG_SEP = '|'
def tr_tag_test_matching(s):
S = []
i = 0
line = 1
while i < len(s) - 2:
if s[i] == s[i+1] == '{':
i += 2
S.append(line)
if s[i] == s[i+1] == '}':
i += 2
try:
S.pop()
except:
pass
else:
if s[i] == '\n':
line += 1
i += 1
if S:
raise ValueError(f'opening not closed: {S}')
def spliti(s:str, sep, i, default:str = None):
try:
return s.split(sep)[i]
except IndexError:
return default
modified = []
for f in filter(RE.match, os.listdir('.')):
if f == 'generate_multilang.py':
continue
if args.print_only:
print(f)
continue
fileinfo = fileinfos.get(f, {})
basename, multilang_symbol, othernames, ending = RE.match(f).groups()
othernames = re.split('\\||_as_', othernames) if othernames else []
with open(f) as fo:
s = fo.read()
try:
tr_tag_test_matching(s)
except ValueError as e:
warning('In {}: Translation tag not matching: {}'.format(f, str(e)))
continue
for i, lang in enumerate(fileinfo.get('langs', LANGS)):
has_other_name = i-1 in range(len(othernames))
has_info_name = 'name' in fileinfo and lang in fileinfo['name']
simple_name = basename + multilang_symbol + lang + ending
filename = (othernames[i-1] + ending if has_other_name else
fileinfo['name'][lang] if has_info_name else
simple_name)
if os.path.isfile(filename):
with open(filename, 'r') as fo:
prev_str = fo.read()
else:
prev_str = ''
next_str = RE_TR_TAG.sub(lambda m: spliti(m.group(1), TR_TAG_SEP, i, 'NOT TRANSLATED ' + spliti(m.group(1), TR_TAG_SEP, 0)), s)
if prev_str != next_str:
modified.append(filename)
with OutFile(filename) as fo:
fo.write(next_str)
if i == 0:
new = OutFile(basename + ending)
new.unlock()
shutil.copy(filename, new.filename)
new.lock()
info('Copy', filename, '->', '(read only)', new.filename)
del new
if filename != simple_name:
try:
new = OutFile(simple_name)
new.unlock()
shutil.copy(filename, simple_name)
new.lock()
print('Copy', filename, '->', '(read only)', simple_name)
del new
except ValueError:
print('simple_name exists and is writable')
print('multilang:', len(modified), 'file' + 's' * (len(modified) != 1) + ' modified' + ':' * bool(modified), ' '.join(modified))