Cleanups to playlist name handling in 'load playlist'.
[hooke.git] / hooke / plugin / playlists.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """The ``playlists`` module provides :class:`PlaylistsPlugin` and
20 several associated :class:`hooke.command.Command`\s for handling
21 lists of :class:`hooke.playlist.Playlist` classes.
22 """
23
24 from ..command import Command, Argument, Failure
25 from ..playlist import FilePlaylist
26 from . import Builtin
27 from .playlist import PlaylistNameArgument, PlaylistAddingCommand
28
29
30 class PlaylistsPlugin (Builtin):
31     def __init__(self):
32         super(PlaylistsPlugin, self).__init__(name='playlists')
33         self._commands = [
34             NextCommand(self), PreviousCommand(self), JumpCommand(self),
35             IndexCommand(self), PlaylistListCommand(self), NewCommand(self)]
36
37
38 # Define commands
39
40 class NextCommand (Command):
41     """Move `hooke.playlists` to the next playlist.
42     """
43     def __init__(self, plugin):
44         super(NextCommand, self).__init__(
45             name='next playlist',
46             help=self.__doc__, plugin=plugin)
47
48     def _run(self, hooke, inqueue, outqueue, params):
49         hooke.playlists.next()
50
51 class PreviousCommand (Command):
52     """Move `hooke.playlists` to the previous playlist.
53     """
54     def __init__(self, plugin):
55         super(PreviousCommand, self).__init__(
56             name='previous playlist',
57             help=self.__doc__, plugin=plugin)
58
59     def _run(self, hooke, inqueue, outqueue, params):
60         hooke.playlists.previous()
61
62 class JumpCommand (Command):
63     """Move `hooke.playlists` to a given playlist.
64     """
65     def __init__(self, plugin):
66         super(JumpCommand, self).__init__(
67             name='jump to playlist',
68             arguments=[
69                 Argument(name='index', type='int', optional=False, help="""
70 Index of target curve.
71 """.strip()),
72                 ],
73             help=self.__doc__, plugin=plugin)
74
75     def _run(self, hooke, inqueue, outqueue, params):
76         hooke.playlists.jump(params['index'])
77
78 class IndexCommand (Command):
79     """Print the index of the current playlist.
80
81     The first playlist has index 0.
82     """
83     def __init__(self, plugin):
84         super(IndexCommand, self).__init__(
85             name='playlist index',
86             help=self.__doc__, plugin=plugin)
87
88     def _run(self, hooke, inqueue, outqueue, params):
89         outqueue.put(hooke.playlists.index())
90
91 class PlaylistListCommand (Command):
92     """Get the playlists in `hooke.playlists`.
93     """
94     def __init__(self, plugin):
95         super(PlaylistListCommand, self).__init__(
96             name='playlists',
97             help=self.__doc__, plugin=plugin)
98
99     def _run(self, hooke, inqueue, outqueue, params):
100         outqueue.put(list(hooke.playlists))
101
102
103 class NewCommand (PlaylistAddingCommand):
104     """Create a new playlist.
105     """
106     def __init__(self, plugin):
107         super(NewCommand, self).__init__(
108             name='new playlist',
109             arguments=[
110                 Argument(name='file', type='file', optional=True,
111                          help="""
112 Default filename for future saves.
113 """.strip()),
114                 ],
115             help=self.__doc__, plugin=plugin)
116
117     def _run(self, hooke, inqueue, outqueue, params):
118         p = FilePlaylist(
119             drivers=hooke.drivers,
120             path=params['file'],
121             )
122         playlist_names = [playlist.name for playlist in hooke.playlists]
123         if p.name in playlist_names:
124             p.name = params['output playlist']  # HACK: override input name.  How to tell if it is callback-generated?
125         hooke.playlists.append(p)