[Zope3-checkins] SVN: Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/ Use a filename extension of '.xpt' causes Page Templates to be

Julien Anguenot ja at nuxeo.com
Fri Oct 14 14:20:02 EDT 2005


Log message for revision 39454:
  Use a filename extension of '.xpt' causes Page Templates to be
  processed in XML mode.  This has the advantage that the identification
  of the file as XML is performed outside the content and can be checked
  very quickly.
  
  

Changed:
  U   Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/pagetemplatefile.py
  A   Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/input/test.xpt
  U   Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/test_ptfile.py
  U   Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/typesniffer.py

-=-
Modified: Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/pagetemplatefile.py
===================================================================
--- Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/pagetemplatefile.py	2005-10-14 17:56:52 UTC (rev 39453)
+++ Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/pagetemplatefile.py	2005-10-14 18:20:01 UTC (rev 39454)
@@ -81,7 +81,7 @@
         except:
             f.close()
             raise
-        type_ = sniff_type(text)
+        type_ = sniff_type(text, self.filename)
         if type_ == "text/xml":
             text += f.read()
         else:

Added: Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/input/test.xpt
===================================================================


Property changes on: Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/input/test.xpt
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/test_ptfile.py
===================================================================
--- Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/test_ptfile.py	2005-10-14 17:56:52 UTC (rev 39453)
+++ Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/tests/test_ptfile.py	2005-10-14 18:20:01 UTC (rev 39454)
@@ -177,26 +177,41 @@
     ##    self.assertEqual(type_, 'text/xml')
 
     def test_type_sniffing_based_on_xmlns(self):
-        from zope.pagetemplate.typesniffer import sniff_type
+
         self.assertEqual(
-            sniff_type("<doc><element/></doc>"), None)
+            sniff_type("<doc><element/></doc>", ''), None)
         self.assertEqual(
-            sniff_type("<doc xmlns=''><element/></doc>"), 'text/xml')
+            sniff_type("<doc xmlns=''><element/></doc>", ''), 'text/xml')
         self.assertEqual(
-            sniff_type("<doc><element xmlns=''/></doc>"), 'text/xml')
+            sniff_type("<doc><element xmlns=''/></doc>", ''), 'text/xml')
         self.assertEqual(
-            sniff_type("<doc xmlns='http://foo/bar'><element/></doc>"),
+            sniff_type("<doc xmlns='http://foo/bar'><element/></doc>", ''),
             'text/xml')
         self.assertEqual(
-            sniff_type("<doc ><element xmlns='http://foo/bar'/></doc>"),
+            sniff_type("<doc ><element xmlns='http://foo/bar'/></doc>", ''),
             'text/xml')
         self.assertEqual(
-            sniff_type("<doc xmlns:foo='http://foo/'><element/></doc>"),
+            sniff_type("<doc xmlns:foo='http://foo/'><element/></doc>", ''),
             'text/xml')
         self.assertEqual(
-            sniff_type("<doc ><element xmlns:foo='http://foo/'/></doc>"),
+            sniff_type("<doc ><element xmlns:foo='http://foo/'/></doc>", ''),
             'text/xml')
 
+    def test_type_sniffing_based_on_xpt_extension(self):
+        
+        this_directory = os.path.split(__file__)[0]
+        filepath = os.path.join(this_directory, 'input/test.xpt')
+
+        pt = PageTemplateFile(filepath)
+        text_, type_ = pt._read_file()
+
+        self.assertEqual('text/xml', type_)
+        self.assertEqual(sniff_type(text_, filepath), 'text/xml')
+
+        # After interpretation
+        pt._cook_check()
+        self.assertEqual('text/xml', pt.content_type)
+
 def test_suite():
     return unittest.makeSuite(TypeSniffingTestCase)
 

Modified: Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/typesniffer.py
===================================================================
--- Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/typesniffer.py	2005-10-14 17:56:52 UTC (rev 39453)
+++ Zope3/branches/fdrake-anguenot_better_xml_support_for_pt/src/zope/pagetemplate/typesniffer.py	2005-10-14 18:20:01 UTC (rev 39454)
@@ -16,6 +16,7 @@
 $Id$
 """
 
+import os.path
 import xml.parsers.expat
 
 XML_PREFIXES = [
@@ -38,13 +39,19 @@
     # Called when an element contains a namespace declaration.
     raise NamespaceFound
 
-def sniff_type(text):
+def sniff_type(text, filename):
     """Return 'text/xml' if text appears to be XML, otherwise return None.
 
+     o if the document has an .xpt explicit extension then it should
+       be processed in XML
      o if the document contains the xml process header <?xml ... ?>
      o if the document contains any namespace declarations
     """
 
+    # Check if the extension of the file is .xpt
+    if os.path.normcase(filename).endswith('.xpt'):
+        return "text/xml"
+
     # Check the xml processing header
     for prefix in XML_PREFIXES:
         if text.startswith(prefix):



More information about the Zope3-Checkins mailing list