[Zope3-checkins] CVS: Zope3/src/zope/app/interpreter - __init__.py:1.1.2.1 configure.zcml:1.1.2.1 meta.zcml:1.1.2.1 metaconfigure.py:1.1.2.1 metadirectives.py:1.1.2.1 python.py:1.1.2.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Aug 20 21:11:12 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/interpreter
In directory cvs.zope.org:/tmp/cvs-serv26608/interpreter

Added Files:
      Tag: srichter-inlinepython-branch
	__init__.py configure.zcml meta.zcml metaconfigure.py 
	metadirectives.py python.py 
Log Message:
Hooked up the new TAL script support to Templated Pages. The support has to
be manually activated on a separate page (as requested by __gotcha).

Also, currently we only have support for the "text/server-python" type. 
Later we might want to add other languages too.

TO DO:

  - tests

  - documentation



=== Added File Zope3/src/zope/app/interpreter/__init__.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Code Interpreters Service

$Id: __init__.py,v 1.1.2.1 2003/08/21 00:11:11 srichter Exp $
"""
from zope.app.interfaces.interpreter import \
     IGlobalInterpreterService, IInterpreter
from zope.interface import implements


class GlobalInterpreterService:

    implements(IGlobalInterpreterService)

    def __init__(self):
        self.__registry = {}

    def getInterpreter(self, type):
        """See zope.app.interfaces.IInterpreterService"""
        return self.__registry[type]

    def queryInterpreter(self, type, default=None):
        """See zope.app.interfaces.IInterpreterService"""
        return self.__registry.get(type, default)

    def provideInterpreter(self, type, interpreter):
        """See zope.app.interfaces.IGlobalInterpreterService"""
        assert IInterpreter.isImplementedBy(interpreter)
        self.__registry[type] = interpreter
        

interpreterService = GlobalInterpreterService()
provideInterpreter = interpreterService.provideInterpreter
_clear = interpreterService.__init__


=== Added File Zope3/src/zope/app/interpreter/configure.zcml ===
<configure 
    xmlns="http://namespaces.zope.org/zope"
    xmlns:code="http://namespaces.zope.org/code">

  <serviceType 
      id="Interpreter" 
      interface="zope.app.interfaces.interpreter.IInterpreterService" />

  <service 
      serviceType="Interpreter" 
      component="zope.app.interpreter.interpreterService" />


  <code:registerInterpreter
      type="text/server-python"
      component=".python.PythonInterpreter" />

</configure>


=== Added File Zope3/src/zope/app/interpreter/meta.zcml ===
<configure
    xmlns:meta="http://namespaces.zope.org/meta">

  <meta:directives namespace="http://namespaces.zope.org/code">

    <meta:directive
        name="registerInterpreter"
        schema=".metadirectives.IRegisterInterpreterDirective"
        handler=".metaconfigure.registerInterpreter" />

  </meta:directives>

</configure>


=== Added File Zope3/src/zope/app/interpreter/metaconfigure.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Handle code: directives

$Id: metaconfigure.py,v 1.1.2.1 2003/08/21 00:11:11 srichter Exp $
"""
from zope.app.interpreter import provideInterpreter

def registerInterpreter(_context, type, component):
    return _context.action(
        discriminator = ('code', 'registerInterpreter', type),
        callable = provideInterpreter,
        args = (type, component)
        )


=== Added File Zope3/src/zope/app/interpreter/metadirectives.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Code Interpreter Service Directives

$Id: metadirectives.py,v 1.1.2.1 2003/08/21 00:11:11 srichter Exp $
"""
from zope.interface import Interface
from zope.configuration.fields import GlobalObject
from zope.schema import TextLine

class IRegisterInterpreterDirective(Interface):

    type = TextLine(
        title=u"Type",
        description=u"Type/name of the language in content type format.",
        required=True)

    component = GlobalObject(
        title=u"Interpreter Component",
        description=u"Path to the interpreter instance.",
        required=True)



=== Added File Zope3/src/zope/app/interpreter/python.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Python Code Interpreter

$Id: python.py,v 1.1.2.1 2003/08/21 00:11:11 srichter Exp $
"""
import sys, StringIO
from zope.app.interfaces.interpreter import IInterpreter
from zope.interface import implements
from zope.security.interpreter import RestrictedInterpreter

class PythonInterpreter:

    implements(IInterpreter)

    def evaluate(self, code, globals):
        """See zope.app.interfaces.IInterpreter"""
        tmp = sys.stdout
        sys.stdout = StringIO.StringIO()
        ri = RestrictedInterpreter()
        ri.globals = globals
        try:
            ri.ri_exec(code)
        finally:
            result = sys.stdout
            sys.stdout = tmp
        return result.getvalue()
        

    def evaluateRawCode(self, code, globals):
        """See zope.app.interfaces.IInterpreter"""
        # Removing probable comments
        if code.strip().startswith('<!--') and code.strip().endswith('-->'):
            code = code.strip()[4:-3]

        # Prepare code.
        lines = code.split('\n')
        lines = filter(lambda l: l.strip() != '', lines)
        code = '\n'.join(lines)
        # This saves us from all indentation issues :)
        if code.startswith(' ') or code.startswith('\t'):
            code = 'if 1 == 1:\n' + code
        return self.evaluate(code, globals)
        

# It's a singelton for now.
PythonInterpreter = PythonInterpreter()




More information about the Zope3-Checkins mailing list