[Checkins] SVN: z3c.form/trunk/ merge of branch z3c.form/branches/adamg-z3c-pt-optional

Adam Groszer agroszer at gmail.com
Sat Jun 13 11:50:48 EDT 2009


Log message for revision 100919:
  merge of branch z3c.form/branches/adamg-z3c-pt-optional
  which should make z3c.pt dependency optional
  and make z3c.form compatible with KGS 3.4
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/setup.py
  U   z3c.form/trunk/src/z3c/form/__init__.py
  U   z3c.form/trunk/src/z3c/form/adding.txt
  U   z3c.form/trunk/src/z3c/form/browser/README.txt
  U   z3c.form/trunk/src/z3c/form/browser/object.txt
  U   z3c.form/trunk/src/z3c/form/browser/objectmulti.txt
  U   z3c.form/trunk/src/z3c/form/browser/tests.py
  A   z3c.form/trunk/src/z3c/form/compatibility.py
  U   z3c.form/trunk/src/z3c/form/error.py
  U   z3c.form/trunk/src/z3c/form/error.txt
  U   z3c.form/trunk/src/z3c/form/form.py
  U   z3c.form/trunk/src/z3c/form/form.txt
  U   z3c.form/trunk/src/z3c/form/group.txt
  U   z3c.form/trunk/src/z3c/form/object.py
  A   z3c.form/trunk/src/z3c/form/ptcompat.py
  U   z3c.form/trunk/src/z3c/form/subform.txt
  U   z3c.form/trunk/src/z3c/form/testing.py
  U   z3c.form/trunk/src/z3c/form/tests/test_doc.py
  U   z3c.form/trunk/src/z3c/form/widget.py
  U   z3c.form/trunk/src/z3c/form/zcml.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/CHANGES.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -5,10 +5,16 @@
 Version 2.0.0 (unreleased)
 --------------------------
 
-Features
-~~~~~~~~
+- KGS 3.4 compatibility. This is a real hard thing, because z3c.form tests
+  use lxml >= 2.1.1 to check test output, but KGS 3.4 has lxml 1.3.6.
+  Therefore we agree on that if tests pass with all package versions nailed by
+  KGS 3.4 but lxml overridden to 2.1.1 then the z3c.form package works with a
+  plain KGS 3.4.
 
-- Added nested group support. Groups are rendered as fieldsets.
+- Feature: Removed hard z3c.ptcompat and thus z3c.pt dependency.
+  If you have z3c.ptcompat on the pythonpath it will be used.
+
+- Feature: Added nested group support. Groups are rendered as fieldsets.
   Nested fieldsets are very useful when designing forms.
 
 - Added `labelRequired` and `requiredInfo` form attributes. This is

Modified: z3c.form/trunk/setup.py
===================================================================
--- z3c.form/trunk/setup.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/setup.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -75,15 +75,19 @@
     package_dir = {'':'src'},
     namespace_packages = ['z3c'],
     extras_require = dict(
+        extra = [
+            'z3c.pt >= 1.0b4',
+            'z3c.ptcompat',
+        ],
         test = [
             'zope.app.testing',
             'zope.app.container',
+            'zope.container',
             'zope.app.pagetemplate',
             'zope.app.i18n',
             'zope.testing',
             'z3c.coverage',
             'z3c.template',
-            'z3c.pt >= 1.0b4',
             'zc.sourcefactory',
             'lxml >= 2.1.1',
             ],
@@ -107,7 +111,6 @@
         'zope.security',
         'zope.site',
         'zope.traversing',
-        'z3c.ptcompat',
         ],
     zip_safe = False,
     )

Modified: z3c.form/trunk/src/z3c/form/__init__.py
===================================================================
--- z3c.form/trunk/src/z3c/form/__init__.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/__init__.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -1 +1,6 @@
 # Make a package.
+
+# Apply compatibility layer
+from z3c.form import compatibility
+compatibility.apply()
+del compatibility
\ No newline at end of file

Modified: z3c.form/trunk/src/z3c/form/adding.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/adding.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/adding.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -35,7 +35,7 @@
 
 Next we need a container to which we wish to add the person:
 
-  >>> from zope.container import btree
+  >>> from zope.app.container import btree
   >>> people = btree.BTreeContainer()
 
 When creating and adding a new object using the ``IAdding`` API, the container
@@ -52,7 +52,7 @@
 for it now:
 
   >>> import os
