[Zope3-checkins] CVS: Zope3/src/zope/app/interpreter -
__init__.py:1.2 configure.zcml:1.2 meta.zcml:1.2
metaconfigure.py:1.2 metadirectives.py:1.2 python.py:1.2
Stephan Richter
srichter at cosmos.phy.tufts.edu
Thu Aug 21 11:19:55 EDT 2003
Update of /cvs-repository/Zope3/src/zope/app/interpreter
In directory cvs.zope.org:/tmp/cvs-serv8485/src/zope/app/interpreter
Added Files:
__init__.py configure.zcml meta.zcml metaconfigure.py
metadirectives.py python.py
Log Message:
Final HEAD checkin of "Inline Code Support in TAL". For detailed messages
during the development, see "srichter-inlinepython-branch". I tested the
code with both, Python 2.2.3 and Python 2.3 and all works fine.
Here an example of what you can do:
<script type="text/server-python">
print "Hello World!"
</script>
and
<p tal:script="text/server-python">
print "Hello World!"
</p>
A more elaborate example would be:
<html><body>
<script type="text/server-python">
global x
x = "Hello World!"
</script>
<b tal:content="x" />
</body></html>
This support is currently only available in "Templated Pages" after you
activate the hook using the "Inline Code" screen.
=== Zope3/src/zope/app/interpreter/__init__.py 1.1 => 1.2 ===
--- /dev/null Thu Aug 21 10:19:55 2003
+++ Zope3/src/zope/app/interpreter/__init__.py Thu Aug 21 10:19:24 2003
@@ -0,0 +1,46 @@
+##############################################################################
+#
+# 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$
+"""
+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__
=== Zope3/src/zope/app/interpreter/configure.zcml 1.1 => 1.2 ===
--- /dev/null Thu Aug 21 10:19:55 2003
+++ Zope3/src/zope/app/interpreter/configure.zcml Thu Aug 21 10:19:24 2003
@@ -0,0 +1,18 @@
+<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>
=== Zope3/src/zope/app/interpreter/meta.zcml 1.1 => 1.2 ===
--- /dev/null Thu Aug 21 10:19:55 2003
+++ Zope3/src/zope/app/interpreter/meta.zcml Thu Aug 21 10:19:24 2003
@@ -0,0 +1,13 @@
+<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>
=== Zope3/src/zope/app/interpreter/metaconfigure.py 1.1 => 1.2 ===
--- /dev/null Thu Aug 21 10:19:55 2003
+++ Zope3/src/zope/app/interpreter/metaconfigure.py Thu Aug 21 10:19:24 2003
@@ -0,0 +1,25 @@
+##############################################################################
+#
+# 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$
+"""
+from zope.app.interpreter import provideInterpreter
+
+def registerInterpreter(_context, type, component):
+ return _context.action(
+ discriminator = ('code', 'registerInterpreter', type),
+ callable = provideInterpreter,
+ args = (type, component)
+ )
=== Zope3/src/zope/app/interpreter/metadirectives.py 1.1 => 1.2 ===
--- /dev/null Thu Aug 21 10:19:55 2003
+++ Zope3/src/zope/app/interpreter/metadirectives.py Thu Aug 21 10:19:24 2003
@@ -0,0 +1,33 @@
+##############################################################################
+#
+# 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$
+"""
+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)
+
=== Zope3/src/zope/app/interpreter/python.py 1.1 => 1.2 ===
--- /dev/null Thu Aug 21 10:19:55 2003
+++ Zope3/src/zope/app/interpreter/python.py Thu Aug 21 10:19:24 2003
@@ -0,0 +1,59 @@
+##############################################################################
+#
+# 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$
+"""
+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:
+ # The newline character is for Python 2.2 :(
+ ri.ri_exec(code+'\n')
+ 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