def __init__(self, cache): if not has_beaker: raise errors.RuntimeException( "Can't initialize Beaker plugin; Beaker is not installed.") global _beaker_cache if _beaker_cache is None: if 'manager' in cache.template.cache_args: _beaker_cache = cache.template.cache_args['manager'] else: _beaker_cache = beaker_cache.CacheManager() super(BeakerCacheImpl, self).__init__(cache)
def load(self, name): if name in self.impls: return self.impls[name]() else: import pkg_resources for impl in pkg_resources.iter_entry_points(self.group, name): self.impls[name] = impl.load return impl.load() else: from choco import errors raise errors.RuntimeException("Can't load plugin %s %s" % (self.group, name))
def capture(context, callable_, *args, **kwargs): """Execute the given template def, capturing the output into a buffer. See the example in :ref:`namespaces_python_modules`. """ if not compat.callable(callable_): raise errors.RuntimeException( "capture() function expects a callable as " "its argument (i.e. capture(func, *args, **kwargs))") context._push_buffer() try: callable_(*args, **kwargs) finally: buf = context._pop_buffer() return buf.getvalue()
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 )
def __getattr__(self, key): raise errors.RuntimeException("No loop context is established")