def _lookup_template(context, uri, relativeto): lookup = context._with_template.lookup if lookup is None: raise errors.TemplateLookupException( "Template '%s' has no TemplateLookup associated" % context._with_template.uri) uri = lookup.adjust_uri(uri, relativeto) try: return lookup.get_template(uri) except errors.TopLevelLookupException: raise errors.TemplateLookupException(str(compat.exception_as()))
def _lookup_uicls(context, ui): lookup = context._with_template.lookup if lookup is None: raise errors.TemplateLookupException( "Template '%s' has no TemplateLookup associated" % context._with_template.uri) uicls = lookup.get_ui(ui) return uicls
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]: self.collection.pop(uri, None) return self.load(template.filename, uri) else: return template except OSError: self.collection.pop(uri, None) raise errors.TemplateLookupException( "Cant locate template for uri %r" % uri)
def __init__(self, text=None, filename=None, uri=None, format_errors=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): 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 errors.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 errors.UnsupportedError( "Choco for Python 3 does not " "support disabling Unicode") elif output_encoding and disable_unicode: raise errors.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 errors.RuntimeException( "Template requires text or filename") self.module = module self.filename = filename self.callable_ = self.module.render_body self.format_errors = format_errors self.error_handler = 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 )