Added check if Jinja module exists and appropriate error message to project generation.
50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
|
|
import pathlib
|
|
|
|
Import('env')
|
|
|
|
if not hasattr(env, 'Jinja'):
|
|
Return('env')
|
|
|
|
def _jinja_load_config(env, config_name):
|
|
searched_paths = []
|
|
for scons_path in env['JINJA_CONFIG_SEARCHPATH']:
|
|
if hasattr(scons_path, 'abspath'):
|
|
scons_path = scons_path.abspath
|
|
path = pathlib.Path(scons_path) / f'{config_name}.yml'
|
|
if path.exists():
|
|
with path.open('r') as file:
|
|
import yaml
|
|
return yaml.safe_load(file)
|
|
searched_paths.append(f'\n{path}')
|
|
joined_paths = ''.join(searched_paths)
|
|
raise Exception(f'Could not find Jinja config file "{config_name}.yml". Searched: {joined_paths}')
|
|
def _wrap_jinja(orig_jinja):
|
|
def _wrapped(env, target, **kwargs):
|
|
if 'source' not in kwargs:
|
|
kwargs['source'] = f'{target}.jinja'
|
|
target = orig_jinja(**kwargs)
|
|
if 'depends' in kwargs:
|
|
for dependency in kwargs['depends']:
|
|
env.Depends(target, dependency)
|
|
# env.Depends(alias_prepare, target)
|
|
return target
|
|
return _wrapped
|
|
|
|
def _file_size(env, fname: str) -> int:
|
|
return env.File(fname).get_size()
|
|
|
|
def _file_content_hex(env, fname: str) -> str:
|
|
bytes = env.File(fname).get_contents()
|
|
return ','.join([hex(byte) for byte in bytes])
|
|
|
|
env.AddMethod(_wrap_jinja(env.Jinja), 'Jinja')
|
|
env.Append(JINJA_FILTERS = {'load_config': _jinja_load_config})
|
|
env.Append(JINJA_GLOBALS = {
|
|
'file_size': lambda *args: _file_size(env, *args),
|
|
'file_content_hex': lambda *args: _file_content_hex(env, *args)
|
|
})
|
|
env.Append(JINJA_TEMPLATE_SEARCHPATH = ['data/jinja'])
|
|
env['JINJA_CONFIG_SEARCHPATH'] = [env.Dir('#data/config')]
|
|
Return('env')
|