132 lines
4.5 KiB
Python
132 lines
4.5 KiB
Python
"""Legacy Creole engine
|
|
|
|
Created by:
|
|
EOLE (http://eole.orion.education.fr)
|
|
Copyright (C) 2005-2018
|
|
|
|
Forked by:
|
|
Cadoles (http://www.cadoles.com)
|
|
Copyright (C) 2021
|
|
|
|
distribued with GPL-2 or later license
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
"""
|
|
|
|
|
|
from typing import Dict, Any
|
|
from Cheetah.NameMapper import NotFound
|
|
|
|
from .creole import CheetahTemplate as oriCheetahTemplate
|
|
from ...i18n import _
|
|
from ...utils import normalize_family
|
|
from ...error import TemplateError
|
|
|
|
|
|
@classmethod
|
|
def cl_compile(kls, *args, **kwargs):
|
|
"""Rewrite compile methode to force some settings
|
|
"""
|
|
kwargs['compilerSettings'] = {'directiveStartToken' : u'%',
|
|
'cheetahVarStartToken' : u'%%',
|
|
'EOLSlurpToken' : u'%',
|
|
'PSPStartToken' : u'µ' * 10,
|
|
'PSPEndToken' : u'µ' * 10,
|
|
'commentStartToken' : u'µ' * 10,
|
|
'commentEndToken' : u'µ' * 10,
|
|
'multiLineCommentStartToken' : u'µ' * 10,
|
|
'multiLineCommentEndToken' : u'µ' * 10}
|
|
return kls.old_compile(*args, **kwargs) # pylint: disable=E1101
|
|
oriCheetahTemplate.compile = cl_compile
|
|
|
|
|
|
class IsDefined:
|
|
"""
|
|
filtre permettant de ne pas lever d'exception au cas où
|
|
la variable Creole n'est pas définie
|
|
"""
|
|
def __init__(self, context):
|
|
self.context = context
|
|
|
|
def __call__(self, varname):
|
|
if '.' in varname:
|
|
splitted_var = varname.split('.')
|
|
if len(splitted_var) != 2:
|
|
msg = u"Group variables must be of type master.slave"
|
|
raise KeyError(msg)
|
|
master, slave = splitted_var
|
|
if master in self.context:
|
|
return slave in self.context[master].slave.keys()
|
|
return False
|
|
else:
|
|
return varname in self.context
|
|
|
|
|
|
class CheetahTemplate(oriCheetahTemplate):
|
|
def __init__(self,
|
|
filename: str,
|
|
source: str,
|
|
context,
|
|
eosfunc: Dict,
|
|
extra_context: Dict,
|
|
):
|
|
extra_context['is_defined'] = IsDefined(context)
|
|
super().__init__(filename, source, context, eosfunc, extra_context)
|
|
|
|
|
|
# Sync to creole.py
|
|
def process(filename: str,
|
|
source: str,
|
|
true_destfilename: str,
|
|
destfilename: str,
|
|
destdir: str,
|
|
variable: Any,
|
|
rougail_variables_dict: Dict,
|
|
eosfunc: Dict,
|
|
):
|
|
"""Process a cheetah template
|
|
"""
|
|
# full path of the destination file
|
|
try:
|
|
extra_context = {'normalize_family': normalize_family,
|
|
'rougail_filename': true_destfilename,
|
|
'rougail_destination_dir': destdir,
|
|
}
|
|
if variable is not None:
|
|
extra_context['rougail_variable'] = variable
|
|
cheetah_template = CheetahTemplate(filename,
|
|
source,
|
|
rougail_variables_dict,
|
|
eosfunc,
|
|
extra_context,
|
|
)
|
|
data = str(cheetah_template)
|
|
except NotFound as err: # pragma: no cover
|
|
varname = err.args[0][13:-1]
|
|
if filename:
|
|
msg = f"Error: unknown variable used in template {filename} to {destfilename}: {varname}"
|
|
else:
|
|
msg = f"Error: unknown variable used in file {destfilename}: {varname}"
|
|
raise TemplateError(_(msg)) from err
|
|
except Exception as err: # pragma: no cover
|
|
if filename:
|
|
msg = _(f"Error while instantiating template {filename} to {destfilename}: {err}")
|
|
else:
|
|
msg = _(f"Error while instantiating filename {destfilename}: {err}")
|
|
raise TemplateError(msg) from err
|
|
|
|
with open(destfilename, 'w') as file_h:
|
|
file_h.write(data)
|