Commits

Aleš Erjavec committed 5fdc5b6

SegmentationListContexHandler cleanup.

Comments (0)

Files changed (1)

_textable/widgets/TextableUtils.py

 
 
 class ContextListField(OWContexts.ContextField):
+    """
+    Context field for an item list.
+
+    :param str name: Attribute name in the widget.
+    :param flags int: Context flags (unused).
+    :param str selected:
+        Attribute name of a list of indices corresponding to
+        selected items (default: `None`).
+
+    """
     def __init__(self, name, flags=0, selected=None, **kwargs):
         OWContexts.ContextField.__init__(self, name, flags, **kwargs)
         self.selected = selected
 
 
 class ContextInputListField(OWContexts.ContextField):
+    """
+    Context field for a list of 'Segmentations inputs'.
+
+    This Field matches widget's input list (a list of
+    (Segmentations, inputid) tuples as managed by for instance
+    :func:`updateMultipleInputs`)
+
+    """
+
     def __init__(self, name, flags=0, **kwargs):
         OWContexts.ContextField.__init__(self, name, flags, **kwargs)
 
 
 class ContextInputIndex(OWContexts.ContextField):
+    """
+    Context field for an index into the input Segmentations list.
+    """
     def __init__(self, name, flags=0, **kwargs):
         OWContexts.ContextField.__init__(self, name, flags, **kwargs)
 
 
 class SegmentationListContexHandler(OWContexts.ContextHandler):
+    """
+    Segmentations list context handler.
+
+    This Context handler matches settings on a list of
+    (Segmentations, inputid) tuples as managed by for instance
+    :func:`updateMultipleInputs`.
 
+    """
     def __init__(self, contextName, fields=[], findImperfect=False, **kwargs):
         if findImperfect != False:
             raise ValueError("'findImperfect' is disabled")
                   for field in fields]
 
         self.fields = fields
+
+        # We store the ContextInputListField separately
+        # (should it be passed as a separate argument?)
         self.inputListField = None
         inputListField = [field for field in fields
                           if isinstance(field, ContextInputListField)]
             self.inputListField = inputListField[0]
             self.fields.remove(self.inputListField)
         elif len(inputListField) > 1:
-            raise ValueError("Only one 'ContextInputListField' allowed")
+            raise ValueError("Only one 'ContextInputListField' is allowed")
 
     def findOrCreateContext(self, widget, items):
         encoded = self.encode(items)
         context, isnew = OWContexts.ContextHandler.findOrCreateContext(
             self, widget, encoded)
 
+        # Store the encoded context
         context.encoded = encoded
 
         if isnew:
     def encode(self, segmentationlist):
         """
         Encode a list of input segmentations.
+
+        :param segmentationlist:
+            List of (Segmentations, inputid) tuples.
+
         """
         encoded = []
         for inputid, segmentation in segmentationlist:
         return encoded
 
     def match(self, context, imperfect, encodedlist):
+        """
+        Match the `context` to the encoded segmentations list
+        """
         stored = context.encoded
-        if self.inputListField.name in context.values:
-            stored = context.values[self.inputListField.name]
 
         if len(stored) == len(encodedlist):
             if set(stored) == set(encodedlist):
         OWContexts.ContextHandler.settingsToWidget(self, widget, context)
 
         if self.inputListField and self.inputListField.name in context.values:
-            # find the permutation of the input list so it matches
+            # find the permutation of the current input list so it matches
             # the stored one
             inputs = widget.getdeepattr(self.inputListField.name)
             encoded = self.encode(inputs)
             permutation = self._permutation(
                 encoded, context.values[self.inputListField.name])
             permuted = [inputs[p] for p in permutation]
+
+            # Restore the stored order in the widget.
             setattr(widget, self.inputListField.name, permuted)
 
         for field in self.fields:
                 setattr(widget, field.name, context.values[field.name])
 
     def settingsFromWidget(self, widget, context):
+        """
+        Get the settings from a widget.
+        """
         OWContexts.ContextHandler.settingsFromWidget(self, widget, context)
 
         if self.inputListField:
             inputs = self.encode(
                 widget.getdeepattr(self.inputListField.name))
             context.values[self.inputListField.name] = inputs
-            # Update the stored encoding
-            context.encoded = inputs
 
         for field in self.fields:
             if isinstance(field, ContextListField):