8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 """PyAMS_template.metadirectives module |
|
14 |
|
15 Is module allows to handle <template /> and <layout > directives in ZCML files. |
|
16 |
|
17 """ |
|
18 |
|
19 import os |
|
20 |
|
21 from pyramid.exceptions import ConfigurationError |
|
22 from pyramid.interfaces import IRequest |
|
23 from zope.component import zcml |
|
24 from zope.interface import Interface, directlyProvides |
|
25 |
|
26 from pyams_template.interfaces import IContentTemplate, ILayoutTemplate |
|
27 from pyams_template.template import TemplateFactory |
14 |
28 |
15 |
29 |
16 # import standard library |
30 __docformat__ = 'restructuredtext' |
17 import os |
|
18 |
|
19 # import interfaces |
|
20 from pyams_template.interfaces import ILayoutTemplate, IContentTemplate |
|
21 from pyramid.interfaces import IRequest |
|
22 |
|
23 # import packages |
|
24 from pyams_template.template import TemplateFactory |
|
25 from pyramid.exceptions import ConfigurationError |
|
26 from zope.component import zcml |
|
27 from zope.interface import directlyProvides, Interface |
|
28 |
31 |
29 |
32 |
30 def template_directive(_context, template, name=u'', |
33 def template_directive(_context, template, name=u'', |
31 for_=Interface, |
34 for_=Interface, |
32 layer=IRequest, |
35 layer=IRequest, |
33 provides=IContentTemplate, |
36 provides=IContentTemplate, |
34 contentType='text/html', |
37 content_type='text/html', |
35 macro=None, |
38 macro=None, |
36 context=None): |
39 context=None): |
|
40 # pylint: disable=too-many-arguments |
|
41 """ZCML <template /> directive handler""" |
37 # Make sure that the template exists |
42 # Make sure that the template exists |
38 template = os.path.abspath(str(_context.path(template))) |
43 template = os.path.abspath(str(_context.path(template))) |
39 if not os.path.isfile(template): |
44 if not os.path.isfile(template): |
40 raise ConfigurationError("No such file", template) |
45 raise ConfigurationError("No such file", template) |
41 |
46 |
42 factory = TemplateFactory(template, contentType, macro) |
47 factory = TemplateFactory(template, content_type, macro) |
43 directlyProvides(factory, provides) |
48 directlyProvides(factory, provides) |
44 |
49 |
45 if context is not None: |
50 if context is not None: |
46 for_ = (for_, layer, context) |
51 for_ = (for_, layer, context) |
47 else: |
52 else: |
56 |
61 |
57 def layout_template_directive(_context, template, name=u'', |
62 def layout_template_directive(_context, template, name=u'', |
58 for_=Interface, |
63 for_=Interface, |
59 layer=IRequest, |
64 layer=IRequest, |
60 provides=ILayoutTemplate, |
65 provides=ILayoutTemplate, |
61 contentType='text/html', |
66 content_type='text/html', |
62 macro=None, |
67 macro=None, |
63 context=None): |
68 context=None): |
|
69 # pylint: disable=too-many-arguments |
|
70 """ZCML <layout /> directive handler""" |
64 template_directive(_context, template, name, for_, layer, provides, |
71 template_directive(_context, template, name, for_, layer, provides, |
65 contentType, macro, context) |
72 content_type, macro, context) |