本文整理汇总了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函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:blaflamme,项目名称:pyramid,代码行数:48,代码来源:mako_templating.py
示例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')
开发者ID:it3s,项目名称:mootiro_web,代码行数:29,代码来源:pyramid_genshi.py
示例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)
开发者ID:blaflamme,项目名称:pyramid_jinja2,代码行数:28,代码来源:__init__.py
示例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)
开发者ID:pyeek,项目名称:pyramid,代码行数:32,代码来源:mako_templating.py
示例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))
开发者ID:tch,项目名称:pyramid_jinja2,代码行数:7,代码来源:__init__.py
示例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
开发者ID:dmckeone,项目名称:bag,代码行数:9,代码来源:starter.py
示例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
开发者ID:jpfarias,项目名称:pyramid_jinja2,代码行数:10,代码来源:__init__.py
示例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)
开发者ID:0x1997,项目名称:pyramid_jinja2,代码行数:55,代码来源:__init__.py
示例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)
开发者ID:drocco007,项目名称:kajiki,代码行数:12,代码来源:pyramid.py
示例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)
开发者ID:nandoflorestan,项目名称:bag,代码行数:52,代码来源:views.py
示例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')
开发者ID:dmckeone,项目名称:bag,代码行数:14,代码来源:starter.py
示例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')
开发者ID:it3s,项目名称:mootiro_web,代码行数:14,代码来源:pyramid_starter.py
示例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")
开发者ID:leofigs,项目名称:bag,代码行数:17,代码来源:starter.py
示例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))
开发者ID:prmtl,项目名称:pyramid_jinja2,代码行数:20,代码来源:__init__.py
示例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)
开发者ID:RyoAbe,项目名称:pyramid,代码行数:21,代码来源:mako_templating.py
示例16: renderer_factory
def renderer_factory(path):
from mako.lookup import TemplateLookup
registry = get_current_registry()
lookup = registry.queryUtility(IMakoLookup)
if lookup is None:
settings = get_settings() or {}
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 lookup path')
directories = directories.splitlines()
directories = [ abspath_from_resource_spec(d) for d in directories ]
lookup = TemplateLookup(directories=directories,
module_directory=module_directory,
input_encoding=input_encoding,
filesystem_checks=reload_templates)
registry.registerUtility(lookup, IMakoLookup)
_, path = resolve_resource_spec(path)
return MakoLookupTemplateRenderer(path, lookup)
开发者ID:markramm,项目名称:pyramid,代码行数:22,代码来源:mako_templating.py
示例17: get_paths
def get_paths(self):
paths = set()
root = self.registry['root']
request = _make_request('/', registry=self.registry)
if root:
excludes = self.siteconfig['site'].get('excludes', '').split('\n')
excludes.extend([
'.*',
'/config.py*',
'/site.cfg',
'/%s' % self.siteconfig['site']['outpath']])
relpaths = dirtools.Dir(
root.abspath,
excludes=excludes).files()
for relpath in relpaths:
traverse(root, relpath)
if root:
paths.add('/%s' % relpath)
visited_routes = set()
info = self.registry.queryUtility(IStaticURLInfo)
if info:
for (url, spec, route_name) in info._get_registrations(self.registry):
visited_routes.add(route_name)
path = abspath_from_resource_spec(spec)
relpaths = dirtools.Dir(path).files()
for relpath in relpaths:
paths.add(
request.route_path(route_name, subpath=relpath))
routelist = self.site.config.config.get_routes_mapper().routelist
for route in routelist:
if route.factory is not None:
matches = route.factory.matches(self.registry)
paths = paths.union(route.generate(x) for x in matches)
elif route.name not in visited_routes:
paths.add(route.generate({}))
visited_routes.add(route.name)
return list(sorted(paths))
开发者ID:fschulze,项目名称:stasis,代码行数:37,代码来源:core.py
示例18: get_template
def get_template(self, uri):
"""Fetch a template from the cache, or check the filesystem
for it
In addition to the basic filesystem lookup, this subclass will
use pkg_resource to load a file using the resource
specification syntax.
"""
isabs = os.path.isabs(uri)
if (not isabs) and (':' in uri):
try:
if self.filesystem_checks:
return self._check(uri, self._collection[uri])
else:
return self._collection[uri]
except KeyError:
pname, path = resolve_resource_spec(uri)
srcfile = abspath_from_resource_spec(path, pname)
if os.path.isfile(srcfile):
return self._load(srcfile, uri)
raise exceptions.TopLevelLookupException(
"Cant locate template for uri %r" % uri)
return TemplateLookup.get_template(self, uri)
开发者ID:pyeek,项目名称:pyramid,代码行数:24,代码来源:mako_templating.py
示例19: _callFUT
def _callFUT(self, spec, pname='__main__'):
from pyramid.resource import abspath_from_resource_spec
return abspath_from_resource_spec(spec, pname)
开发者ID:RyoAbe,项目名称:pyramid,代码行数:3,代码来源:test_resource.py
示例20: _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'))
debug = asbool(settings.get('debug_templates', False))
# get basic environment jinja2 settings
kw.update(_parse_config_for_settings(settings))
reload_templates = settings.get('reload_templates', None)
if reload_templates is None:
# since version 1.5, both settings are supported
reload_templates = settings.get('pyramid.reload_templates', False)
reload_templates = asbool(reload_templates)
undefined = parse_undefined(settings.get('jinja2.undefined', ''))
# get supplementary junja2 settings
input_encoding = settings.get('jinja2.input_encoding', 'utf-8')
domain = settings.get('jinja2.i18n.domain', package and package.__name__ or 'messages')
# get jinja2 extensions
extensions = parse_multiline(settings.get('jinja2.extensions', ''))
if 'jinja2.ext.i18n' not in extensions:
extensions.append('jinja2.ext.i18n')
# get jinja2 directories
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)
# get jinja2 bytecode caching settings and set up bytecaching
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)
# clear cache on exit
atexit.register(kw['bytecode_cache'].clear)
# should newstyle gettext calls be enabled?
newstyle = asbool(settings.get('jinja2.newstyle', False))
environment = Environment(loader=loader,
auto_reload=reload_templates,
extensions=extensions,
undefined=undefined,
**kw)
# register pyramid i18n functions
wrapper = GetTextWrapper(domain=domain)
environment.install_gettext_callables(wrapper.gettext, wrapper.ngettext, newstyle=newstyle)
# register global repository for templates
if package is not None:
environment._default_package = package.__name__
#add custom jinja2 filters
filters = parse_config(settings.get('jinja2.filters', ''))
environment.filters.update(filters)
#add custom jinja2 tests
tests = parse_config(settings.get('jinja2.tests', ''))
environment.tests.update(tests)
# add custom jinja2 functions
jinja_globals = parse_config(settings.get('jinja2.globals', ''))
environment.globals.update(jinja_globals)
registry.registerUtility(environment, IJinja2Environment)
return registry.queryUtility(IJinja2Environment)
开发者ID:aventurella,项目名称:pyramid_jinja2,代码行数:78,代码来源:__init__.py
注:本文中的pyramid.resource.abspath_from_resource_spec函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论