def _lookup_template(context, uri, relativeto): lookup = context._with_template.lookup if lookup is None: raise exceptions.TemplateLookupException("Template '%s' has no TemplateLookup associated" % context._with_template.uri) uri = lookup.adjust_uri(uri, relativeto) try: return lookup.get_template(uri) except exceptions.TopLevelLookupException, e: raise exceptions.TemplateLookupException(str(e))
def __check(self, template): """private method used to verify if a template has changed since the last time it has been put in cache... This method being based on the mtime of a real file this should never be called on a zipped deployed application. This method is a ~copy/paste of the original caching system from the Mako lookup loader. """ if template.filename is None: return template if not os.path.exists(template.filename): # remove from cache. self.template_cache.pop(template.filename, None) raise exceptions.TemplateLookupException( "Cant locate template '%s'" % template.filename) elif template.module._modified_time < os.stat( template.filename)[stat.ST_MTIME]: # cache is too old, remove old template # from cache and reload. self.template_cache.pop(template.filename, None) return self.__load(template.filename) else: # cache is correct, use it. return template
def __check(self, uri, template): if template.filename is None: return template if not os.path.exists(template.filename): self.__collection.pop(uri, None) raise exceptions.TemplateLookupException("Cant locate template for uri '%s'" % uri) elif template.module._modified_time < os.stat(template.filename)[stat.ST_MTIME]: self.__collection.pop(uri, None) return self.__load(template.filename, uri) else: return template
def _check(self, uri, template): if template.filename is None: return template try: template_stat = os.stat(template.filename) if template.module._modified_time >= template_stat[stat.ST_MTIME]: return template self._collection.pop(uri, None) return self._load(template.filename, uri) except OSError as e: self._collection.pop(uri, None) raise exceptions.TemplateLookupException( "Can't locate template for uri %r" % uri) from e
def _load(self, filename, uri): def resolve(uri): """Check whether any referenced template has changed -- recursively.""" if uri in self.resolved: return self.resolved[uri] filename = posixpath.normpath(posixpath.join(self.directories[0], uri)) p = self.modulename_callable(filename, uri) has_changed = getmtime(filename) > getmtime(p) if isfile(p) else True if has_changed: self.resolved[uri] = True return True with io.open(filename) as fp: source = fp.read() for match in self.inherits.finditer(source): if resolve(match.group(1)): return True for match in self.includes.finditer(source): if resolve(match.group(1)): return True return False try: template = self._collection[uri] except KeyError: template = super(ExtendedLookup, self)._load(filename, uri) try: template.has_changed = resolve(basename(template.filename)) except (OSError, IOError): raise exceptions.TemplateLookupException( "Can't locate template for uri %r" % uri) return template
def __init__( self, text=None, filename=None, uri=None, format_exceptions=False, error_handler=None, lookup=None, output_encoding=None, encoding_errors="strict", module_directory=None, cache_args=None, cache_impl="beaker", cache_enabled=True, cache_type=None, cache_dir=None, cache_url=None, module_filename=None, input_encoding=None, disable_unicode=False, module_writer=None, bytestring_passthrough=False, default_filters=None, buffer_filters=(), strict_undefined=False, imports=None, future_imports=None, enable_loop=True, preprocessor=None, lexer_cls=None, include_error_handler=None, ): if uri: self.module_id = re.sub(r"\W", "_", uri) self.uri = uri elif filename: self.module_id = re.sub(r"\W", "_", filename) drive, path = os.path.splitdrive(filename) path = os.path.normpath(path).replace(os.path.sep, "/") self.uri = path else: self.module_id = "memory:" + hex(id(self)) self.uri = self.module_id u_norm = self.uri if u_norm.startswith("/"): u_norm = u_norm[1:] u_norm = os.path.normpath(u_norm) if u_norm.startswith(".."): raise exceptions.TemplateLookupException( 'Template uri "%s" is invalid - ' "it cannot be relative outside " "of the root path." % self.uri) self.input_encoding = input_encoding self.output_encoding = output_encoding self.encoding_errors = encoding_errors self.disable_unicode = disable_unicode self.bytestring_passthrough = bytestring_passthrough or disable_unicode self.enable_loop = enable_loop self.strict_undefined = strict_undefined self.module_writer = module_writer if compat.py3k and disable_unicode: raise exceptions.UnsupportedError("Mako for Python 3 does not " "support disabling Unicode") elif output_encoding and disable_unicode: raise exceptions.UnsupportedError( "output_encoding must be set to " "None when disable_unicode is used.") if default_filters is None: if compat.py3k or self.disable_unicode: self.default_filters = ["str"] else: self.default_filters = ["unicode"] else: self.default_filters = default_filters self.buffer_filters = buffer_filters self.imports = imports self.future_imports = future_imports self.preprocessor = preprocessor if lexer_cls is not None: self.lexer_cls = lexer_cls # if plain text, compile code in memory only if text is not None: (code, module) = _compile_text(self, text, filename) self._code = code self._source = text ModuleInfo(module, None, self, filename, code, text) elif filename is not None: # if template filename and a module directory, load # a filesystem-based module file, generating if needed if module_filename is not None: path = module_filename elif module_directory is not None: path = os.path.abspath( os.path.join(os.path.normpath(module_directory), u_norm + ".py")) else: path = None module = self._compile_from_file(path, filename) else: raise exceptions.RuntimeException( "Template requires text or filename") self.module = module self.filename = filename self.callable_ = self.module.render_body self.format_exceptions = format_exceptions self.error_handler = error_handler self.include_error_handler = include_error_handler self.lookup = lookup self.module_directory = module_directory self._setup_cache_args( cache_impl, cache_enabled, cache_args, cache_type, cache_dir, cache_url, )