From fbb2e1012cce9b0907e9e6290ef5f2053cb173a2 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Sun, 11 Jul 2021 19:57:25 +0200 Subject: [PATCH] Added check_i18n --- paramecio2/libraries/check_i18n.py | 203 +++++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 paramecio2/libraries/check_i18n.py diff --git a/paramecio2/libraries/check_i18n.py b/paramecio2/libraries/check_i18n.py new file mode 100644 index 0000000..e80cbb6 --- /dev/null +++ b/paramecio2/libraries/check_i18n.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python3 + +import argparse +import os, sys + +sys.path.insert(0, os.path.realpath('.')) + +import re +from pathlib import Path +from importlib import import_module +from paramecio2.libraries.i18n import I18n +from settings import config + +pattern=re.compile('^\w+\.(py|html|phtml|js)$') + +ignored=re.compile('^[__|\.].*$') + +lang_p=re.compile("I18n\.lang\('(.*?)',\s+'(.*?)',\s+'(.*?)'\)") +lang_t=re.compile("\${lang\('(.*?)',\s+'(.*?)',\s+'(.*?)'\)\}") + +tmp_lang={} + +def start(): + + global lang_p + global lang_t + + # Module to search a file where save the file. + + parser = argparse.ArgumentParser(description='A tool for create python language files') + + parser.add_argument('--module', help='The module where search lang files', required=False) + + args = parser.parse_args() + + #Without arguments, search in paramecio directory all language files + + path_save='paramecio/i18n' + + path='paramecio' + + if args.module!=None: + + path=args.module + + path_save=args.module+'/i18n' + + module_base=os.path.basename(args.module) + + lang_p=re.compile("I18n\.lang\('("+module_base+"?)',\s+'(.*?)',\s+'(.*?)'\)") + #lang_t=re.compile("\${lang\('("+module_base+"?)',\s+'(.*?)',\s+'(.*?)'\)\}") + lang_t=re.compile("lang\('("+module_base+"?)',\s+'(.*?)',\s+'(.*?)'\)") + + if not os.path.isdir(path): + + print("Error: directory to scan doesn't exists") + + exit(1) + + scandir(path, path_save) + + #Save the files + + file_lang='' + + for module in tmp_lang.keys(): + + # Save in a file + real_path=path_save + + if not os.path.isdir(real_path): + + p=Path(real_path) + p.mkdir(0o755, True) + + try: + path_module=path_save.replace('/', '.')+'.'+module + + import_module(path_module) + + #Add values to tmp lang + + #for real_key, real_text in I18n.l[lang][module].items(): + #tmp_lang[module][real_key]=real_text + + except: + pass + + + + i=Path(real_path+'/__init__py') + + i.touch(0o644) + + file_lang="#!/usr/bin/env python3\n\n" + + file_lang+="from paramecio2.libraries.i18n import I18n\n\n" + + for lang in I18n.dict_i18n: + + #I18n.l['en-US']['admin']=I18n.l['en-US'].get('admin', {}) + + file_lang+="I18n.l['%s']=I18n.l.get('%s', {})\n\n" % (lang, lang) + + file_lang+="I18n.l['"+lang+"']['"+module+"']=I18n.l['"+lang+"'].get('"+module+"', {})\n\n" + + I18n.l[lang]=I18n.l.get(lang, {}) + + I18n.l[lang][module]=I18n.l[lang].get(module, {}) + + for key, text in tmp_lang[module].items(): + + if not key in I18n.l[lang][module]: + + I18n.l[lang][module][key]=text + + file_lang+="I18n.l['"+lang+"']['"+module+"']['"+key+"']='"+I18n.l[lang][module][key].replace("'", "\\'")+"'\n\n" + + final_file=real_path+'/'+module+'.py' + + f=open(final_file, 'w') + + f.write(file_lang) + + f.close() + + pass + +def scandir(path, module_search=''): + + list=os.listdir(path) + + for name in list: + + new_path=path+'/'+name + + if os.path.isdir(new_path): + if ignored.match(name)==None: + scandir(new_path) + elif pattern.match(name)!=None and ignored.match(name)==None: + + f=open(new_path) + + for line in f: + + #[('pokermind', 'performance_questions_default', 'Performance questions default'), ('pokermind', 'performance_questions_defasult', 'Performance questions defaufflt')] + + match_p=lang_p.findall(line) + match_t=lang_t.findall(line) + + if match_p!=None: + + for m in match_p: + module=m[0] + symbol=m[1] + text_default=m[2] + + tmp_lang[module]=tmp_lang.get(module, {}) + + tmp_lang[module][symbol]=tmp_lang[module].get(symbol, text_default) + + if match_t!=None: + """ + module=match_t.group(1) + symbol=match_t.group(2) + text_default=match_t.group(3) + + tmp_lang[module]=tmp_lang.get(module, {}) + + tmp_lang[module][symbol]=tmp_lang[module].get(symbol, text_default) + """ + + for m in match_t: + module=m[0] + symbol=m[1] + text_default=m[2] + + tmp_lang[module]=tmp_lang.get(module, {}) + + tmp_lang[module][symbol]=tmp_lang[module].get(symbol, text_default) + + + f.close() + + #print('archivo->'+path+'/'+name) + # Open file + # obtain modules, keys, and default text + + #Open all files in path specified. If not specified, see in all files recursively in path. + + #Extract lang and I18n.lang and fill i18n property that save the values of language texts, only extracs key specified in option key, if not specified, extract last member of path key. + + # Open all language files in a loop with modules from dictionary created from open files, if module path is not specified, the file is in paramecio.i18n. With module path the language files are saved in i18n directory into the path, for example if path is modules/panel, the files are saved in modules/panel/i18n/example.py. If key option is saved then only saved lang with keys selected. Normally you only need a file by module and by default. key option is the last member of path. For example, you can create a language file for a module and use in other module, but don't extract key used in the other module language file used. + + # THe array i18n is overwrited loading the lang files. + + # Save the files in specified path. + +if __name__=='__main__': + + start() + + diff --git a/setup.py b/setup.py index 6552454..14aa045 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ setup(name='paramecio2', include_package_data=True, install_requires=['flask', 'pymysql', 'sqlalchemy', 'colorama', 'python-slugify', 'mako', 'pillow', 'arrow'], entry_points={'console_scripts': [ - 'paramecio2 = paramecio2.console:start', 'paramecio2db = paramecio2.libraries.db.dbadmin:start', + 'paramecio2 = paramecio2.console:start', 'paramecio2db = paramecio2.libraries.db.dbadmin:start', 'paramecio2lang = paramecio2.libraries.check_i18n:start', ]}, zip_safe=False, license='GPLV3',