"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/roundup/cgi/PageTemplates/PythonExpr.py" (26 Aug 2019, 3641 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "PythonExpr.py": 1.6.1_vs_2.0.0.

    1 ##############################################################################
    2 #
    3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
    4 #
    5 # This software is subject to the provisions of the Zope Public License,
    6 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
    7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
    8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
   10 # FOR A PARTICULAR PURPOSE
   11 #
   12 ##############################################################################
   13 # Modified for Roundup:
   14 # 
   15 # 1. more informative traceback info
   16 
   17 """Generic Python Expression Handler
   18 """
   19 
   20 import symtable
   21 
   22 from .TALES import CompilerError
   23 from sys import exc_info
   24 
   25 class getSecurityManager:
   26     '''Null security manager'''
   27     def validate(self, *args, **kwargs):
   28         return 1
   29     addContext = removeContext = validateValue = validate
   30 
   31 class PythonExpr:
   32     def __init__(self, name, expr, engine):
   33         self.expr = expr = expr.strip().replace('\n', ' ')
   34         try:
   35             d = {}
   36             self.f_code = 'def f():\n return %s\n' % expr.strip()
   37             exec(self.f_code, d)
   38             self._f = d['f']
   39         except:
   40             raise CompilerError(('Python expression error:\n'
   41                                  '%s: %s') % exc_info()[:2])
   42         self._get_used_names()
   43 
   44     def _get_used_names(self):
   45         self._f_varnames = vnames = []
   46         for vname in self._get_from_symtab():
   47             if vname[0] not in '$_.':
   48                 vnames.append(vname)
   49 
   50     def _get_from_symtab(self):
   51         """
   52         Get the variables used in the 'f' function.
   53         """
   54         variables = set()
   55         table = symtable.symtable(self.f_code, "<string>", "exec")
   56         if table.has_children():
   57             variables.update(self._walk_children(table))
   58         return variables
   59 
   60     def _walk_children(self, sym):
   61         """
   62         Get the variables at this level. Recurse to get them all.
   63         """
   64         variables = set()
   65         for child in sym.get_children():
   66             variables.update(set(child.get_identifiers()))
   67             if child.has_children():
   68                 variables.update(self._walk_children(child))
   69         return variables
   70 
   71     def _bind_used_names(self, econtext, _marker=[]):
   72         # Bind template variables
   73         names = {'CONTEXTS': econtext.contexts}
   74         variables = econtext.vars
   75         getType = econtext.getCompiler().getTypes().get
   76         for vname in self._f_varnames:
   77             val = variables.get(vname, _marker)
   78             if val is _marker:
   79                 has = val = getType(vname)
   80                 if has:
   81                     val = ExprTypeProxy(vname, val, econtext)
   82                     names[vname] = val
   83             else:
   84                 names[vname] = val
   85         return names
   86 
   87     def __call__(self, econtext):
   88         __traceback_info__ = 'python expression "%s"'%self.expr
   89         f = self._f
   90         f.__globals__.update(self._bind_used_names(econtext))
   91         return f()
   92 
   93     def __str__(self):
   94         return 'Python expression "%s"' % self.expr
   95     def __repr__(self):
   96         return '<PythonExpr %s>' % self.expr
   97 
   98 class ExprTypeProxy:
   99     '''Class that proxies access to an expression type handler'''
  100     def __init__(self, name, handler, econtext):
  101         self._name = name
  102         self._handler = handler
  103         self._econtext = econtext
  104     def __call__(self, text):
  105         return self._handler(self._name, text,
  106                              self._econtext.getCompiler())(self._econtext)
  107