ext/tal.py
branchtls-sprint
changeset 1802 d628defebc17
parent 704 0c2c8f0a6ded
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
     1 """provides simpleTAL extensions for CubicWeb
     1 """provides simpleTAL extensions for CubicWeb
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 
     7 
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    10 import sys
    10 import sys
    11 import re
    11 import re
    12 from os.path import exists, isdir, join
    12 from os.path import exists, isdir, join
    13 from logging import getLogger
    13 from logging import getLogger
    14 from StringIO import StringIO
    14 from StringIO import StringIO
    15         
    15 
    16 from simpletal import simpleTAL, simpleTALES
    16 from simpletal import simpleTAL, simpleTALES
    17 
    17 
    18 from logilab.common.decorators import cached
    18 from logilab.common.decorators import cached
    19 
    19 
    20 LOGGER = getLogger('cubicweb.tal')
    20 LOGGER = getLogger('cubicweb.tal')
    21 
    21 
    22 
    22 
    23 class LoggerAdapter(object):
    23 class LoggerAdapter(object):
    24     def __init__(self, tal_logger):
    24     def __init__(self, tal_logger):
    25         self.tal_logger = tal_logger
    25         self.tal_logger = tal_logger
    26         
    26 
    27     def debug(self, msg):
    27     def debug(self, msg):
    28         LOGGER.debug(msg)
    28         LOGGER.debug(msg)
    29 
    29 
    30     def warn(self, msg):
    30     def warn(self, msg):
    31         LOGGER.warning(msg)
    31         LOGGER.warning(msg)
    47 
    47 
    48     def addRepeat(self, name, var, initialValue):
    48     def addRepeat(self, name, var, initialValue):
    49         simpleTALES.Context.addRepeat(self, name, var, initialValue)
    49         simpleTALES.Context.addRepeat(self, name, var, initialValue)
    50 
    50 
    51 # XXX FIXME need to find a clean to define OPCODE values for extensions
    51 # XXX FIXME need to find a clean to define OPCODE values for extensions
    52 I18N_CONTENT = 18  
    52 I18N_CONTENT = 18
    53 I18N_REPLACE = 19
    53 I18N_REPLACE = 19
    54 RQL_EXECUTE  = 20
    54 RQL_EXECUTE  = 20
    55 # simpleTAL uses the OPCODE values to define priority over commands.
    55 # simpleTAL uses the OPCODE values to define priority over commands.
    56 # TAL_ITER should have the same priority than TAL_REPEAT (i.e. 3), but
    56 # TAL_ITER should have the same priority than TAL_REPEAT (i.e. 3), but
    57 # we can't use the same OPCODE for two different commands without changing
    57 # we can't use the same OPCODE for two different commands without changing
   111 
   111 
   112         backport this to simpleTAL
   112         backport this to simpleTAL
   113         """
   113         """
   114         # Compile tal:attributes into attribute command
   114         # Compile tal:attributes into attribute command
   115         # Argument: [(attributeName, expression)]
   115         # Argument: [(attributeName, expression)]
   116         
   116 
   117         # Break up the list of attribute settings first
   117         # Break up the list of attribute settings first
   118         commandArgs = []
   118         commandArgs = []
   119         # We only want to match semi-colons that are not escaped
   119         # We only want to match semi-colons that are not escaped
   120         argumentSplitter =  re.compile(r'(?<!;);(?!;)')
   120         argumentSplitter =  re.compile(r'(?<!;);(?!;)')
   121         for attributeStmt in argumentSplitter.split(argument):
   121         for attributeStmt in argumentSplitter.split(argument):
   185 
   185 
   186 
   186 
   187 def compile_template_file(filepath):
   187 def compile_template_file(filepath):
   188     """compiles a TAL template file
   188     """compiles a TAL template file
   189     :type filepath: str
   189     :type filepath: str
   190     :param template: path of the file to compile 
   190     :param template: path of the file to compile
   191     """
   191     """
   192     fp = file(filepath)
   192     fp = file(filepath)
   193     file_content = unicode(fp.read()) # template file should be pure ASCII
   193     file_content = unicode(fp.read()) # template file should be pure ASCII
   194     fp.close()
   194     fp.close()
   195     return compile_template(file_content)
   195     return compile_template(file_content)
   251             filepath = join(fileordirectory, self.filename)
   251             filepath = join(fileordirectory, self.filename)
   252             if isdir(fileordirectory) and exists(filepath):
   252             if isdir(fileordirectory) and exists(filepath):
   253                 return compile_template_file(filepath)
   253                 return compile_template_file(filepath)
   254         raise Exception('no such template %s' % self.filename)
   254         raise Exception('no such template %s' % self.filename)
   255     _compiled_template = cached(_compiled_template, 0)
   255     _compiled_template = cached(_compiled_template, 0)
   256