-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmplt_utils.py
58 lines (51 loc) · 2.14 KB
/
tmplt_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
from jinja2 import meta
from pathlib import Path
import markdown
import model_context
def get_included_templates(env, parent_tmplt_filename, inclusions=None, add_root=True, debug=False):
'''
recursively assemble list of included templates
template filenames will be [../../filename.html]
'''
template_source = 'No Source Available'
try:
if inclusions is None:
inclusions = [Path(parent_tmplt_filename).stem] if add_root else []
if parent_tmplt_filename.endswith('html'):
template_source = env.loader.get_source(env, parent_tmplt_filename)
elif parent_tmplt_filename.endswith('md'):
markdown_source = env.loader.get_source(env, parent_tmplt_filename)
template_source = markdown.markdown(markdown_source[0])
parsed_content = env.parse(template_source)
ref_tmplt_filenames = meta.find_referenced_templates(parsed_content)
for ref_tmplt_filename in ref_tmplt_filenames:
just_tmplt_name = Path(ref_tmplt_filename).stem
if debug:
print('tmplt name:', just_tmplt_name)
inclusions.append(just_tmplt_name)
inclusions = get_included_templates(
env, ref_tmplt_filename, inclusions)
except Exception as e:
err_line = sys.exc_info()[-1].tb_lineno
print('Exception parsing template {} on line {}'.format(e, err_line))
print('Source:\n', template_source)
return inclusions
def get_model_context(comp_names, host, req_args, req_kwargs, strict=True):
'''
Assemble the context dictionary
'''
ctx = getattr(model_context, 'global_ctx')(
host, comp_names[0], req_args, req_kwargs) # initialise
for comp_name in comp_names:
if hasattr(model_context, comp_name):
comp_ctx = getattr(model_context, comp_name)(
host, req_args, req_kwargs)
else:
if strict:
raise Exception(
'No Model Context defined for the "{0}" component'.format(comp_name))
else:
comp_ctx = {}
ctx.update(comp_ctx)
return ctx