Back to index

plone3  3.1.7
expression.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 Persistent TALES Expression. Highly inspired (read copied)
00020 from CMFCore's Expression.
00021 
00022 $Id: expression.py 5626 2006-01-24 02:26:47Z dreamcatcher $
00023 """
00024 
00025 from Globals import Persistent, InitializeClass
00026 from Acquisition import aq_inner, aq_parent
00027 from AccessControl import getSecurityManager, ClassSecurityInfo
00028 
00029 from Products.PageTemplates.Expressions import getEngine
00030 from Products.PageTemplates.Expressions import SecureModuleImporter
00031 
00032 class Expression(Persistent):
00033     """A Persistent TALES Expression"""
00034 
00035     _text = ''
00036     _v_compiled = None
00037 
00038     security = ClassSecurityInfo()
00039 
00040     def __init__(self, text):
00041         self._text = text
00042         self._v_compiled = getEngine().compile(text)
00043 
00044     def __call__(self, econtext):
00045         compiled = self._v_compiled
00046         if compiled is None:
00047             compiled = self._v_compiled = getEngine().compile(self._text)
00048         res = compiled(econtext)
00049         if isinstance(res, Exception):
00050             raise res
00051         return res
00052 
00053 InitializeClass(Expression)
00054 
00055 def createExprContext(obj, **kw):
00056     """ Provides names for TALES expressions.
00057     """
00058     if obj is None:
00059         object_url = ''
00060     else:
00061         object_url = obj.absolute_url()
00062 
00063     user = getSecurityManager().getUser()
00064 
00065     data = {
00066         'object_url':   object_url,
00067         'object':       obj,
00068         'nothing':      None,
00069         'request':      getattr(obj, 'REQUEST', None),
00070         'modules':      SecureModuleImporter,
00071         'user':         user,
00072         }
00073     if kw.has_key('mimetype') and not kw.has_key('content_type'):
00074         # Alias content_type to mimetype
00075         kw['content_type'] = kw['mimetype']
00076     data.update(kw)
00077     for k in ('filename', 'content_type', 'data', 'mimetype'):
00078         data.setdefault(k, None)
00079     return getEngine().getContext(data)