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:
		
							parent
							
								
									273a618a28
								
							
						
					
					
						commit
						0af3bb6512
					
				
					 5 changed files with 51 additions and 51 deletions
				
			
		|  | @ -1,22 +1,22 @@ | ||||||
| ============ | ============== | ||||||
| Python Pages | Python Scripts | ||||||
| ============ | ============== | ||||||
| 
 | 
 | ||||||
| Persistent Python Page - Content Type | Persistent Python Scripts | ||||||
| 
 | 
 | ||||||
| Examples: | Examples: | ||||||
| 
 | 
 | ||||||
|   >>> from cybertools.pyscript.tests import Root |   >>> 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.__parent__ = Root() | ||||||
|   >>> pp.__name__ = 'pp' |   >>> pp.__name__ = 'pp' | ||||||
|   >>> request = None |   >>> request = None | ||||||
| 
 | 
 | ||||||
| Test that can produce the correct filename | Test that can produce the correct filename | ||||||
| 
 | 
 | ||||||
|   >>> pp._PythonPage__filename() |   >>> pp._PythonScript__filename() | ||||||
|   u'/pp' |   u'/pp' | ||||||
| 
 | 
 | ||||||
| A simple test that checks that any lone-standing triple-quotes are | 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.setSource(u"'''<html>...</html>'''\nreturn printed") | ||||||
|   >>> pp(request) |   >>> pp(request) | ||||||
|   '<html>...</html>\n' |   u'<html>...</html>\n' | ||||||
| 
 | 
 | ||||||
| Make sure that strings with prefixes work. | 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) |   >>> pp(request) | ||||||
|   'test\\r\n' |   u'test\\r\n' | ||||||
| 
 | 
 | ||||||
| Make sure that Windows (\r\n) line ends also work. | 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.setSource(u"if 1 == 1:\r\n\r\n   '''<html>...</html>'''\nreturn printed") | ||||||
|   >>> pp(request) |   >>> pp(request) | ||||||
|   '<html>...</html>\n' |   u'<html>...</html>\n' | ||||||
| 
 | 
 | ||||||
| Make sure that unicode strings work as expected. | Make sure that unicode strings work as expected. | ||||||
| 
 | 
 | ||||||
|   >>> #pp.setSource(u"u'''\u0442\u0435\u0441\u0442'''") |   >>> pp.setSource(u"u'''\u0442\u0435\u0441\u0442'''\nreturn printed") | ||||||
|   >>> #pp(request) |   >>> pp(request) | ||||||
| 
 |   u'\u0442\u0435\u0441\u0442\n' | ||||||
| u'\u0442\u0435\u0441\u0442\n' |  | ||||||
| 
 | 
 | ||||||
| Make sure that multi-line strings work. | Make sure that multi-line strings work. | ||||||
| 
 | 
 | ||||||
|   >>> pp.setSource(u"u'''test\ntest\ntest'''\nreturn printed") |   >>> pp.setSource(u"u'''test\ntest\ntest'''\nreturn printed") | ||||||
|   >>> pp(request) |   >>> pp(request) | ||||||
|   'test\n...test\n...test\n' |   u'test\n...test\n...test\n' | ||||||
| 
 | 
 | ||||||
| Here you can see a simple Python command... | Here you can see a simple Python command... | ||||||
| 
 | 
 | ||||||
|   >>> pp.setSource(u"print u'<html>...</html>'\nreturn printed") |   >>> pp.setSource(u"print u'<html>...</html>'\nreturn printed") | ||||||
|   >>> pp(request) |   >>> pp(request) | ||||||
|   '<html>...</html>\n' |   u'<html>...</html>\n' | ||||||
| 
 | 
 | ||||||
| ... and here a triple quote with some variable replacement. | ... and here a triple quote with some variable replacement. | ||||||
| 
 | 
 | ||||||
|   >>> pp.setSource(u"'''<html>%s</html>''' %x\nreturn printed") |   >>> pp.setSource(u"'''<html>%s</html>''' %x\nreturn printed") | ||||||
|   >>> pp(request, x='test') |   >>> pp(request, x='test') | ||||||
|   '<html>test</html>\n' |   u'<html>test</html>\n' | ||||||
| 
 | 
 | ||||||
| Make sure that the context of the page is available. | Make sure that the context of the page is available. | ||||||
| 
 | 
 | ||||||
