[Checkins] SVN: Sandbox/malthe/chameleon.core/ Split out template compilation in order to always be able to write source code to disk, even in case of a compile error.

Malthe Borch mborch at gmail.com
Sat Nov 15 18:13:29 EST 2008


Log message for revision 92992:
  Split out template compilation in order to always be able to write source code to disk, even in case of a compile error.

Changed:
  U   Sandbox/malthe/chameleon.core/CHANGES.txt
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/config.py
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/template.py
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py

-=-
Modified: Sandbox/malthe/chameleon.core/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.core/CHANGES.txt	2008-11-15 22:16:46 UTC (rev 92991)
+++ Sandbox/malthe/chameleon.core/CHANGES.txt	2008-11-15 23:13:29 UTC (rev 92992)
@@ -4,6 +4,9 @@
 HEAD
 ~~~~
 
+- Split out compile-function such that we can always write the source
+  code to disk in debug-mode. [malthe]
+
 - Correctly transfer scope to callback-function that will fill macro
   slot contents. Previously, we would precompute fill-slots, but this
   was wrong since it's expected to inherit the scope of the macro

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/config.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/config.py	2008-11-15 22:16:46 UTC (rev 92991)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/config.py	2008-11-15 23:13:29 UTC (rev 92992)
@@ -16,7 +16,9 @@
 
 # when validation is enabled, dynamically inserted content is
 # validated against the XHTML standard
-VALIDATION = DEBUG_MODE
+VALIDATION_KEY = 'CHAMELEON_VALIDATE'
+VALIDATION = os.environ.get(VALIDATION_KEY, 'false')
+VALIDATION = VALIDATION.lower() in TRUEVALS
 
 # use the disable-i18n flag to disable the translation machinery; this
 # will speed up templates that use internationalization

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/template.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/template.py	2008-11-15 22:16:46 UTC (rev 92991)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/template.py	2008-11-15 23:13:29 UTC (rev 92992)
@@ -57,7 +57,9 @@
             encoding=self.encoding)
 
     def cook(self, **kwargs):
-        return self.compiler(**kwargs)
+        template = self.compiler(**kwargs)
+        template.compile()
+        return template
     
     def cook_check(self, parameters, **kwargs):
         key = tuple(parameters), \
@@ -156,11 +158,14 @@
             exception.msg += ' (%s)' % self.filename
             raise exception
 
-        if config.DEBUG_MODE:
-            filename = "%s.py" % self.filename
-            f = open(filename, 'w')
-            f.write(template.source)
-            f.close()
+        try:
+            template.compile()
+        finally:
+            if config.DEBUG_MODE:
+                filename = "%s.py" % self.filename
+                f = open(filename, 'w')
+                f.write(template.source)
+                f.close()
 
         return template
 

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py	2008-11-15 22:16:46 UTC (rev 92991)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/testing.py	2008-11-15 23:13:29 UTC (rev 92992)
@@ -36,17 +36,20 @@
     compiler = TestCompiler.from_text(
         body, mock_parser, implicit_doctype=doctypes.xhtml)
     template = compiler(parameters=sorted(kwargs.keys()))
+    template.compile()
     return template.render(**kwargs)    
 
 def compile_template(parser, body, encoding=None, **kwargs):
     compiler = TestCompiler(
         body, parser, encoding=encoding, implicit_doctype=doctypes.xhtml)
     template = compiler(parameters=sorted(kwargs.keys()))
+    template.compile()
     return template.render(**kwargs)    
 
 class TestCompiler(translation.Compiler):
     def __call__(self, *args, **kwargs):
         template = translation.Compiler.__call__(self, *args, **kwargs)
+        template.compile()
         template = loads(dumps(template))
         return template
 

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py	2008-11-15 22:16:46 UTC (rev 92991)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py	2008-11-15 23:13:29 UTC (rev 92992)
@@ -702,22 +702,22 @@
 class ByteCodeTemplate(object):
     """Template compiled to byte-code."""
 
+    func = None
+    
     def __init__(self, source, symbols, xmldoc, parser, tree):
-        # compile code
-        suite = codegen.Suite(source, globals=symbols)
-        suite._globals.update(symbols)
-        
-        # execute code
-        _locals = {}
-        exec suite.code in suite._globals, _locals
-
-        # keep state
-        self.func = _locals['render']
         self.source = source
         self.symbols = symbols
         self.xmldoc = xmldoc
         self.parser = parser
         self.tree = tree
+
+    def compile(self):
+        suite = codegen.Suite(self.source, globals=self.symbols)
+        suite._globals.update(self.symbols)
+
+        _locals = {}
+        exec suite.code in suite._globals, _locals
+        self.func = _locals['render']
             
     def __reduce__(self):
         reconstructor, (cls, base, state), kwargs = \



More information about the Checkins mailing list