Browse Source

[WIP] Try to get template options in a more robust way

pull/194/head
Louis 9 years ago
parent
commit
e3fe08e204
  1. 89
      patacrep/templates.py

89
patacrep/templates.py

@ -8,7 +8,7 @@ import yaml
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, \ from jinja2 import Environment, FileSystemLoader, ChoiceLoader, \
TemplateNotFound, nodes TemplateNotFound, nodes
from jinja2.ext import Extension from jinja2.ext import Extension
from jinja2.meta import find_referenced_templates as find_templates from jinja2.meta import find_referenced_templates
from patacrep import errors, files, utils from patacrep import errors, files, utils
from patacrep.latex import lang2babel, UnknownLanguage from patacrep.latex import lang2babel, UnknownLanguage
@ -162,12 +162,16 @@ class TexBookRenderer(Renderer):
) )
def get_all_variables(self, user_config): def get_all_variables(self, user_config):
''' '''Validate template variables (and set defaults when needed)
Validate template variables (and set defaults when needed)
''' '''
data = self.get_template_variables(self.template) data = self.get_template_variables(self.template)
variables = dict() variables = dict()
for name, param in data.items(): for name, param in data.items():
print("*"*30, "DEBUG", "*"*30) # TODO DELETE THIS
from pprint import pprint
print("Data:"); pprint(data)
print("Name:", name)
print("Param:", param)
template_config = user_config.get(name, {}) template_config = user_config.get(name, {})
variables[name] = self._get_variables(param, template_config) variables[name] = self._get_variables(param, template_config)
return variables return variables
@ -181,63 +185,31 @@ class TexBookRenderer(Renderer):
data = utils.DictOfDict(parameter.get('default', {})) data = utils.DictOfDict(parameter.get('default', {}))
data.update(user_config) data.update(user_config)
print("*"*30, "DEBUG", "*"*30) # TODO DELETE THIS
from pprint import pprint
print("Parameter", parameter)
print("Data"); pprint(data)
print("Schema"); pprint(schema)
utils.validate_yaml_schema(data, schema) utils.validate_yaml_schema(data, schema)
return data return data
def get_template_variables(self, template, skip=None): def get_template_variables(self, templatename):
"""Parse the template to extract the variables as a dictionary. """Parse the template to extract the variables as a dictionary.
If the template includes or extends other templates, load them as well. If the template includes or extends other templates, load them as well.
Arguments: Arguments:
- template: the name of the template, as a string. - templatename: the name of the template, as a string.
- skip: a list of templates (as strings) to skip: if they are included
in 'template' (or one of its subtemplates), it is not parsed. in 'template' (or one of its subtemplates), it is not parsed.
""" """
if not skip:
skip = []
variables = {} variables = {}
(current, templates) = self.parse_template(template) for template in self._iter_template_content(templatename):
if current: match = re.findall(_VARIABLE_REGEXP, template)
variables[template.name] = current if not match:
for subtemplate in templates:
if subtemplate in skip:
continue continue
subtemplate = self.jinjaenv.get_template(subtemplate) for variables_string in match:
variables.update(
self.get_template_variables(
subtemplate,
skip + templates
)
)
return variables
def parse_template(self, template):
"""Return (variables, templates).
Argument:
- template: name of the template to parse.
Return values:
- variables: a dictionary of variables contained in 'template', NOT
recursively (included templates are not parsed).
- templates: list of included temlates, NOT recursively.
"""
subvariables = {}
templatename = self.jinjaenv.get_template(template).filename
with patacrep.encoding.open_read(
templatename,
encoding=self.encoding
) as template_file:
content = template_file.read()
subtemplates = list(find_templates(self.jinjaenv.parse(content)))
match = re.findall(_VARIABLE_REGEXP, content)
if match:
for var in match:
try: try:
subvariables.update(yaml.load(var)) variables.update(yaml.load(variables_string))
except ValueError as exception: except ValueError as exception:
raise errors.TemplateError( raise errors.TemplateError(
exception, exception,
@ -246,12 +218,29 @@ class TexBookRenderer(Renderer):
"{filename}. The yaml string was:" "{filename}. The yaml string was:"
"\n'''\n{yamlstring}\n'''" "\n'''\n{yamlstring}\n'''"
).format( ).format(
filename=templatename, filename=template.filename,
yamlstring=var, yamlstring=variables_string,
) )
) )
return variables
return (subvariables, subtemplates) def _iter_template_content(self, templatename, *, skip=None):
"""Iterate over template (and subtemplate) content."""
if skip is None:
skip = []
template = self.jinjaenv.get_template(templatename)
with patacrep.encoding.open_read(
template.filename,
encoding=self.encoding
) as contentfile:
content = contentfile.read()
for subtemplatename in find_referenced_templates(self.jinjaenv.parse(content)):
if subtemplatename not in skip:
yield from self._iter_template_content(
subtemplatename,
skip=skip + [templatename],
)
yield content
def render_tex(self, output, context): def render_tex(self, output, context):
'''Render a template into a .tex file '''Render a template into a .tex file

Loading…
Cancel
Save