|   >>> pp.setSource(u"'''<html>%s</html>''' %context.__name__\nreturn printed") |   >>> pp.setSource(u"'''<html>%s</html>''' %context.__name__\nreturn printed") | ||||||
|   >>> pp(request) |   >>> pp(request) | ||||||
|   '<html>root</html>\n' |   u'<html>root</html>\n' | ||||||
| 
 | 
 | ||||||
| Make sure that faulty syntax is interpreted correctly. | Make sure that faulty syntax is interpreted correctly. | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -20,17 +20,20 @@ from zope.app.form.browser.editview import EditView | ||||||
| from zope.app.i18n import ZopeMessageFactory as _ | from zope.app.i18n import ZopeMessageFactory as _ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class PythonPageEval(object): | class PythonScriptEval(object): | ||||||
|     """Evaluate the Python Page.""" |     """Evaluate the Python Script.""" | ||||||
| 
 | 
 | ||||||
|     def index(self, **kw): |     def index(self, **kw): | ||||||
|         """Call a Python Page""" |         """Call a Python Page""" | ||||||
|         self.request.response.setHeader('content-type', |         self.request.response.setHeader('content-type', | ||||||
|                                         self.context.contentType) |                                         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.""" |     """Edit View Class for Python Page.""" | ||||||
| 
 | 
 | ||||||
|     syntaxError = None |     syntaxError = None | ||||||
|  |  | ||||||
|  | @ -4,19 +4,19 @@ | ||||||
|     i18n_domain="zope"> |     i18n_domain="zope"> | ||||||
| 
 | 
 | ||||||
|   <interface |   <interface | ||||||
|       interface=".interfaces.IPythonPage" |       interface=".interfaces.IPythonScript" | ||||||
|       type="zope.app.content.interfaces.IContentType" |       type="zope.app.content.interfaces.IContentType" | ||||||
|       /> |       /> | ||||||
| 
 | 
 | ||||||
|   <class class=".script.PythonPage"> |   <class class=".script.PythonScript"> | ||||||
|       <factory |       <factory | ||||||
|         id="cybertools.pyscript.PythonPage" |         id="cybertools.pyscript.PythonScript" | ||||||
|         title="Python Page" |         title="Python Page" | ||||||
|         description="A simple, content-based Python Page" |         description="A simple, content-based Python Script" | ||||||
|         /> |         /> | ||||||
|     <require |     <require | ||||||
|         permission="zope.View" |         permission="zope.View" | ||||||
|         interface=".interfaces.IPythonPage" |         interface=".interfaces.IPythonScript" | ||||||
|         /> |         /> | ||||||
|     <require |     <require | ||||||
|         permission="zope.ManageContent" |         permission="zope.ManageContent" | ||||||
|  | @ -32,34 +32,34 @@ | ||||||
| 
 | 
 | ||||||
|   <browser:page |   <browser:page | ||||||
|       name="index.html" |       name="index.html" | ||||||
|       for=".interfaces.IPythonPage" |       for=".interfaces.IPythonScript" | ||||||
|       class=".browser.PythonPageEval" |       class=".browser.PythonScriptEval" | ||||||
|       attribute="index" |       attribute="index" | ||||||
|       permission="zope.View" |       permission="zope.View" | ||||||
|       /> |       /> | ||||||
| 
 | 
 | ||||||
|   <browser:addform |   <browser:addform | ||||||
|       label="Add Python Page" |       label="Add Python Script" | ||||||
|       name="AddPythonPage.html" |       name="AddPythonScript.html" | ||||||
|       schema=".interfaces.IPythonPage" |       schema=".interfaces.IPythonScript" | ||||||
|       content_factory=".script.PythonPage" |       content_factory=".script.PythonScript" | ||||||
|       permission="zope.ManageContent" |       permission="zope.ManageContent" | ||||||
|       /> |       /> | ||||||
| 
 | 
 | ||||||
|   <browser:addMenuItem |   <browser:addMenuItem | ||||||
|       class=".script.PythonPage" |       class=".script.PythonScript" | ||||||
|       title="Python Page" |       title="Python Page" | ||||||
|       description="An Python Page" |       description="An Python Page" | ||||||
|       permission="zope.ManageContent" |       permission="zope.ManageContent" | ||||||
|       view="AddPythonPage.html" |       view="AddPythonScript.html" | ||||||
|       /> |       /> | ||||||
| 
 | 
 | ||||||
|   <browser:editform |   <browser:editform | ||||||
|       for=".interfaces.IPythonPage" |       for=".interfaces.IPythonScript" | ||||||
|       schema=".interfaces.IPythonPage" |       schema=".interfaces.IPythonScript" | ||||||
|       name="edit.html" |       name="edit.html" | ||||||
|       label="Edit Python Page" |       label="Edit Python Page" | ||||||
|       class=".browser.PythonPageEditView" |       class=".browser.PythonScriptEditView" | ||||||
|       template="edit.pt" |       template="edit.pt" | ||||||
|       permission="zope.ManageContent" |       permission="zope.ManageContent" | ||||||
|       menu="zmi_views" title="Edit" |       menu="zmi_views" title="Edit" | ||||||
|  | @ -70,7 +70,7 @@ | ||||||
| 
 | 
 | ||||||
|   <configure package="zope.app.preview"> |   <configure package="zope.app.preview"> | ||||||
|     <browser:page |     <browser:page | ||||||
|         for="cybertools.pyscript.interfaces.IPythonPage" |         for="cybertools.pyscript.interfaces.IPythonScript" | ||||||
|         name="preview.html" |         name="preview.html" | ||||||
|         template="preview.pt" |         template="preview.pt" | ||||||
|         permission="zope.ManageContent" |         permission="zope.ManageContent" | ||||||
|  |  | ||||||
|  | @ -27,8 +27,8 @@ from zope import schema | ||||||
| from zope.app.i18n import ZopeMessageFactory as _ | from zope.app.i18n import ZopeMessageFactory as _ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class IPythonPage(Interface): | class IPythonScript(Interface): | ||||||
|     """Python Page |     """Python Script, derived from zope.app.pythonpage.PythonPage. | ||||||
| 
 | 
 | ||||||
|     The Python Page acts as a simple content type that allows you to execute |     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 |     Python in content space. Additionally, if you have a free-standing | ||||||
|  |  | ||||||
|  | @ -31,20 +31,20 @@ from zope.app.container.contained import Contained | ||||||
| from zope.interface import implements | from zope.interface import implements | ||||||
| from zope.app.i18n import ZopeMessageFactory as _ | 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 |     """Persistent Python Page - Content Type | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
|     implements(IPythonPage) |     implements(IPythonScript) | ||||||
| 
 | 
 | ||||||
|     _v_compiled = None |     _v_compiled = None | ||||||
| 
 | 
 | ||||||
|     def __init__(self, source=u'', contentType=u'text/plain'): |     def __init__(self, source=u'', contentType=u'text/plain'): | ||||||
|         """Initialize the object.""" |         """Initialize the object.""" | ||||||
|         super(PythonPage, self).__init__() |         super(PythonScript, self).__init__() | ||||||
|         self.source = source |         self.source = source | ||||||
|         self.contentType = contentType |         self.contentType = contentType | ||||||
| 
 | 
 | ||||||
|  | @ -80,7 +80,7 @@ class PythonPage(Contained, Persistent): | ||||||
|                 source = source.encode('ascii') |                 source = source.encode('ascii') | ||||||
|             except UnicodeEncodeError: |             except UnicodeEncodeError: | ||||||
|                 return self._tripleQuotedString.sub(_print_usrc, source) |                 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): |     def getSource(self): | ||||||
|  | @ -102,7 +102,7 @@ class PythonPage(Contained, Persistent): | ||||||
|         self._v_compiled(kw) |         self._v_compiled(kw) | ||||||
|         result = kw['script_result'] |         result = kw['script_result'] | ||||||
|         if result == output: |         if result == output: | ||||||
|             result = result.getvalue() |             result = result.getvalue().decode('unicode-escape') | ||||||
|         return result |         return result | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -122,9 +122,7 @@ class Function(object): | ||||||
| 
 | 
 | ||||||
|     def __call__(self, globals): |     def __call__(self, globals): | ||||||
|         globals['__builtins__'] = SafeBuiltins |         globals['__builtins__'] = SafeBuiltins | ||||||
|         #fct = new.function(self.code, globals) |  | ||||||
|         exec self.code in globals, None |         exec self.code in globals, None | ||||||
|         #return fct() |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def _print_usrc(match): | def _print_usrc(match): | ||||||
|  | @ -132,4 +130,4 @@ def _print_usrc(match): | ||||||
|     raw = match.group(2) |     raw = match.group(2) | ||||||
|     if raw: |     if raw: | ||||||
|         return match.group(1)+'print '+`string` |         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') | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 helmutm
						helmutm