Added basic files

This commit is contained in:
Antonio de la Rosa 2019-12-15 20:40:51 +01:00
commit 114342bc9b
16 changed files with 1320 additions and 0 deletions

View file

View file

@ -0,0 +1,96 @@
#!/usr/bin/env python3
from importlib import import_module
#from paramecio.citoplasma.sessions import get_session
import json
from flask import session
yes_session=False
i18n_module={}
def load_lang(*args):
for module in args:
lang_path=module[0]+'.i18n.'+module[1]
try:
i18n_module[lang_path]=import_module(lang_path)
pass
except:
pass
# here load the language
class I18n:
default_lang='en-US'
dict_i18n=['en-US', 'es-ES']
l={}
#@staticmethod
#def set_lang(code_lang):
# if default_lang
@staticmethod
def get_default_lang():
lang=I18n.default_lang
s=session
lang=s.get('lang', lang)
return lang
@staticmethod
def lang(module, symbol, text_default, lang=None):
if not lang:
lang=I18n.get_default_lang()
I18n.l[lang]=I18n.l.get(lang, {})
I18n.l[lang][module]=I18n.l[lang].get(module, {})
I18n.l[lang][module][symbol]=I18n.l[lang][module].get(symbol, text_default)
return I18n.l[lang][module][symbol]
@staticmethod
def extract_value(value):
value=json.loads(value)
lang=I18n.get_default_lang()
if value[lang]!='':
return value[lang]
return value[I18n.default_lang]
"""
@staticmethod
def get_browser_lang():
return request.headers.get('Accept-Language', 'en-US')
"""
@staticmethod
def lang_json(module, symbol, text_default):
arr_final={}
for l in I18n.dict_i18n:
arr_final[l]=I18n.lang(module, symbol, text_default, l)
return json.dumps(arr_final)

View file

@ -0,0 +1,97 @@
# Template frontend from mako.
from mako.template import Template
from flask import session
from mako.lookup import TemplateLookup
from os import path
from settings import config
import gettext
import sys
from paramecio2.libraries.i18n import I18n
def env_theme(module, cache_enabled=True, cache_impl='', cache_args={}, module_directory="./tmp/modules"):
ext=module[len(module)-3:]
if ext=='.py':
module=path.dirname(module)
standard_templates=path.dirname(__file__)+'/templates'
module_directory+='/'+module
module_templates=module+'/templates'
theme_templates='themes/'+config.theme+'/templates'
search_folders=[theme_templates, module_templates, standard_templates]
#if self.inject_folder is not None:
#search_folders.insert(1, self.inject_folder+'/templates')
#Standard templates
#print(standard_templates)
return TemplateLookup(directories=search_folders, default_filters=['h'], input_encoding='utf-8', encoding_errors='replace', cache_enabled=cache_enabled, cache_impl=cache_impl, cache_args=cache_args, module_directory=module_directory, filesystem_checks=config.reloader)
class PTemplate:
templates_loaded={}
def __init__(self, environment, prepare_gettext=True):
self.autoescape_ext=('html', 'htm', 'xml', 'phtml', 'js')
self.env=environment
self.filters={}
self.add_filter(I18n.lang)
"""
def gettext(self, text):
return gettext.dgettext(self.domain_gettext, text)
"""
def load_template(self, template_file, **arguments):
"""
if self.prepare_gettext:
module_lang_dir=self.env.directories[1].replace('/templates', '/locales')
module_name=path.basename(module_lang_dir.replace('/locales', ''))
en = gettext.translation(module_name, localedir=module_lang_dir, languages=['en'])
en.install()
_=en.gettext
arguments.update({'_': _})
"""
template = self.env.get_template(template_file)
arguments.update(self.filters)
return template.render(**arguments)
def guess_autoescape(self, template_name):
if template_name is None or '.' not in template_name:
return False
ext = template_name.rsplit('.', 1)[1]
return ext in self.autoescape_ext
def render_template(self, template_file, **arguments):
if not str(self.env.directories)+'/'+template_file in PTemplate.templates_loaded:
PTemplate.templates_loaded[str(self.env.directories)+'/'+template_file]=self.env.get_template(template_file)
arguments.update(self.filters)
return PTemplate.templates_loaded[str(self.env.directories)+'/'+template_file].render(**arguments)
def add_filter(self, filter_name):
self.filters[filter_name.__name__]=filter_name