[Zope3-checkins] CVS: Zope3/src/zope/tal - dummyengine.py:1.14 htmltalparser.py:1.5 interfaces.py:1.10 taldefs.py:1.7 talgenerator.py:1.12 talgettext.py:1.17 talinterpreter.py:1.29

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


Update of /cvs-repository/Zope3/src/zope/tal
In directory cvs.zope.org:/tmp/cvs-serv8485/src/zope/tal

Modified Files:
	dummyengine.py htmltalparser.py interfaces.py taldefs.py 
	talgenerator.py talgettext.py talinterpreter.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/tal/dummyengine.py 1.13 => 1.14 ===
--- Zope3/src/zope/tal/dummyengine.py:1.13	Fri Aug 15 10:02:37 2003
+++ Zope3/src/zope/tal/dummyengine.py	Thu Aug 21 10:19:29 2003
@@ -45,6 +45,7 @@
         self.locals = self.globals = dict
         self.stack = [dict]
         self.translationService = DummyTranslationService()
+        self.useEngineAttrDicts = False
 
     def getCompilerError(self):
         return CompilerError
@@ -197,6 +198,47 @@
         return self.translationService.translate(
             msgid, domain, mapping, default=default)
 
+    def evaluateCode(self, lang, code):
+        # We probably implement too much, but I use the dummy engine to test
+        # some of the issues that we will have.
+
+        # For testing purposes only
+        locals = {}
+        globals = {}
+        if self.useEngineAttrDicts:
+            globals = self.globals.copy()
+            locals = self.locals.copy()
+            
+        assert lang == 'text/server-python'
+        import sys, StringIO
+
+        # 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 + '\n'
+        tmp = sys.stdout
+        sys.stdout = StringIO.StringIO()
+        try:
+            exec code in globals, locals
+        finally:
+            result = sys.stdout
+            sys.stdout = tmp
+
+        # For testing purposes only
+        self.codeLocals = locals
+        self.codeGlobals = globals
+
+        self.locals.update(locals)
+        self.globals.update(globals)
+
+        return result.getvalue()
 
 class Iterator:
 


=== Zope3/src/zope/tal/htmltalparser.py 1.4 => 1.5 ===
--- Zope3/src/zope/tal/htmltalparser.py:1.4	Thu May  1 15:35:49 2003
+++ Zope3/src/zope/tal/htmltalparser.py	Thu Aug 21 10:19:29 2003
@@ -11,8 +11,9 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""
-Parse HTML and compile to TALInterpreter intermediate code.
+"""Parse HTML and compile to TALInterpreter intermediate code.
+
+$Id$
 """
 
 from HTMLParser import HTMLParser, HTMLParseError
@@ -146,6 +147,12 @@
             raise TALError(
                 "empty HTML tags cannot use tal:content: %s" % `tag`,
                 self.getpos())
+        # Support for inline Python code.
+        if tag == 'script':
+            type_attr = filter(lambda a: a[0] == 'type', attrlist)
+            if type_attr and type_attr[0][1].startswith('text/server-'):
+                attrlist.remove(type_attr[0])
+                taldict = {'script': type_attr[0][1], 'omit-tag': ''}
         self.tagstack.append(tag)
         self.gen.emitStartElement(tag, attrlist, taldict, metaldict, i18ndict,
                                   self.getpos())


=== Zope3/src/zope/tal/interfaces.py 1.9 => 1.10 ===
--- Zope3/src/zope/tal/interfaces.py:1.9	Tue Jul  1 13:31:03 2003
+++ Zope3/src/zope/tal/interfaces.py	Thu Aug 21 10:19:29 2003
@@ -149,9 +149,16 @@
         """
 
     def translate(msgid, domain=None, mapping=None, default=None):
+        """See ITranslationService.translate()"""
+
+    def evaluateCode(lang, code):
+        """Evaluates code of the given language.
+
+        Returns whatever the code outputs. This can be defined on a
+        per-language basis. In Python this usually everything the print
+        statement will return.
         """
-        See ITranslationService.translate()
-        """
+        
 
 class ITALIterator(Interface):
     """A TAL iterator
@@ -164,7 +171,6 @@
 
         Return a true value if it was possible to advance and return
         a false value otherwise.
-
         """
 
 


=== Zope3/src/zope/tal/taldefs.py 1.6 => 1.7 ===
--- Zope3/src/zope/tal/taldefs.py:1.6	Tue Jun  3 11:20:06 2003
+++ Zope3/src/zope/tal/taldefs.py	Thu Aug 21 10:19:29 2003
@@ -48,6 +48,7 @@
     "on-error",
     "omit-tag",
     "tal tag",
+    "script"
     ]
 
 KNOWN_I18N_ATTRIBUTES = [


=== Zope3/src/zope/tal/talgenerator.py 1.11 => 1.12 ===
--- Zope3/src/zope/tal/talgenerator.py:1.11	Thu Aug 14 13:23:18 2003
+++ Zope3/src/zope/tal/talgenerator.py	Thu Aug 21 10:19:29 2003
@@ -319,6 +319,10 @@
             assert key == "structure"
             self.emit("insertStructure", cexpr, attrDict, program)
 
+    def emitEvaluateCode(self, lang):
+        program = self.popProgram()
+        self.emit('evaluateCode', lang, program)
+
     def emitI18nVariable(self, stuff):
         # Used for i18n:name attributes.  arg is extra information describing
         # how the contents of the variable should get filled in, and it will
@@ -512,6 +516,7 @@
         repeat = taldict.get("repeat")
         content = taldict.get("content")
         replace = taldict.get("replace")
+        script = taldict.get("script")
         attrsubst = taldict.get("attributes")
         onError = taldict.get("on-error")
         omitTag = taldict.get("omit-tag")
@@ -689,6 +694,8 @@
         if replace:
             todo["repldict"] = repldict
             repldict = {}
+        if script:
+            todo["script"] = script    
         self.emitStartTag(name, self.replaceAttrs(attrlist, repldict), isend)
         if optTag:
             self.pushProgram()
@@ -698,6 +705,8 @@
             self.pushProgram()
         if content and varname:
             self.pushProgram()
+        if script:
+            self.pushProgram()            
         if todo and position != (None, None):
             todo["position"] = position
         self.todoPush(todo)
@@ -720,6 +729,7 @@
         repeat = todo.get("repeat")
         content = todo.get("content")
         replace = todo.get("replace")
+        script = todo.get("script")
         condition = todo.get("condition")
         onError = todo.get("onError")
         repldict = todo.get("repldict", {})
@@ -740,6 +750,8 @@
             raise exc("%s attributes on <%s> require explicit </%s>" %
                       (what, name, name), position)
 
+        if script:
+            self.emitEvaluateCode(script)
         # If there's no tal:content or tal:replace in the tag with the
         # i18n:name, tal:replace is the default.
         if content:


=== Zope3/src/zope/tal/talgettext.py 1.16 => 1.17 ===
--- Zope3/src/zope/tal/talgettext.py:1.16	Mon Aug 18 16:44:54 2003
+++ Zope3/src/zope/tal/talgettext.py	Thu Aug 21 10:19:29 2003
@@ -12,7 +12,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-
 """Program to extract internationalization markup from Page Templates.
 
 Once you have marked up a Page Template file with i18n: namespace tags, use


=== Zope3/src/zope/tal/talinterpreter.py 1.28 => 1.29 ===
--- Zope3/src/zope/tal/talinterpreter.py:1.28	Fri Aug 15 10:02:37 2003
+++ Zope3/src/zope/tal/talinterpreter.py	Thu Aug 21 10:19:29 2003
@@ -586,6 +586,21 @@
         program, macros = gen.getCode()
         self.interpret(program)
 
+    def do_evaluateCode(self, stuff):
+        lang, program = stuff
+        # Use a temporary stream to capture the interpretation of the
+        # subnodes, which should /not/ go to the output stream.
+        tmpstream = StringIO()
+        self.pushStream(tmpstream)
+        try:
+            self.interpret(program)
+        finally:
+            self.popStream()
+        code = tmpstream.getvalue()
+        output = self.engine.evaluateCode(lang, code)
+        self._stream_write(output)        
+    bytecode_handlers["evaluateCode"] = do_evaluateCode
+
     def do_loop(self, (name, expr, block)):
         self.interpret(block)
 




More information about the Zope3-Checkins mailing list