-  >>> from z3c import ptcompat as viewpagetemplatefile
+  >>> from z3c.form import ptcompat as viewpagetemplatefile
   >>> from z3c.form import tests, field
   >>> from z3c.form.adding import AddForm
 

Modified: z3c.form/trunk/src/z3c/form/browser/README.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/README.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/browser/README.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -15,10 +15,12 @@
 
   >>> from zope.configuration import xmlconfig
   >>> import zope.component
+  >>> import zope.app.component
   >>> import zope.app.security
   >>> import zope.i18n
   >>> import z3c.form
   >>> xmlconfig.XMLConfig('meta.zcml', zope.component)()
+  >>> xmlconfig.XMLConfig('meta.zcml', zope.app.component)()
   >>> xmlconfig.XMLConfig('meta.zcml', zope.app.security)()
   >>> xmlconfig.XMLConfig('meta.zcml', zope.i18n)()
   >>> xmlconfig.XMLConfig('meta.zcml', z3c.form)()

Modified: z3c.form/trunk/src/z3c/form/browser/object.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/object.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/browser/object.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -562,12 +562,12 @@
   4
 
   >>> printEvents()
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObject>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.container.contained.ContainerModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...contained.ContainerModifiedEvent object at ...>
 
 
   >>> eventlog=[]
@@ -662,10 +662,10 @@
   2
 
   >>> printEvents()
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObject>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMyObject>
   ('subobject',)
 
@@ -1146,10 +1146,10 @@
   >>> len(eventlog)
   2
   >>> printEvents()
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObject>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMyObject>
   ('subobject', 'name')
 

Modified: z3c.form/trunk/src/z3c/form/browser/objectmulti.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/objectmulti.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/browser/objectmulti.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -1276,16 +1276,16 @@
 ((why is IMySubObjectMulti created twice???))
 
   >>> printEvents()
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObjectMulti>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObjectMulti>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.container.contained.ContainerModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...contained.ContainerModifiedEvent object at ...>
 
 
   >>> eventlog=[]
@@ -1415,19 +1415,19 @@
 ((TODO: now this is real crap here, why is IMySubObjectMulti created 3 times???))
 
   >>> printEvents()
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObjectMulti>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObjectMulti>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMySubObjectMulti>
   ('foofield', 'barfield')
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
   <InterfaceClass z3c.form.testing.IMyMultiObject>
   ('listOfObject',)
 
@@ -1578,8 +1578,8 @@
 ((TODO: bummer... who creates those 2 objects???))
 
   >>> printEvents()
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
-  <zope.lifecycleevent.ObjectCreatedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
+  <zope...ObjectCreatedEvent object at ...>
 
 
 Watch for the error message in the HTML:

Modified: z3c.form/trunk/src/z3c/form/browser/tests.py
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/tests.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/browser/tests.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -23,10 +23,16 @@
 from zope.testing.doctestunit import DocFileSuite
 
 from z3c.form import testing
+from z3c.form.ptcompat import AVAILABLE
 
 def test_suite():
     checker = testing.OutputChecker(doctest)
 
+    if AVAILABLE:
+        setups = (testing.setUpZPT, testing.setUpZ3CPT)
+    else:
+        setups = (testing.setUpZPT,)
+
     tests = ((
         DocFileSuite('README.txt',
                      setUp=setUp, tearDown=testing.tearDown,
@@ -118,6 +124,6 @@
                      optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
                      checker=checker,
                      ))
-             for setUp in (testing.setUpZPT, testing.setUpZ3CPT))
+             for setUp in setups)
 
     return unittest.TestSuite(itertools.chain(*tests))

Copied: z3c.form/trunk/src/z3c/form/compatibility.py (from rev 100915, z3c.form/branches/adamg-z3c-pt-optional/src/z3c/form/compatibility.py)
===================================================================
--- z3c.form/trunk/src/z3c/form/compatibility.py	                        (rev 0)
+++ z3c.form/trunk/src/z3c/form/compatibility.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -0,0 +1,37 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Form and Widget Framework Interfaces
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import datetime
+import decimal
+import zope.interface
+
+def addHooks():
+    try:
+        from zope.site import hooks
+        return
+    except AttributeError:
+        #this is a crappy situation
+        import zope.location.interfaces
+        import zope.traversing.interfaces
+        zope.location.interfaces.IRoot = zope.traversing.interfaces.IContainmentRoot
+        import zope.site
+        import zope.app.component.hooks
+        zope.site.hooks = zope.app.component.hooks
+
+def apply():
+    addHooks()

Modified: z3c.form/trunk/src/z3c/form/error.py
===================================================================
--- z3c.form/trunk/src/z3c/form/error.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/error.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -20,11 +20,11 @@
 import zope.component
 import zope.interface
 import zope.schema
-from z3c import ptcompat
 from zope.pagetemplate.interfaces import IPageTemplate
 
 import z3c.form
 from z3c.form import interfaces, util, value
+from z3c.form import ptcompat
 from z3c.form.i18n import MessageFactory as _
 
 ErrorViewMessage = value.StaticValueCreator(
@@ -127,7 +127,8 @@
     template = None
 
     def __init__(self, filename, contentType='text/html'):
-        self.template = ptcompat.ViewPageTemplateFile(filename, content_type=contentType)
+        self.template = ptcompat.ViewPageTemplateFile(
+            filename, content_type=contentType)
 
     def __call__(self, errorView, request):
         return self.template

Modified: z3c.form/trunk/src/z3c/form/error.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/error.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/error.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -99,7 +99,7 @@
 needs, sometimes one wishes to register a custom view to have more complex
 views. In this example we wish to register a custom error message:
 
-  >>> from z3c import ptcompat as viewpagetemplatefile
+  >>> from z3c.form import ptcompat as viewpagetemplatefile
   >>> from z3c.form import tests
 
   >>> class NegativeAgeView(error.ErrorViewSnippet):

Modified: z3c.form/trunk/src/z3c/form/form.py
===================================================================
--- z3c.form/trunk/src/z3c/form/form.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/form.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -21,13 +21,13 @@
 import zope.component
 import zope.event
 import zope.lifecycleevent
-from z3c import ptcompat
 from zope.publisher import browser
 from zope.pagetemplate.interfaces import IPageTemplate
 from zope.schema.fieldproperty import FieldProperty
 
 from z3c.form import button, field, interfaces, util
 from z3c.form.i18n import MessageFactory as _
+from z3c.form import ptcompat
 
 
 def applyChanges(form, content, data):

Modified: z3c.form/trunk/src/z3c/form/form.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/form.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/form.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -289,7 +289,7 @@
 very simple template as part of this example:
 
   >>> import os
-  >>> from z3c import ptcompat as viewpagetemplatefile
+  >>> from z3c.form import ptcompat as viewpagetemplatefile
   >>> from z3c.form import tests
   >>> def addTemplate(form):
   ...     form.template = viewpagetemplatefile.bind_template(
@@ -563,7 +563,7 @@
   >>> addForm.name
   'form'
 
-The template can then use those attributes, if it likes to. 
+The template can then use those attributes, if it likes to.
 
 In the examples previously we set the template manually. If no
 template is specified, the system tries to find an adapter. Without
@@ -648,8 +648,8 @@
     <input class="text-widget required textline-field"
            id="form-widgets-name" name="form.widgets.name" type="text" value="">
   </div>
-  
 
+
 Adding a "Cancel" button
 ------------------------
 
@@ -918,7 +918,7 @@
 
   >>> event = eventlog[-1]
   >>> event
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
 
   >>> attrs = event.descriptions[0]
   >>> attrs.interface
@@ -1476,7 +1476,7 @@
   ...         self.refreshActions = True
 
 First, let's illustrate simple cases, when no button is pressed.
-The button will be available when context is not empty. 
+The button will be available when context is not empty.
 
   >>> context = [1, 2, 3, 4]
   >>> request = TestRequest()

Modified: z3c.form/trunk/src/z3c/form/group.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/group.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/group.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -60,7 +60,7 @@
 one of the base classes. The groups are specified in a simple tuple:
 
   >>> import os
-  >>> from z3c import ptcompat as viewpagetemplatefile
+  >>> from z3c.form.ptcompat import ViewPageTemplateFile
   >>> from z3c.form import form, tests
 
   >>> class RegistrationAddForm(group.GroupForm, form.AddForm):
@@ -68,7 +68,7 @@
   ...         'firstName', 'lastName')
   ...     groups = (LicenseGroup, CarGroup)
   ...
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_groupedit.pt', os.path.dirname(tests.__file__))
   ...
   ...     def create(self, data):
@@ -281,7 +281,7 @@
   ...         'firstName', 'lastName')
   ...     groups = (LicenseGroup, CarGroup)
   ...
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_groupedit.pt', os.path.dirname(tests.__file__))
 
   >>> request = testing.TestRequest()
@@ -445,7 +445,7 @@
 
   >>> event = eventlog[-1]
   >>> event
-  <zope.lifecycleevent.ObjectModifiedEvent object at ...>
+  <zope...ObjectModifiedEvent object at ...>
 
 The event's description contains the changed Interface and the names of
 all changed fields, even if they where in different groups:
@@ -467,7 +467,7 @@
   ...     fields = field.Fields(IVehicleRegistration).select(
   ...         'firstName', 'lastName')
   ...
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_groupedit.pt', os.path.dirname(tests.__file__))
 
   >>> request = testing.TestRequest()
@@ -606,7 +606,7 @@
   ...         'owner')
   ...     groups = (OwnerGroup,)
   ...
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_groupedit.pt', os.path.dirname(tests.__file__))
 
   >>> reg = VehicleRegistration(
@@ -746,13 +746,13 @@
   ...         'owner')
   ...     groups = (OwnerGroup,)
   ...
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_groupedit.pt', os.path.dirname(tests.__file__))
 
   >>> class RegistrationEditForm(group.GroupForm, form.EditForm):
   ...     groups = (VehicleRegistrationGroup,)
   ...
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_nested_groupedit.pt', os.path.dirname(tests.__file__))
 
   >>> reg = VehicleRegistration(

Modified: z3c.form/trunk/src/z3c/form/object.py
===================================================================
--- z3c.form/trunk/src/z3c/form/object.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/object.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -24,12 +24,12 @@
 from zope.security.proxy import removeSecurityProxy
 from zope.pagetemplate.interfaces import IPageTemplate
 
-from z3c import ptcompat as viewpagetemplatefile
 from z3c.form.converter import BaseDataConverter
 
 from z3c.form import form, interfaces, util, widget
 from z3c.form.field import Fields
 from z3c.form.error import MultipleErrors
+from z3c.form.ptcompat import ViewPageTemplateFile
 
 def getIfName(iface):
     return iface.__module__+'.'+iface.__name__
@@ -316,7 +316,7 @@
     def __init__(self, filename, contentType='text/html',
                  context=None, request=None, view=None,
                  field=None, widget=None, schema=None):
-        self.template = viewpagetemplatefile.ViewPageTemplateFile(
+        self.template = ViewPageTemplateFile(
             filename, content_type=contentType)
         zope.component.adapter(
             util.getSpecification(context),

Copied: z3c.form/trunk/src/z3c/form/ptcompat.py (from rev 100915, z3c.form/branches/adamg-z3c-pt-optional/src/z3c/form/ptcompat.py)
===================================================================
--- z3c.form/trunk/src/z3c/form/ptcompat.py	                        (rev 0)
+++ z3c.form/trunk/src/z3c/form/ptcompat.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -0,0 +1,30 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""z3c.pt optional compatibility
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+#import utilities from z3c.ptcompat when available and set AVAILABLE flag
+
+try:
+    from z3c.ptcompat import ViewPageTemplateFile
+    from z3c.ptcompat import bind_template
+    AVAILABLE = True
+except ImportError:
+    AVAILABLE = False
+    from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+    from zope.app.pagetemplate.viewpagetemplatefile import BoundPageTemplate \
+         as bind_template

Modified: z3c.form/trunk/src/z3c/form/subform.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/subform.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/subform.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -68,13 +68,13 @@
 is the template, which should not render a form-tag:
 
   >>> import os
-  >>> from z3c import ptcompat as viewpagetemplatefile
+  >>> from z3c.form.ptcompat import ViewPageTemplateFile
   >>> from z3c.form import form, field, tests
 
   >>> templatePath = os.path.dirname(tests.__file__)
 
   >>> class OwnerForm(form.EditForm):
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_owneredit.pt', templatePath)
   ...     fields = field.Fields(IOwner)
   ...     prefix = 'owner'
@@ -87,7 +87,7 @@
 
   >>> class CarForm(form.EditForm):
   ...     fields = field.Fields(ICar).select('model', 'make')
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_caredit.pt', templatePath)
   ...     prefix = 'car'
   ...     def update(self):
@@ -267,7 +267,7 @@
   >>> from z3c.form import subform
 
   >>> class OwnerForm(subform.EditSubForm):
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_subedit.pt', templatePath)
   ...     fields = field.Fields(IOwner)
   ...     prefix = 'owner'
@@ -277,7 +277,7 @@
 
   >>> class CarForm(form.EditForm):
   ...     fields = field.Fields(ICar).select('model', 'make')
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_caredit.pt', templatePath)
   ...     prefix = 'car'
   ...
@@ -467,7 +467,7 @@
 
   >>> print carForm.render() # doctest: +NOPARSE_MARKUP
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-  <html xmlns="http://www.w3.org/1999/xhtml">       
+  <html xmlns="http://www.w3.org/1999/xhtml">
       ...
       <fieldset>
         <legend>Owner</legend>
@@ -482,7 +482,7 @@
      </fieldset>
      ...
   </html>
-  
+
 If the data did not change, it is also locally reported:
 
   >>> request = TestRequest(form={
@@ -525,7 +525,7 @@
 
   >>> from z3c.form.interfaces import IWidgets
   >>> class OwnerAddForm(form.EditForm):
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_owneredit.pt', templatePath)
   ...     fields = field.Fields(IOwner)
   ...     prefix = 'owner'
@@ -540,7 +540,7 @@
 
   >>> class CarAddForm(form.EditForm):
   ...     fields = field.Fields(ICar).select('model', 'make')
-  ...     template = viewpagetemplatefile.ViewPageTemplateFile(
+  ...     template = ViewPageTemplateFile(
   ...         'simple_caredit.pt', templatePath)
   ...     prefix = 'car'
   ...

Modified: z3c.form/trunk/src/z3c/form/testing.py
===================================================================
--- z3c.form/trunk/src/z3c/form/testing.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/testing.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -37,10 +37,10 @@
 from z3c.form.browser import radio, select, text, textarea
 from z3c.form.browser import file as fileWidget
 
-from z3c.ptcompat.testing import render
-from z3c.ptcompat.testing import OutputChecker
+from z3c.form.ptcompat import AVAILABLE
 
-import z3c.ptcompat
+#from z3c.ptcompat.testing import render
+#from z3c.ptcompat.testing import OutputChecker
 
 import lxml.html
 import lxml.doctestcompare
@@ -189,10 +189,13 @@
     test.globs = {'root': setup.placefulSetUp(True)}
 
 def setUpZPT(suite):
-    z3c.ptcompat.config.disable()
+    if AVAILABLE:
+        import z3c.ptcompat
+        z3c.ptcompat.config.disable()
     setUp(suite)
 
 def setUpZ3CPT(suite):
+    import z3c.ptcompat
     z3c.ptcompat.config.enable()
     setUp(suite)
     zope.configuration.xmlconfig.XMLConfig('configure.zcml', z3c.pt)()
@@ -354,3 +357,96 @@
 
 def tearDown(test):
     setup.placefulTearDown()
+
+
+##########################
+import doctest as pythondoctest
+
+import lxml.etree
+import lxml.doctestcompare
+
+import re
+
+class OutputChecker(lxml.doctestcompare.LHTMLOutputChecker):
+    """Doctest output checker which is better equippied to identify
+    HTML markup than the checker from the ``lxml.doctestcompare``
+    module. It also uses the text comparison function from the
+    built-in ``doctest`` module to allow the use of ellipsis."""
+
+    _repr_re = re.compile(r'^<([A-Z]|[^>]+ (at|object) |[a-z]+ \'[A-Za-z0-9_.]+\'>)')
+
+    def __init__(self, doctest=pythondoctest):
+        self.doctest = doctest
+
+        # make sure these optionflags are registered
+        doctest.register_optionflag('PARSE_HTML')
+        doctest.register_optionflag('PARSE_XML')
+        doctest.register_optionflag('NOPARSE_MARKUP')
+
+    def _looks_like_markup(self, s):
+        s = s.replace('<BLANKLINE>', '\n').strip()
+        return (s.startswith('<')
+                and not self._repr_re.search(s))
+
+    def text_compare(self, want, got, strip):
+        if want is None: want = ""
+        if got is None: got = ""
+        checker = self.doctest.OutputChecker()
+        return checker.check_output(
+            want, got, self.doctest.ELLIPSIS|self.doctest.NORMALIZE_WHITESPACE)
+
+    def get_parser(self, want, got, optionflags):
+        NOPARSE_MARKUP = self.doctest.OPTIONFLAGS_BY_NAME.get(
+            "NOPARSE_MARKUP", 0)
+        PARSE_HTML = self.doctest.OPTIONFLAGS_BY_NAME.get(
+            "PARSE_HTML", 0)
+        PARSE_XML = self.doctest.OPTIONFLAGS_BY_NAME.get(
+            "PARSE_XML", 0)
+
+        parser = None
+        if NOPARSE_MARKUP & optionflags:
+            return None
+        if PARSE_HTML & optionflags:
+            parser = lxml.doctestcompare.html_fromstring
+        elif PARSE_XML & optionflags:
+            parser = lxml.etree.XML
+        elif (want.strip().lower().startswith('<html')
+              and got.strip().startswith('<html')):
+            parser = lxml.doctestcompare.html_fromstring
+        elif (self._looks_like_markup(want)
+              and self._looks_like_markup(got)):
+            parser = self.get_default_parser()
+        return parser
+
+def render(view, xpath='.'):
+    method = getattr(view, 'render', None)
+    if method is None:
+        method = view.__call__
+
+    string = method()
+    if string == "":
+        return string
+
+    try:
+        root = lxml.etree.fromstring(string)
+    except lxml.etree.XMLSyntaxError:
+        root = lxml.html.fromstring(string)
+
+    output = ""
+    for node in root.xpath(
+        xpath, namespaces={'xmlns': 'http://www.w3.org/1999/xhtml'}):
+        s = lxml.etree.tounicode(node, pretty_print=True)
+        s = s.replace(' xmlns="http://www.w3.org/1999/xhtml"', ' ')
+        output += s
+
+    if not output:
+        raise ValueError("No elements matched by %s." % repr(xpath))
+
+    # let's get rid of blank lines
+    output = output.replace('\n\n', '\n')
+
+    # self-closing tags are more readable with a space before the
+    # end-of-tag marker
+    output = output.replace('"/>', '" />')
+
+    return output
\ No newline at end of file

Modified: z3c.form/trunk/src/z3c/form/tests/test_doc.py
===================================================================
--- z3c.form/trunk/src/z3c/form/tests/test_doc.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/tests/test_doc.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -24,10 +24,16 @@
 from zope.app.testing import placelesssetup
 
 from z3c.form import testing
+from z3c.form.ptcompat import AVAILABLE
 
 def test_suite():
     checker = testing.OutputChecker(doctest)
 
+    if AVAILABLE:
+        setups = (testing.setUpZPT, testing.setUpZ3CPT)
+    else:
+        setups = (testing.setUpZPT,)
+
     tests = ((
         doctest.DocFileSuite(
             '../action.txt',
@@ -144,6 +150,6 @@
             optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
             checker=checker,
             ))
-        for setUp in (testing.setUpZPT, testing.setUpZ3CPT))
+        for setUp in setups)
 
     return unittest.TestSuite(itertools.chain(*tests))

Modified: z3c.form/trunk/src/z3c/form/widget.py
===================================================================
--- z3c.form/trunk/src/z3c/form/widget.py	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/widget.py	2009-06-13 15:50:48 UTC (rev 100919)
@@ -22,11 +22,11 @@
 import zope.location
 import zope.schema.interfaces
 from zope.pagetemplate.interfaces import IPageTemplate
-from z3c import ptcompat as viewpagetemplatefile
 from zope.i18n import translate
 from zope.schema.fieldproperty import FieldProperty
 
 from z3c.form import interfaces, util, value
+from z3c.form import ptcompat as viewpagetemplatefile
 
 PLACEHOLDER = object()
 

Modified: z3c.form/trunk/src/z3c/form/zcml.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/zcml.txt	2009-06-13 15:28:03 UTC (rev 100918)
+++ z3c.form/trunk/src/z3c/form/zcml.txt	2009-06-13 15:50:48 UTC (rev 100919)
@@ -74,8 +74,8 @@
 
 and check it:
 
-  >>> from z3c import ptcompat
-  >>> isinstance(template, ptcompat.ViewPageTemplateFile)
+  >>> from z3c.form.ptcompat import ViewPageTemplateFile
+  >>> isinstance(template, ViewPageTemplateFile)
   True
 
 Let's use the template within the widget.
@@ -178,7 +178,7 @@
 
 and check it:
 
-  >>> isinstance(template, ptcompat.ViewPageTemplateFile)
+  >>> isinstance(template, ViewPageTemplateFile)
   True
 
 Let's use the template within the widget.



More information about the Checkins mailing list