call the python script class PythonScript; try to handle unicode correctly

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1869 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-08-03 09:50:32 +00:00
parent 273a618a28
commit 0af3bb6512
5 changed files with 51 additions and 51 deletions

View file

@ -1,22 +1,22 @@
============
Python Pages
============
==============
Python Scripts
==============
Persistent Python Page - Content Type
Persistent Python Scripts
Examples:
>>> from cybertools.pyscript.tests import Root
>>> from cybertools.pyscript.script import PythonPage
>>> from cybertools.pyscript.script import PythonScript
>>> pp = PythonPage()
>>> pp = PythonScript()
>>> pp.__parent__ = Root()
>>> pp.__name__ = 'pp'
>>> request = None
Test that can produce the correct filename
>>> pp._PythonPage__filename()
>>> pp._PythonScript__filename()
u'/pp'
A simple test that checks that any lone-standing triple-quotes are
@ -24,50 +24,49 @@ being printed.
>>> pp.setSource(u"'''<html>...</html>'''\nreturn printed")
>>> pp(request)
'<html>...</html>\n'
u'<html>...</html>\n'
Make sure that strings with prefixes work.
>>> pp.setSource(ur"ur'''test\r'''" + "\nreturn printed")
>>> pp.setSource(ur"ur'''test\\r'''" + "\nreturn printed")
>>> pp(request)
'test\\r\n'
u'test\\r\n'
Make sure that Windows (\r\n) line ends also work.
>>> pp.setSource(u"if 1 == 1:\r\n\r\n '''<html>...</html>'''\nreturn printed")
>>> pp(request)
'<html>...</html>\n'
u'<html>...</html>\n'
Make sure that unicode strings work as expected.
>>> #pp.setSource(u"u'''\u0442\u0435\u0441\u0442'''")
>>> #pp(request)
u'\u0442\u0435\u0441\u0442\n'
>>> pp.setSource(u"u'''\u0442\u0435\u0441\u0442'''\nreturn printed")
>>> pp(request)
u'\u0442\u0435\u0441\u0442\n'
Make sure that multi-line strings work.
>>> pp.setSource(u"u'''test\ntest\ntest'''\nreturn printed")
>>> pp(request)
'test\n...test\n...test\n'
u'test\n...test\n...test\n'
Here you can see a simple Python command...
>>> pp.setSource(u"print u'<html>...</html>'\nreturn printed")
>>> pp(request)
'<html>...</html>\n'
u'<html>...</html>\n'
... and here a triple quote with some variable replacement.
>>> pp.setSource(u"'''<html>%s</html>''' %x\nreturn printed")
>>> pp(request, x='test')
'<html>test</html>\n'
u'<html>test</html>\n'
Make sure that the context of the page is available.
>>> pp.setSource(u"'''<html>%s</html>''' %context.__name__\nreturn printed")
>>> pp(request)
'<html>root</html>\n'
u'<html>root</html>\n'
Make sure that faulty syntax is interpreted correctly.

View file

@ -20,17 +20,20 @@ from zope.app.form.browser.editview import EditView
from zope.app.i18n import ZopeMessageFactory as _
class PythonPageEval(object):
"""Evaluate the Python Page."""
class PythonScriptEval(object):
"""Evaluate the Python Script."""
def index(self, **kw):
"""Call a Python Page"""
self.request.response.setHeader('content-type',
self.context.contentType)
return str(self.context(self.request, **kw))
result = self.context(self.request, **kw)
if type(result) is unicode:
return result
return unicode(result)
class PythonPageEditView(EditView):
class PythonScriptEditView(EditView):
"""Edit View Class for Python Page."""
syntaxError = None

View file

