[Zope-Checkins] CVS: Zope/lib/python/Products/PageTemplates - PageTemplateFile.py:1.24
Fred L. Drake, Jr.
fred@zope.com
Fri, 21 Mar 2003 14:22:30 -0500
Update of /cvs-repository/Zope/lib/python/Products/PageTemplates
In directory cvs.zope.org:/tmp/cvs-serv20502
Modified Files:
PageTemplateFile.py
Log Message:
Backport the type-sniffing code from Zope 3 to support XML page templates
from the filesystem.
=== Zope/lib/python/Products/PageTemplates/PageTemplateFile.py 1.23 => 1.24 ===
--- Zope/lib/python/Products/PageTemplates/PageTemplateFile.py:1.23 Tue Feb 11 12:17:07 2003
+++ Zope/lib/python/Products/PageTemplates/PageTemplateFile.py Fri Mar 21 14:22:00 2003
@@ -117,7 +117,12 @@
mtime = 0
if self._v_program is not None and mtime == self._v_last_read:
return
- self.pt_edit(open(self.filename), None)
+ f = open(self.filename, "rb")
+ try:
+ text = f.read()
+ finally:
+ f.close()
+ self.pt_edit(text, sniff_type(text))
self._cook()
if self._v_errors:
LOG('PageTemplateFile', ERROR, 'Error in template',
@@ -154,3 +159,19 @@
from ZODB.POSException import StorageError
raise StorageError, ("Instance of AntiPersistent class %s "
"cannot be stored." % self.__class__.__name__)
+
+
+XML_PREFIXES = [
+ "<?xml", # ascii, utf-8
+ "\xef\xbb\xbf<?xml", # utf-8 w/ byte order mark
+ "\0<\0?\0x\0m\0l", # utf-16 big endian
+ "<\0?\0x\0m\0l\0", # utf-16 little endian
+ "\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
+ ]
+
+def sniff_type(text):
+ for prefix in XML_PREFIXES:
+ if text.startswith(prefix):
+ return "text/xml"
+ return None