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_viewlet.provider module |
|
14 |
|
15 This module provides the "provider:" TALES expression, which allows inclusion of any registered |
|
16 content provider into a Chameleon or ZPT template. |
|
17 """ |
14 |
18 |
15 import re |
19 import re |
16 |
20 |
17 from chameleon.astutil import Symbol |
21 from chameleon.astutil import Symbol |
18 from chameleon.tales import StringExpr |
22 from chameleon.tales import StringExpr |
19 from pyramid_zope_request import PyramidPublisherRequest |
23 from pyramid_zope_request import PyramidPublisherRequest |
20 from zope.contentprovider.interfaces import BeforeUpdateEvent, ContentProviderLookupError, IContentProvider |
24 from zope.contentprovider.interfaces import BeforeUpdateEvent, ContentProviderLookupError, \ |
|
25 IContentProvider |
21 from zope.contentprovider.tales import addTALNamespaceData |
26 from zope.contentprovider.tales import addTALNamespaceData |
22 from zope.location.interfaces import ILocation |
27 from zope.location.interfaces import ILocation |
23 |
28 |
24 from pyams_utils.tales import ContextExprMixin |
29 from pyams_utils.tales import ContextExprMixin |
25 |
30 |
26 |
31 |
27 FUNCTION_EXPRESSION = re.compile('(.+)\((.+)\)', re.MULTILINE | re.DOTALL) |
32 __docformat__ = 'restructuredtext' |
28 ARGUMENTS_EXPRESSION = re.compile('[^(,)]+') |
|
29 |
33 |
30 CONTENT_PROVIDER_NAME = re.compile('([A-Za-z0-9_\-\.]+)') |
34 |
|
35 FUNCTION_EXPRESSION = re.compile(r'(.+)\((.+)\)', re.MULTILINE | re.DOTALL) |
|
36 ARGUMENTS_EXPRESSION = re.compile(r'[^(,)]+') |
|
37 |
|
38 CONTENT_PROVIDER_NAME = re.compile(r'([A-Za-z0-9_\-\.]+)') |
31 |
39 |
32 |
40 |
33 def render_content_provider(econtext, name): |
41 def render_content_provider(econtext, name): |
34 """TALES provider: content provider |
42 """TALES provider: content provider |
35 |
43 |
65 try: |
73 try: |
66 arg = int(arg) # check integer value |
74 arg = int(arg) # check integer value |
67 except ValueError: |
75 except ValueError: |
68 args = arg.split('.') |
76 args = arg.split('.') |
69 result = econtext.get(args.pop(0)) |
77 result = econtext.get(args.pop(0)) |
70 for arg in args: |
78 for arg in args: # pylint: disable=redefined-argument-from-local |
71 result = getattr(result, arg) |
79 result = getattr(result, arg) |
72 return result |
80 return result |
73 else: |
81 else: |
74 return arg |
82 return arg |
75 |
83 |
76 name = name.strip() |
84 name = name.strip() |
77 context = econtext.get('context') |
85 context = econtext.get('context') |
78 request = econtext.get('request') |
86 request = econtext.get('request') |
79 if isinstance(request, PyramidPublisherRequest): |
87 if isinstance(request, PyramidPublisherRequest): |
80 request = request._request |
88 request = request._request # pylint: disable=protected-access |
81 view = econtext.get('view') |
89 view = econtext.get('view') |
82 |
90 |
83 args, kwargs = [], {} |
91 args, kwargs = [], {} |
84 func_match = FUNCTION_EXPRESSION.match(name) |
92 func_match = FUNCTION_EXPRESSION.match(name) |
85 if func_match: |
93 if func_match: |