Fix a pickling problems with hooke.plugin.playlist.FilterCommand.
[hooke.git] / hooke / plugin / playlist.py
index 01d24b77433af9351af16a2567838b340d463d08..dd6e6261d33ad86052c201a0f051c055eadb8c8f 100644 (file)
@@ -260,8 +260,18 @@ class FilterCommand (Command):
     Removing lots of curves one at a time can be tedious.  With this
     command you can use a function `filter` to select the curves you
     wish to keep.
+
+    Notes
+    -----
+    There are issues with pickling functions bound to class
+    attributes, because the pickle module doesn't know where those
+    functions were originally defined (where it should point the
+    loader).  Because of this, subclasses with hard-coded filter
+    functions are encouraged to define their filter function as a
+    method of their subclass.  See, for example,
+    :meth:`NoteFilterCommand.filter`.
     """
-    def __init__(self, plugin, name='filter playlist', filter_fn=None):
+    def __init__(self, plugin, name='filter playlist'):
         super(FilterCommand, self).__init__(
             name=name,
             arguments=[
@@ -269,20 +279,21 @@ class FilterCommand (Command):
                 PlaylistNameArgument,
                 ],
             help=self.__doc__, plugin=plugin)
-        self.filter_fn = filter_fn
-        if filter_fn == None:
+        if not hasattr(self, 'filter'):
             self.arguments.append(
                 Argument(name='filter', type='function', optional=False,
                          help="""
-Function returning `True` for "good" curves.  `filter(curve) -> True/False`.
+Function returning `True` for "good" curves.
+`filter(curve, hooke, inqueue, outqueue, params) -> True/False`.
 """.strip()))
 
     def _run(self, hooke, inqueue, outqueue, params):
-        if self.filter_fn == None:
+        if not hasattr(self, 'filter'):
             filter_fn = params['filter']
         else:
-            filter_fn = self.filter_fn
-        p = params['playlist'].filter(filter_fn)
+            filter_fn = self.filter
+        p = params['playlist'].filter(filter_fn,
+            hooke=hooke, inqueue=inqueue, outqueue=outqueue, params=params)
         hooke.playlists.add(p)
         outqueue.put(p)
 
@@ -291,6 +302,7 @@ class NoteFilterCommand (FilterCommand):
     """
     def __init__(self, plugin):
         super(NoteFilterCommand, self).__init__(
-            plugin, name='note filter playlist',
-            filter_fn=lambda curve : \
-                'note' in curve.info and curve.info['note'] != None)
+            plugin, name='note filter playlist')
+
+    def filter(self, curve, hooke, inqueue, outqueue, params):
+        return 'note' in curve.info and curve.info['note'] != None