def load_templates(template_readme, template_model): env = dict(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=False) jinja2_ext = ".jinja2" if not template_readme.endswith(jinja2_ext): raise ValueError("README template file name must end with %s" % jinja2_ext) if not template_model.endswith(jinja2_ext): raise ValueError( "Model description template file name must end with %s" % jinja2_ext) try: readme_ext = template_readme[:-len(jinja2_ext)].split(".", 1)[1] except IndexError: raise ValueError( "README template file name must contain the target file " "extension prepended to %s" % jinja2_ext) from None try: model_ext = template_model[:-len(jinja2_ext)].split(".", 1)[1] except IndexError: raise ValueError( "Model description template file name must contain the target file " "extension prepended to %s" % jinja2_ext) from None with open(template_readme) as fin: template_readme_obj = Template(fin.read(), **env) template_readme_obj.filename = template_readme log.info("Loaded %s", template_readme) with open(template_model) as fin: template_model_obj = Template(fin.read(), **env) template_model_obj.filename = template_model log.info("Loaded %s", template_model) return (template_readme_obj, readme_ext), (template_model_obj, model_ext)
def load_template(self, template: str) -> Template: env = dict(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=False) jinja2_ext = ".jinja2" if not template.endswith(jinja2_ext): self._log.error("Template file name must end with %s" % jinja2_ext) raise ValueError if not template[:-len(jinja2_ext)].endswith(".md"): self._log.error("Template file should be a Markdown file.") raise ValueError with open(template, encoding="utf-8") as fin: template_obj = Template(fin.read(), **env) template_obj.filename = template self._log.info("Loaded %s", template) return template_obj
def get_template(self, template_name): """Get a :class:`jinja2.Template` from the environment. :param template_name: A string representing the name of the template. """ try: return self.env.get_template(template_name) except UnicodeDecodeError as e: raise UnicodeError('Unable to decode %s: %s' % (template_name, e)) except TemplateSyntaxError as e: if template_name.split('.')[-1] == 'md': self.logger.info(Fore.RED + 'jinja doesn\'t like syntax in {}, ignoring'.format(template_name) + Fore.RESET) template = Template('') template.name = template_name template.filename = self.find_searchpath(template_name) return template raise TemplateSyntaxError
def load_template(self, template: str) -> Template: """Load a Jinja2 template from the source directory.""" env = dict(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=False) jinja2_ext = ".jinja2" if not template.endswith(jinja2_ext): self._log.error("Template file name must end with %s" % jinja2_ext) raise ValueError if not template[:-len(jinja2_ext)].endswith(".md"): self._log.error("Template file should be a Markdown file.") raise ValueError if not os.path.isabs(template): template = os.path.join(os.path.dirname(__file__), template) with open(template, encoding="utf-8") as fin: template_obj = Template(fin.read(), **env) template_obj.filename = template self._log.info("Loaded %s", template) return template_obj