本文整理汇总了Python中pyramid.resource.abspath_from_resource_spec函数的典型用法代码示例。如果您正苦于以下问题:Python abspath_from_resource_spec函数的具体用法?Python abspath_from_resource_spec怎么用?Python abspath_from_resource_spec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了abspath_from_resource_spec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: renderer_factory
def renderer_factory(info):
path = info.name
registry = info.registry
settings = info.settings
lookup = registry.queryUtility(IMakoLookup)
if lookup is None:
reload_templates = settings.get('reload_templates', False)
directories = settings.get('mako.directories', None)
module_directory = settings.get('mako.module_directory', None)
input_encoding = settings.get('mako.input_encoding', 'utf-8')
error_handler = settings.get('mako.error_handler', None)
default_filters = settings.get('mako.default_filters', None)
imports = settings.get('mako.imports', None)
strict_undefined = settings.get('mako.strict_undefined', 'false')
if directories is None:
raise ConfigurationError(
'Mako template used without a ``mako.directories`` setting')
if not hasattr(directories, '__iter__'):
directories = filter(None, directories.splitlines())
directories = [ abspath_from_resource_spec(d) for d in directories ]
if module_directory is not None:
module_directory = abspath_from_resource_spec(module_directory)
if error_handler is not None:
dotted = DottedNameResolver(info.package)
error_handler = dotted.maybe_resolve(error_handler)
if default_filters is not None:
if not hasattr(default_filters, '__iter__'):
default_filters = filter(None, default_filters.splitlines())
if imports is not None:
if not hasattr(imports, '__iter__'):
imports = filter(None, imports.splitlines())
strict_undefined = asbool(strict_undefined)
lookup = PkgResourceTemplateLookup(directories=directories,
module_directory=module_directory,
input_encoding=input_encoding,
error_handler=error_handler,
default_filters=default_filters,
imports=imports,
filesystem_checks=reload_templates,
strict_undefined=strict_undefined)
registry_lock.acquire()
try:
registry.registerUtility(lookup, IMakoLookup)
finally:
registry_lock.release()
return MakoLookupTemplateRenderer(path, lookup)
示例2: __init__
def __init__(self, settings):
from genshi.template import TemplateLoader
try:
dirs = settings['genshi.directories']
except KeyError:
raise KeyError('You need to configure genshi.directories.')
paths = [abspath_from_resource_spec(p) for p in to_list(dirs)]
# http://genshi.edgewall.org/wiki/Documentation/i18n.html
# If genshi.translation_domain is configured,
# we set up a callback in the loader
domain = settings.get('genshi.translation_domain')
if domain:
from genshi.filters import Translator
from pyramid.i18n import get_localizer
from pyramid.threadlocal import get_current_request
def translate(text):
return get_localizer(get_current_request()) \
.translate(text, domain=domain)
def callback(template):
Translator(translate).setup(template)
else:
callback = None
self.loader = TemplateLoader(paths,
auto_reload = asbool(settings.get('reload_templates')),
callback = callback)
self.strip_whitespace = settings.get('genshi.strip_whitespace', True)
self.encoding = settings.get('genshi.encoding', 'utf-8')
self.doctype = settings.get('genshi.doctype', 'html5')
self.method = settings.get('genshi.method', 'xhtml')
示例3: renderer_factory
def renderer_factory(info):
registry = info.registry
settings = info.settings
environment = registry.queryUtility(IJinja2Environment)
if environment is None:
reload_templates = settings.get('reload_templates', False)
directories = settings.get('jinja2.directories')
input_encoding = settings.get('jinja2.input_encoding', 'utf-8')
autoescape = settings.get('jinja2.autoescape', True)
extensions = settings.get('jinja2.extensions', '')
filters = settings.get('jinja2.filters', '')
if directories is None:
raise ConfigurationError(
'Jinja2 template used without a ``jinja2.directories`` setting')
if isinstance(directories, basestring):
directories = splitlines(directories)
directories = [ abspath_from_resource_spec(d) for d in directories ]
loader = FileSystemLoader(directories, encoding=input_encoding)
autoescape = asbool(autoescape)
extensions = parse_extensions(extensions)
filters = parse_filters(filters)
environment = Environment(loader=loader,
auto_reload=reload_templates,
autoescape=autoescape,
extensions=extensions)
environment.filters.update(filters)
registry.registerUtility(environment, IJinja2Environment)
return Jinja2TemplateRenderer(info, environment)
示例4: renderer_factory
def renderer_factory(info):
path = info.name
registry = info.registry
settings = info.settings
lookup = registry.queryUtility(IMakoLookup)
if lookup is None:
reload_templates = settings.get('reload_templates', False)
directories = settings.get('mako.directories')
module_directory = settings.get('mako.module_directory')
input_encoding = settings.get('mako.input_encoding', 'utf-8')
error_handler = settings.get('mako.error_handler', None)
default_filters = settings.get('mako.default_filters', None)
imports = settings.get('mako.imports', [])
if directories is None:
raise ConfigurationError(
'Mako template used without a ``mako.directories`` setting')
directories = directories.splitlines()
directories = [ abspath_from_resource_spec(d) for d in directories ]
lookup = PkgResourceTemplateLookup(directories=directories,
module_directory=module_directory,
input_encoding=input_encoding,
error_handler=error_handler,
default_filters=default_filters,
imports=imports,
filesystem_checks=reload_templates)
registry_lock.acquire()
try:
registry.registerUtility(lookup, IMakoLookup)
finally:
registry_lock.release()
return MakoLookupTemplateRenderer(path, lookup)
示例5: _add_jinja2_search_path
def _add_jinja2_search_path(config, searchpath):
registry = config.registry
env = _get_or_build_default_environment(registry)
if isinstance(searchpath, basestring):
searchpath = [x.strip() for x in searchpath.split('\n') if x.strip()]
for d in searchpath:
env.loader.searchpath.append(abspath_from_resource_spec(d))
示例6: scan
def scan(self):
self.packages.append(self.package_name)
for p in self.packages:
self.config.scan(p)
locale_dir = abspath_from_resource_spec(p + ':locale')
if os.path.isdir(locale_dir):
self.config.add_translation_dirs(locale_dir)
# Make this method a noop for the future (scan only once)
self.scan = lambda: None
示例7: directory_loader_factory
def directory_loader_factory(settings):
input_encoding = settings.get('jinja2.input_encoding', 'utf-8')
directories = settings.get('jinja2.directories') or ''
if isinstance(directories, string_types):
directories = splitlines(directories)
directories = [abspath_from_resource_spec(d) for d in directories]
loader = SmartAssetSpecLoader(
directories, encoding=input_encoding,
debug=asbool(settings.get('debug_templates', False)))
return loader
示例8: _get_or_build_default_environment
def _get_or_build_default_environment(registry):
environment = registry.queryUtility(IJinja2Environment)
if environment is not None:
return environment
settings = registry.settings
kw = {}
package = _caller_package(('pyramid_jinja2', 'jinja2', 'pyramid.config'))
reload_templates = asbool(settings.get('reload_templates', False))
autoescape = asbool(settings.get('jinja2.autoescape', True))
domain = settings.get('jinja2.i18n.domain', 'messages')
debug = asbool(settings.get('debug_templates', False))
input_encoding = settings.get('jinja2.input_encoding', 'utf-8')
extensions = parse_multiline(settings.get('jinja2.extensions', ''))
if 'jinja2.ext.i18n' not in extensions:
extensions.append('jinja2.ext.i18n')
directories = parse_multiline(settings.get('jinja2.directories') or '')
directories = [abspath_from_resource_spec(d, package) for d in directories]
loader = SmartAssetSpecLoader(
directories,
encoding=input_encoding,
debug=debug)
# bytecode caching
bytecode_caching = asbool(settings.get('jinja2.bytecode_caching', True))
bytecode_caching_directory = settings.get('jinja2.bytecode_caching_directory', None)
if bytecode_caching:
kw['bytecode_cache'] = FileSystemBytecodeCache(bytecode_caching_directory)
defaults_prefix = 'jinja2.defaults.'
for k, v in settings.items():
if k.startswith(defaults_prefix):
kw[k[len(defaults_prefix):]] = v
environment = Environment(loader=loader,
auto_reload=reload_templates,
autoescape=autoescape,
extensions=extensions,
**kw)
# register pyramid i18n functions
wrapper = GetTextWrapper(domain=domain)
environment.install_gettext_callables(wrapper.gettext, wrapper.ngettext)
# register global repository for templates
if package is not None:
environment._default_package = package.__name__
filters = parse_filters(settings.get('jinja2.filters', ''))
environment.filters.update(filters)
registry.registerUtility(environment, IJinja2Environment)
return registry.queryUtility(IJinja2Environment)
示例9: import_
def import_(self, name, *a, **kw):
'''Overrides Loader.import_().
* Resolves the resource spec into an absolute path for the template.
* Checks the template modification time to decide whether to reload it.
'''
name = abspath_from_resource_spec(name)
if self.auto_reload and name in self.modules:
mtime = stat(name).st_mtime
if mtime > self._timestamps.get(name, 0):
del self.modules[name]
return super(PyramidKajikiLoader, self).import_(name, *a, **kw)
示例10: serve_preloaded
def serve_preloaded(
config, route_name, route_path, payload,
encoding=None, content_type=None,
):
"""Read a file (such as robots.txt or favicon.ini) into memory.
...then set up a view that serves it. Usage::
from bag.web.pyramid.views import serve_preloaded
serve_preloaded(
config,
route_name='robots',
route_path='robots.txt',
payload='my_package:static/robots.txt',
encoding='utf-8')
serve_preloaded(
config,
route_name='favicon',
route_path='favicon.ico',
payload='my_package:static/favicon.ico',
content_type='image/x-icon',
)
"""
from os.path import getmtime, getsize
from pyramid.resource import abspath_from_resource_spec
from pyramid.response import Response
path = abspath_from_resource_spec(payload)
if not content_type:
from mimetypes import guess_type
content_type = guess_type(path)[0] or 'application/octet-stream'
if encoding:
import codecs
stream = codecs.open(path, 'r', encoding='utf-8')
else:
stream = open(path, 'rb')
kwargs = dict(
content_type=content_type,
body=stream.read(),
last_modified=getmtime(path),
content_length=getsize(path),
)
stream.close()
def preloaded_view(request): # This closure is the view handler
return Response(**kwargs)
config.add_route(route_name, route_path)
config.add_view(preloaded_view, route_name=route_name)
示例11: enable_favicon
def enable_favicon(self, path='static/favicon.ico'):
'''Registers a view that serves /favicon.ico.
web_deps.PageDeps contains a favicon_tag() method that you can use to
create the link to it.
FileResponse appeared in Pyramid 1.3a9.
'''
path = abspath_from_resource_spec(self.package_name + ':' + path)
def favicon_view(request):
return FileResponse(path, request=request)
self.config.add_route('favicon', 'favicon.ico')
self.config.add_view(favicon_view, route_name='favicon')
示例12: enable_robots
def enable_robots(self, path='static/robots.txt'):
from mimetypes import guess_type
from pyramid.resource import abspath_from_resource_spec
path = abspath_from_resource_spec(
self.settings.get('robots', '{}:{}'.format(self.name, path)))
content_type = guess_type(path)[0]
import codecs
with codecs.open(path, 'r', encoding='utf-8') as f:
content = f.read()
from pyramid.response import Response
def robots_view(request):
return Response(content_type=content_type, app_iter=content)
self.config.add_route('robots', '/robots.txt')
self.config.add_view(robots_view, route_name='robots')
示例13: enable_robots
def enable_robots(self, path="static/robots.txt"):
"""Reads robots.txt into memory, then sets up a view that serves it."""
from mimetypes import guess_type
path = abspath_from_resource_spec(self.settings.get("robots", "{}:{}".format(self.package_name, path)))
content_type = guess_type(path)[0]
import codecs
with codecs.open(path, "r", encoding="utf-8") as f:
content = f.read()
from pyramid.response import Response
def robots_view(request):
return Response(content_type=content_type, app_iter=content)
self.config.add_route("robots", "/robots.txt")
self.config.add_view(robots_view, route_name="robots")
示例14: add_jinja2_search_path
def add_jinja2_search_path(config, searchpath):
"""
This function is added as a method of a :term:`Configurator`, and
should not be called directly. Instead it should be called like so after
``pyramid_jinja2`` has been passed to ``config.include``:
.. code-block:: python
config.add_jinja2_search_path('anotherpackage:templates/')
It will add the directory or :term:`asset spec` passed as ``searchpath``
to the current search path of the ``jinja2.environment.Environment`` used
by :mod:`pyramid_jinja2`.
"""
registry = config.registry
env = _get_or_build_default_environment(registry)
searchpath = parse_multiline(searchpath)
for d in searchpath:
env.loader.searchpath.append(abspath_from_resource_spec(d, config.package_name))
示例15: renderer_factory
def renderer_factory(info):
path = info.name
registry = info.registry
settings = info.settings
lookup = registry.queryUtility(IMakoLookup)
if lookup is None:
reload_templates = settings.get('reload_templates', False)
directories = settings.get('mako.directories')
module_directory = settings.get('mako.module_directory')
input_encoding = settings.get('mako.input_encoding', 'utf-8')
if directories is None:
raise ConfigurationError(
'Mako template used without a ``mako.directories`` setting')
directories = directories.splitlines()
directories = [ abspath_from_resource_spec(d) for d in directories ]
lookup = PkgResourceTemplateLookup(directories=directories,
module_directory=module_directory,
input_encoding=input_encoding,
filesystem_checks=reload_templates)
registry.registerUtility(lookup, IMakoLookup)
return MakoLookupTemplateRenderer(path, lookup)