Add os.path.expanduser() wrappers to user-supplied paths.
[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 import os.path
25
26 from ..command import Command, Argument, Failure
27 from ..playlist import FilePlaylist
28 from . import Builtin
29 from .playlist import PlaylistNameArgument, PlaylistAddingCommand
30
31
32 class PlaylistsPlugin (Builtin):
33     def __init__(self):
34         super(PlaylistsPlugin, self).__init__(name='playlists')
35         self._commands = [
36             NextCommand(self), PreviousCommand(self), JumpCommand(self),
37             IndexCommand(self), PlaylistListCommand(self), NewCommand(self)]
38
39
40 # Define commands
41
42 class NextCommand (Command):
43     """Move `hooke.playlists` to the next playlist.
44     """
45     def __init__(self, plugin):
46         super(NextCommand, self).__init__(
47             name='next playlist',
48             help=self.__doc__, plugin=plugin)
49
50     def _run(self, hooke, inqueue, outqueue, params):
51         hooke.playlists.next()
52
53 class PreviousCommand (Command):
54     """Move `hooke.playlists` to the previous playlist.
55     """
56     def __init__(self, plugin):
57         super(PreviousCommand, self).__init__(
58             name='previous playlist',
59             help=self.__doc__, plugin=plugin)
60
61     def _run(self, hooke, inqueue, outqueue, params):
62         hooke.playlists.previous()
63
64 class JumpCommand (Command):
65     """Move `hooke.playlists` to a given playlist.
66     """
67     def __init__(self, plugin):
68         super(JumpCommand, self).__init__(
69             name='jump to playlist',
70             arguments=[
71                 Argument(name='index', type='int', optional=False, help="""
72 Index of target curve.
73 """.strip()),
74                 ],
75             help=self.__doc__, plugin=plugin)
76
77     def _run(self, hooke, inqueue, outqueue, params):
78         hooke.playlists.jump(params['index'])
79
80 class IndexCommand (Command):
81     """Print the index of the current playlist.
82
83     The first playlist has index 0.
84     """
85     def __init__(self, plugin):
86         super(IndexCommand, self).__init__(
87             name='playlist index',
88             help=self.__doc__, plugin=plugin)
89
90     def _run(self, hooke, inqueue, outqueue, params):
91         outqueue.put(hooke.playlists.index())
92
93 class PlaylistListCommand (Command):
94     """Get the playlists in `hooke.playlists`.
95     """
96     def __init__(self, plugin):
97         super(PlaylistListCommand, self).__init__(
98             name='playlists',
99             help=self.__doc__, plugin=plugin)
100
101     def _run(self, hooke, inqueue, outqueue, params):
102         outqueue.put(list(hooke.playlists))
103
104
105 class NewCommand (PlaylistAddingCommand):
106     """Create a new playlist.
107     """
108     def __init__(self, plugin):
109         super(NewCommand, self).__init__(
110             name='new playlist',
111             arguments=[
112                 Argument(name='file', type='file', optional=True,
113                          help="""
114 Default filename for future saves.
115 """.strip()),
116                 ],
117             help=self.__doc__, plugin=plugin)
118
119     def _run(self, hooke, inqueue, outqueue, params):
120         p = FilePlaylist(
121             drivers=hooke.drivers,
122             path=os.path.expanduser(params['file']),
123             )
124         self._set_playlist(hooke, params, p)
125         outqueue.put(p)