Use input name in 'new playlist' and 'load playlist' if p.name == None.
[hooke.git] / hooke / plugin / playlists.py
index 485904fcd4c911b571e4eb7463c7a274c797d661..ccd5020ab09b8698a194d4423aac851730e77525 100644 (file)
@@ -2,15 +2,15 @@
 #
 # This file is part of Hooke.
 #
-# Hooke is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
+# Hooke is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
 #
-# Hooke is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Lesser General Public License for more details.
+# Hooke is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
 #
 # You should have received a copy of the GNU Lesser General Public
 # License along with Hooke.  If not, see
@@ -22,7 +22,9 @@ lists of :class:`hooke.playlist.Playlist` classes.
 """
 
 from ..command import Command, Argument, Failure
-from ..plugin import Builtin
+from ..playlist import FilePlaylist
+from . import Builtin
+from .playlist import PlaylistNameArgument, PlaylistAddingCommand
 
 
 class PlaylistsPlugin (Builtin):
@@ -30,7 +32,7 @@ class PlaylistsPlugin (Builtin):
         super(PlaylistsPlugin, self).__init__(name='playlists')
         self._commands = [
             NextCommand(self), PreviousCommand(self), JumpCommand(self),
-            IndexCommand(self), PlaylistListCommand(self)]
+            IndexCommand(self), PlaylistListCommand(self), NewCommand(self)]
 
 
 # Define commands
@@ -71,7 +73,7 @@ Index of target curve.
             help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
-       hooke.playlists.jump(int(params['index'])) # HACK, int() should be handled by ui
+       hooke.playlists.jump(params['index'])
 
 class IndexCommand (Command):
     """Print the index of the current playlist.
@@ -84,7 +86,7 @@ class IndexCommand (Command):
             help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
-       outqueue.put(hooke.playlists._index)
+       outqueue.put(hooke.playlists.index())
 
 class PlaylistListCommand (Command):
     """Get the playlists in `hooke.playlists`.
@@ -96,3 +98,28 @@ class PlaylistListCommand (Command):
 
     def _run(self, hooke, inqueue, outqueue, params):
        outqueue.put(list(hooke.playlists))
+
+
+class NewCommand (PlaylistAddingCommand):
+    """Create a new playlist.
+    """
+    def __init__(self, plugin):
+        super(NewCommand, self).__init__(
+            name='new playlist',
+            arguments=[
+                Argument(name='file', type='file', optional=True,
+                         help="""
+Default filename for future saves.
+""".strip()),
+                ],
+            help=self.__doc__, plugin=plugin)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+        p = FilePlaylist(
+            drivers=hooke.drivers,
+            path=params['file'],
+            )
+        playlist_names = [playlist.name for playlist in hooke.playlists]
+        if p.name in playlist_names or p.name == None:
+            p.name = params['output playlist']  # HACK: override input name.  How to tell if it is callback-generated?
+        hooke.playlists.append(p)