Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 
00002 import inspect
00003 
00004 # --
00005 # Getting information about the caller template
00006 # --
00007 
00008 class FrameError(Exception):
00009     pass
00010     
00011 def get_econtext():
00012     '''Inspects full context of the caller page
00013 
00014     We access the caller stack and thus the *entire*
00015     context of the page template, including globals (defines).
00016     (This could not be passed from the template, normally, due to
00017     its restricted nature.)
00018 
00019     Some explanation:
00020 
00021     We walk up the the python expression stack until we find the
00022     Pagetemplates.ZRPythonExpr.__call__ that has econtext
00023     in the locals. The econtext is a 
00024     <Products.PageTemplates.Expressions.ZopeContext object that has: 
00025 
00026         vars:           global and local variables combined, readonly.
00027 
00028         setLocal:       set local context variable
00029 
00030         setGlobal:      set global context variable
00031     
00032     '''
00033     frame = inspect.currentframe().f_back
00034     econtext = None
00035     try:
00036         while frame is not None:
00037             try:
00038                 econtext = frame.f_locals['econtext']
00039             except KeyError:
00040                 # go one frame up
00041                 frame = frame.f_back
00042             else:
00043                 # found
00044                 return econtext
00045         else: 
00046             # Econtext frame not found
00047             return None
00048     finally:
00049         del frame
00050         del econtext