Add hooke.playlist.NoteIndexList and refactor exception handling in commandline.
[hooke.git] / hooke / plugin / cut.py
index dc55d89161a719e27bb81d8076d2186c2a4420bd..68dd2a0223b2a1f55f4ef466adfaa4c331b11aa9 100644 (file)
@@ -1,7 +1,7 @@
 """Defines :class:`CutPlugin` and :class:`CutCommand`.
 """
 
-from ..command import Command, Argument
+from ..command import Command, Argument, Failure
 from ..plugin import Plugin
 
 
@@ -12,6 +12,27 @@ class CutPlugin (Plugin):
     def commands(self):
         return [CutCommand()]
 
+
+# Define common or complicated arguments
+
+def current_curve_callback(hooke, command, argument, value):
+    playlist = hooke.playlists.current()
+    if playlist == None:
+        raise Failure('No playlists loaded')
+    curve = playlist.current()
+    if curve == None:
+        raise Failure('No curves in playlist %s' % playlist.name)
+    return curve
+
+CurveArgument = Argument(
+    name='curve', type='curve', callback=current_curve_callback,
+    help="""
+:class:`hooke.curve.Curve` to cut from.  Defaults to the current curve.
+""".strip())
+
+
+# Define commands
+
 class CutCommand (Command):
     """Cut the selected signal between two points and write it to a file.
 
@@ -22,9 +43,7 @@ class CutCommand (Command):
         super(CutCommand, self).__init__(
             name='cut',
             arguments=[
-                Argument(name='curve', type='curve', help="""
-:class:`hooke.curve.Curve` to cut from.  Defaults to the current curve.
-""".strip()),
+                CurveArgument,
                 Argument(name='block', aliases=['set'], type='int', default=0,
                     help="""
 Data block to save.  For an approach/retract force curve, `0` selects