diff -r 000000000000 -r 31ded33115d7 src/pyams_template/metaconfigure.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_template/metaconfigure.py Thu Feb 19 10:29:02 2015 +0100 @@ -0,0 +1,65 @@ +# +# Copyright (c) 2008-2015 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# + +__docformat__ = 'restructuredtext' + + +# import standard library +import os + +# import interfaces +from pyams_template.interfaces import ILayoutTemplate, IContentTemplate +from pyramid.interfaces import IRequest + +# import packages +from pyams_template.template import TemplateFactory +from pyramid.exceptions import ConfigurationError +from zope.component import zcml +from zope.interface import directlyProvides, Interface + + +def template_directive(_context, template, name=u'', + for_=Interface, + layer=IRequest, + provides=IContentTemplate, + contentType='text/html', + macro=None, + context=None): + # Make sure that the template exists + template = os.path.abspath(str(_context.path(template))) + if not os.path.isfile(template): + raise ConfigurationError("No such file", template) + + factory = TemplateFactory(template, contentType, macro) + directlyProvides(factory, provides) + + if context is not None: + for_ = (for_, layer, context) + else: + for_ = (for_, layer) + + # register the template + if name: + zcml.adapter(_context, (factory,), provides, for_, name=name) + else: + zcml.adapter(_context, (factory,), provides, for_) + + +def layout_template_directive(_context, template, name=u'', + for_=Interface, + layer=IRequest, + provides=ILayoutTemplate, + contentType='text/html', + macro=None, + context=None): + template_directive(_context, template, name, for_, layer, provides, + contentType, macro, context)