我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用jinja2.TemplateNotFound()。
def write(self, file_path: Path, template: str, context: dict={}, preserve: bool = False): """Using a template file name it renders a template into a file given a context """ if not context: context = self.context error = False try: self._write(file_path, template, context, preserve) except TemplateSyntaxError as exc: message = '{0}:{1} error: {2}'.format(exc.filename, exc.lineno, exc.message) click.secho(message, fg='red') error = True except TemplateNotFound as exc: message = '{0} error: Template not found'.format(exc.name) click.secho(message, fg='red') error = True except TemplateError as exc: message = 'error: {0}'.format(exc.message) click.secho(message, fg='red') error = True if error and Generator.strict: sys.exit(-1)
def get_source(self, environment, template): """Get the template source, filename and reload helper for a template. It's passed the environment and template name and has to return a tuple in the form ``(source, filename, uptodate)`` or raise a `TemplateNotFound` error if it can't locate the template. The source part of the returned tuple must be the source of the template as unicode string or a ASCII bytestring. The filename should be the name of the file on the filesystem if it was loaded from there, otherwise `None`. The filename is used by python for the tracebacks if no loader extension is used. The last item in the tuple is the `uptodate` function. If auto reloading is enabled it's always called to check if the template changed. No arguments are passed so the function must store the old state somewhere (for example in a closure). If it returns `False` the template will be reloaded. """ if not self.has_source_access: raise RuntimeError('%s cannot provide access to the source' % self.__class__.__name__) raise TemplateNotFound(template)
def get_source(self, environment, template): pieces = split_template_path(template) for searchpath in self.searchpath: filename = path.join(searchpath, *pieces) f = open_if_exists(filename) if f is None: continue try: contents = f.read().decode(self.encoding) finally: f.close() mtime = path.getmtime(filename) def uptodate(): try: return path.getmtime(filename) == mtime except OSError: return False return contents, filename, uptodate raise TemplateNotFound(template)
def load(self, environment, name, globals=None): key = self.get_template_key(name) module = '%s.%s' % (self.package_name, key) mod = getattr(self.module, module, None) if mod is None: try: mod = __import__(module, None, None, ['root']) except ImportError: raise TemplateNotFound(name) # remove the entry from sys.modules, we only want the attribute # on the module object we have stored on the loader. sys.modules.pop(module, None) return environment.template_class.from_module_dict( environment, mod.__dict__, globals)
def get_source(self, environment, template): pieces = split_template_path(template) p = '/'.join((self.package_path,) + tuple(pieces)) if not self.provider.has_resource(p): raise TemplateNotFound(template) filename = uptodate = None if self.filesystem_bound: filename = self.provider.get_resource_filename(self.manager, p) mtime = path.getmtime(filename) def uptodate(): try: return path.getmtime(filename) == mtime except OSError: return False source = self.provider.get_resource_string(self.manager, p) return source.decode(self.encoding), filename, uptodate
def jinja_format(data, template_file): class GlobalFileLoader(jinja2.BaseLoader): def get_source(self, environment, template): if not os.path.exists(template): raise jinja2.TemplateNotFound(template) with open(template) as f: source = f.read().decode('utf-8') return source, template, lambda: False combined_credentials = combined_credential_pair_format(data) env = jinja2.Environment( loader=GlobalFileLoader(), keep_trailing_newline=True ) template = env.get_template(template_file) return template.render(secrets=combined_credentials['credentials'])
def load_config(path='.boss.yml'): loader = j.FileSystemLoader('.') try: template = loader.load(j.Environment(), path, os.environ) yml = template.render() doc = yaml.load(yml) return transform_config(doc) except j.TemplateNotFound: error = 'Error loading {}: not found'.format(path) raise ConfigurationError(error) except j.TemplateSyntaxError as e: error = 'Error loading {}: {}, line {}'.format(path, e, e.lineno) raise ConfigurationError(error) except IOError as e: error = 'Error loading {}: {}'.format(path, e.strerror) raise ConfigurationError(error) except v.Invalid as e: error = 'Error validating {}: {}'.format(path, e) raise ConfigurationError(error)
def get_source(self, environment, template): def get_source_list(loaders, environment, template): r = None if len(loaders) != 0: try : r = loaders[0].get_source(environment, template) except TemplateNotFound: r = get_source_list(loaders[1:], environment, template) return r r = get_source_list(self._loaders, environment, template) if r: return r else: raise TemplateNotFound(template) return get_source_list(self._loaders, environment, template)