[Zope-Checkins] CVS: Zope/lib/python/Products/PageTemplates - PageTemplateFile.py:1.26

Fred L. Drake, Jr. fred@zope.com
Mon, 7 Apr 2003 15:21:38 -0400


Update of /cvs-repository/Zope/lib/python/Products/PageTemplates
In directory cvs.zope.org:/tmp/cvs-serv32619

Modified Files:
	PageTemplateFile.py 
Log Message:
- avoid re-reading more data than necessary
- remove unused imports
- misc. cleanup


=== Zope/lib/python/Products/PageTemplates/PageTemplateFile.py 1.25 => 1.26 ===
--- Zope/lib/python/Products/PageTemplates/PageTemplateFile.py:1.25	Fri Apr  4 14:37:58 2003
+++ Zope/lib/python/Products/PageTemplates/PageTemplateFile.py	Mon Apr  7 15:21:37 2003
@@ -17,17 +17,16 @@
 
 __version__='$Revision$'[11:-2]
 
-import os, AccessControl, Acquisition, sys
+import os, AccessControl
 from Globals import package_home, DevelopmentMode
-from zLOG import LOG, ERROR, INFO
-from Shared.DC.Scripts.Script import Script, BindingsUI
+from zLOG import LOG, ERROR
+from Shared.DC.Scripts.Script import Script
 from Shared.DC.Scripts.Signature import FuncCode
 from AccessControl import getSecurityManager
 from OFS.Traversable import Traversable
 from PageTemplate import PageTemplate
 from Expressions import SecureModuleImporter
 from ComputedAttribute import ComputedAttribute
-from ExtensionClass import Base
 from Acquisition import aq_parent, aq_inner
 from App.config import getConfiguration
 
@@ -51,7 +50,7 @@
         self.ZBindings_edit(self._default_bindings)
         if _prefix is None:
             _prefix = getConfiguration().softwarehome
-        elif type(_prefix) is not type(''):
+        elif not isinstance(_prefix, str):
             _prefix = package_home(_prefix)
         name = kw.get('__name__')
         if name:
@@ -91,7 +90,7 @@
             pass
 
         # Execute the template in a new security context.
-        security=getSecurityManager()
+        security = getSecurityManager()
         bound_names['user'] = security.getUser()
         security.addContext(self)
         try:
@@ -119,15 +118,18 @@
             return
         f = open(self.filename, "rb")
         try:
-            text = f.read()
-        finally:
+            text = f.read(XML_PREFIX_MAX_LENGTH)
+        except:
             f.close()
+            raise
         t = sniff_type(text)
-        if t != "text/xml" and "\r" in text:
+        if t != "text/xml":
             # For HTML, we really want the file read in text mode:
-            f = open(self.filename)
-            text = f.read()
             f.close()
+            f = open(self.filename)
+            text = ''
+        text += f.read()
+        f.close()
         self.pt_edit(text, t)
         self._cook()
         if self._v_errors:
@@ -179,6 +181,8 @@
     "\xfe\xff\0<\0?\0x\0m\0l",    # utf-16 big endian w/ byte order mark
     "\xff\xfe<\0?\0x\0m\0l\0",    # utf-16 little endian w/ byte order mark
     ]
+
+XML_PREFIX_MAX_LENGTH = max(map(len, XML_PREFIXES))
 
 def sniff_type(text):
     for prefix in XML_PREFIXES: