[Zope3-checkins] SVN: Zope3/branches/f12gsprint-widget/src/zope/widget/ incremental checkin.

Gary Poster gary at zope.com
Fri Sep 2 18:12:24 EDT 2005


Log message for revision 38269:
  incremental checkin.
  
  add draft of legacy adapter for zope.app.form.browser input widgets
  
  change initialize value to a marker, so can set to None
  
  
  

Changed:
  U   Zope3/branches/f12gsprint-widget/src/zope/widget/interfaces.py
  A   Zope3/branches/f12gsprint-widget/src/zope/widget/legacy.py
  U   Zope3/branches/f12gsprint-widget/src/zope/widget/widget.py

-=-
Modified: Zope3/branches/f12gsprint-widget/src/zope/widget/interfaces.py
===================================================================
--- Zope3/branches/f12gsprint-widget/src/zope/widget/interfaces.py	2005-09-02 21:35:49 UTC (rev 38268)
+++ Zope3/branches/f12gsprint-widget/src/zope/widget/interfaces.py	2005-09-02 22:12:23 UTC (rev 38269)
@@ -24,16 +24,18 @@
 
 # TODO: dots in names
 
+marker = object()
+
 class ConversionError(ValueError):
     """ Value could not be converted to correct type """
     
 class InvalidStateError(RuntimeError):
     """ This widget's state has been invalidated by a call to setValue()"""
 
-class IBaseWidget(IView):
+class IWidget(IView):
     """Generically describes the behavior of a widget.
 
-    Note that this level must be still presentation independent.
+    This must be presentation independent.
     """
 
     name = Attribute(
@@ -62,14 +64,28 @@
         field can be set to False for widgets that always provide input (e.g.
         a checkbox) to avoid unnecessary 'required' UI notations.
         """)
+                
+    prefix = Attribute("""Element names should begin with the given `prefix`.
+        Prefix name may be None, or a string.  Any other value raises 
+        ValueError.  When prefixes are concatenated with the widget name, a dot 
+        is used as a delimiter; a trailing dot is neither required nor suggested
+        in the prefix itself. """)
+        
+    error = Attribute(""" An exception created by initialize or setValue.  If 
+        this value is not None, it is raised when getValue is called.
+        
+        Minimally, an exception object is expected to be adaptable to
+        a view named snippet.""")
     
-class IWidget(IBaseWidget):
-    """ new interface for widget """
+    message =  Attribute(""" A message object created in initialize or 
+        setValue.  Minimally, a message object is expected to be adaptable to
+        a view named snippet.
+        """)
     
     def __call__():
         """ render widget """
         
-    def initialize(prefix=None, value=None, state=None):
+    def initialize(prefix=None, value=marker, state=None):
         """ Initialize widget and set its value.
 
         Initialize must be called before any other method besides __init__.
@@ -120,24 +136,7 @@
         message attributes to only the message and error pertaining 
         to the new value, if any.
         """
-                
-    prefix = Attribute("""Element names should begin with the given `prefix`.
-        Prefix name may be None, or a string.  Any other value raises 
-        ValueError.  When prefixes are concatenated with the widget name, a dot 
-        is used as a delimiter; a trailing dot is neither required nor suggested
-        in the prefix itself. """)
-        
-    error = Attribute(""" An exception created by initialize or setValue.  If 
-        this value is not None, it is raised when getValue is called.
-        
-        Minimally, an exception object is expected to be adaptable to
-        a view named snippet.""")
     
-    message =  Attribute(""" A message object created in initialize or 
-        setValue.  Minimally, a message object is expected to be adaptable to
-        a view named snippet.
-        """)
-    
 class IInputWidget(IWidget):
     """a widget used for input"""
 

Added: Zope3/branches/f12gsprint-widget/src/zope/widget/legacy.py
===================================================================
--- Zope3/branches/f12gsprint-widget/src/zope/widget/legacy.py	2005-09-02 21:35:49 UTC (rev 38268)
+++ Zope3/branches/f12gsprint-widget/src/zope/widget/legacy.py	2005-09-02 22:12:23 UTC (rev 38269)
@@ -0,0 +1,82 @@
+from zope import interface, component
+from zope.app.form.interfaces import IInputWidget, IDisplayWidget
+from zope.widget import interfaces
+import zope.app.form.interfaces # import IWidget, InputErrors
+from zope.schema.interfaces import ValidationError
+
+class LegacyValidationError(ValidationError):
+    """a validation error that wraps an old style
+    zope.app.form.interfaces.WidgetInputError"""
+    def __init__(self, error):
+        self.error = error
+
+class LegacyConversionError(interfaces.ConversionError):
+    def __init__(self, error):
+        self.error = error
+
+class LegacyInputWidgetAdapter(object):
+    def __init__(self, widget):
+        self._widget = widget
+        context = widget.context
+        self.context = context
+        self.request = widget.request
+        self.label = context.title
+        self.hint = context.description
+        self.required = context.required
+
+    name = property(lambda self: self._widget.name)
+
+    _prefix = None
+    def prefix(self, value):
+        self._prefix = value # no API to get from legacy interface
+        self._widget.setPrefix(value)
+    prefix = property(lambda self: self._prefix, prefix)
+
+    _error = None
+    error = property(lambda self: self._error)
+    
+    message = property(lambda self: None)
+
+    def getState(self):
+        raise NotImplementedError(
+            'Legacy Widget Adapter does not implement getState')
+
+    def hasState(self):
+        if not self._initialized:
+            raise RuntimeError('Initialize widget first')
+        return self._widget.hasInput()
+
+    def setValue(self, value):
+        if not self._initialized:
+            raise RuntimeError('Initialize widget first')
+        self._value = value
+        self._widget.setRenderedValue(value)
+        self._error = None
+    
+    _value = None
+    def getValue(self):
+        if not self._initialized:
+            raise RuntimeError('Initialize widget first')
+        if self.error is not None:
+            raise self.error
+        return self._value
+
+    def initialize(self, prefix=None, value=interfaces.marker, state=None):
+        if state is not None:
+            raise NotImplementedError(
+                'Legacy Widget Adapter does not implement initializing from '
+                'state')
+        self._initialized = True
+        self.prefix = prefix
+        if value is interfaces.marker:
+            try:
+                self._value = self._widget.getInputValue()
+            # let ValidationErrors pass through
+            except ValidationError, e:
+                self._error = e
+            except zope.app.form.interfaces.WidgetInputError, e:
+                self._error = LegacyValidationError(e)
+            except zope.app.form.interfaces.ConversionError, e:
+                self._error = LegacyConversionError(e)
+        else:
+            self.setValue(value)


Property changes on: Zope3/branches/f12gsprint-widget/src/zope/widget/legacy.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: Zope3/branches/f12gsprint-widget/src/zope/widget/widget.py
===================================================================
--- Zope3/branches/f12gsprint-widget/src/zope/widget/widget.py	2005-09-02 21:35:49 UTC (rev 38268)
+++ Zope3/branches/f12gsprint-widget/src/zope/widget/widget.py	2005-09-02 22:12:23 UTC (rev 38269)
@@ -19,13 +19,13 @@
 from zope.schema.interfaces import ValidationError
 from xml.sax.saxutils import quoteattr
 
-import zope.widget.interfaces
+from zope.widget import interfaces
 
 class BaseInputWidget(object):
     """base simple widget"""
     
-    interface.implements(zope.widget.interfaces.IInputWidget)
-    __doc__ = zope.widget.interfaces.IInputWidget.__doc__
+    interface.implements(interfaces.IInputWidget)
+    __doc__ = interfaces.IInputWidget.__doc__
     
     _name = None
     name = property(lambda self: self._name)
@@ -55,16 +55,16 @@
     _valueForced = False
     _initialized = False    
     _state = None
-    def initialize(self, prefix=None, value=None, state=None):
+    def initialize(self, prefix=None, value=interfaces.marker, state=None):
         self._initialized = True
         self.prefix = prefix
         if state is None:
             state = self._calculateStateFromRequest()
         else:
-            if value is not None:
+            if value is not interfaces.marker:
                 raise TypeError('May pass only one of value and state')
         self._state = state
-        if value is None:
+        if value is interfaces.marker:
             if self._state is None:
                 value = self.context.default
             else:
@@ -78,7 +78,7 @@
         if not self._initialized:
             raise RuntimeError('Initialize widget first')
         if self._state is not None and self._valueForced:
-            raise zope.widget.interfaces.InvalidStateError()
+            raise interfaces.InvalidStateError()
         return self._state
     
     def hasState(self):
@@ -156,6 +156,6 @@
         try:
             value = unicode(self._state)
         except ValueError, v: # XXX
-            e = zope.widget.interfaces.ConversionError(v)
+            e = interfaces.ConversionError(v)
         else:
-            return value
\ No newline at end of file
+            return value



More information about the Zope3-Checkins mailing list