src/pyams_utils/tales.py
branchdev-tf
changeset 427 63284c98cdc1
parent 322 a7d712e514a0
equal deleted inserted replaced
426:2022e4da3ad9 427:63284c98cdc1
     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_utils.tales module
       
    14 
       
    15 This module provides a custom TALES extension engine, which allows you to define custom
       
    16 TALES expressions which can be used from Chameleon or Zope templates.
       
    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.codegen import template
    22 from chameleon.codegen import template
    19 from chameleon.tales import StringExpr
    23 from chameleon.tales import StringExpr
    20 from zope.contentprovider.tales import addTALNamespaceData
    24 from zope.contentprovider.tales import addTALNamespaceData
    21 
    25 
    22 from pyams_utils.interfaces.tales import ITALESExtension
    26 from pyams_utils.interfaces.tales import ITALESExtension
    23 
    27 
       
    28 __docformat__ = 'restructuredtext'
    24 
    29 
    25 class ContextExprMixin(object):
    30 
       
    31 class ContextExprMixin:
    26     """Mixin-class for expression compilers"""
    32     """Mixin-class for expression compilers"""
    27 
    33 
    28     transform = None
    34     transform = None
    29 
    35 
    30     def __call__(self, target, engine):
    36     def __call__(self, target, engine):
    34                              target=target,
    40                              target=target,
    35                              transform=self.transform)
    41                              transform=self.transform)
    36         return assignment + transform
    42         return assignment + transform
    37 
    43 
    38 
    44 
    39 FUNCTION_EXPRESSION = re.compile('(.+)\((.+)\)', re.MULTILINE | re.DOTALL)
    45 FUNCTION_EXPRESSION = re.compile(r'(.+)\((.+)\)', re.MULTILINE | re.DOTALL)
    40 ARGUMENTS_EXPRESSION = re.compile('[^(,)]+')
    46 ARGUMENTS_EXPRESSION = re.compile(r'[^(,)]+')
    41 
    47 
    42 
    48 
    43 def render_extension(econtext, name):
    49 def render_extension(econtext, name):
    44     """TALES extension renderer
    50     """TALES extension renderer
    45 
    51 
    70         try:
    76         try:
    71             arg = int(arg)  # check integer value
    77             arg = int(arg)  # check integer value
    72         except ValueError:
    78         except ValueError:
    73             args = arg.split('.')
    79             args = arg.split('.')
    74             result = econtext.get(args.pop(0))
    80             result = econtext.get(args.pop(0))
    75             for arg in args:
    81             for arg in args:  # pylint: disable=redefined-argument-from-local
    76                 result = getattr(result, arg)
    82                 result = getattr(result, arg)
    77             return result
    83             return result
    78         else:
    84         else:
    79             return arg
    85             return arg
    80 
    86