@ -4,19 +4,19 @@
i18n_domain="zope">
<interface
interface=".interfaces.IPythonPage"
interface=".interfaces.IPythonScript"
type="zope.app.content.interfaces.IContentType"
/>
<class class=".script.PythonPage">
<class class=".script.PythonScript">
<factory
id="cybertools.pyscript.PythonPage"
id="cybertools.pyscript.PythonScript"
title="Python Page"
description="A simple, content-based Python Page"
description="A simple, content-based Python Script"
/>
<require
permission="zope.View"
interface=".interfaces.IPythonPage"
interface=".interfaces.IPythonScript"
/>
<require
permission="zope.ManageContent"
@ -32,34 +32,34 @@
<browser:page
name="index.html"
for=".interfaces.IPythonPage"
class=".browser.PythonPageEval"
for=".interfaces.IPythonScript"
class=".browser.PythonScriptEval"
attribute="index"
permission="zope.View"
/>
<browser:addform
label="Add Python Page"
name="AddPythonPage.html"
schema=".interfaces.IPythonPage"
content_factory=".script.PythonPage"
label="Add Python Script"
name="AddPythonScript.html"
schema=".interfaces.IPythonScript"
content_factory=".script.PythonScript"
permission="zope.ManageContent"
/>
<browser:addMenuItem
class=".script.PythonPage"
class=".script.PythonScript"
title="Python Page"
description="An Python Page"
permission="zope.ManageContent"
view="AddPythonPage.html"
view="AddPythonScript.html"
/>
<browser:editform
for=".interfaces.IPythonPage"
schema=".interfaces.IPythonPage"
for=".interfaces.IPythonScript"
schema=".interfaces.IPythonScript"
name="edit.html"
label="Edit Python Page"
class=".browser.PythonPageEditView"
class=".browser.PythonScriptEditView"
template="edit.pt"
permission="zope.ManageContent"
menu="zmi_views" title="Edit"
@ -70,7 +70,7 @@
<configure package="zope.app.preview">
<browser:page
for="cybertools.pyscript.interfaces.IPythonPage"
for="cybertools.pyscript.interfaces.IPythonScript"
name="preview.html"
template="preview.pt"
permission="zope.ManageContent"

View file

@ -27,8 +27,8 @@ from zope import schema
from zope.app.i18n import ZopeMessageFactory as _
class IPythonPage(Interface):
"""Python Page
class IPythonScript(Interface):
"""Python Script, derived from zope.app.pythonpage.PythonPage.
The Python Page acts as a simple content type that allows you to execute
Python in content space. Additionally, if you have a free-standing

View file

@ -31,20 +31,20 @@ from zope.app.container.contained import Contained
from zope.interface import implements
from zope.app.i18n import ZopeMessageFactory as _
from cybertools.pyscript.interfaces import IPythonPage
from cybertools.pyscript.interfaces import IPythonScript
class PythonPage(Contained, Persistent):
class PythonScript(Contained, Persistent):
"""Persistent Python Page - Content Type
"""
implements(IPythonPage)
implements(IPythonScript)
_v_compiled = None
def __init__(self, source=u'', contentType=u'text/plain'):
"""Initialize the object."""
super(PythonPage, self).__init__()
super(PythonScript, self).__init__()
self.source = source
self.contentType = contentType
@ -80,7 +80,7 @@ class PythonPage(Contained, Persistent):
source = source.encode('ascii')
except UnicodeEncodeError:
return self._tripleQuotedString.sub(_print_usrc, source)
return self._tripleQuotedString.sub(r"\1print u\2\3", source)
return self._tripleQuotedString.sub(r"\1print \2\3", source)
def getSource(self):
@ -102,7 +102,7 @@ class PythonPage(Contained, Persistent):
self._v_compiled(kw)
result = kw['script_result']
if result == output:
result = result.getvalue()
result = result.getvalue().decode('unicode-escape')
return result
@ -122,9 +122,7 @@ class Function(object):
def __call__(self, globals):
globals['__builtins__'] = SafeBuiltins
#fct = new.function(self.code, globals)
exec self.code in globals, None
#return fct()
def _print_usrc(match):
@ -132,4 +130,4 @@ def _print_usrc(match):
raw = match.group(2)
if raw:
return match.group(1)+'print '+`string`
return match.group(1)+'print u'+match.group(3).encode('unicode-escape')
return match.group(1)+'print '+match.group(3).encode('unicode-escape')