1 """The `playlist` module provides :class:`PlaylistPlugin` several
2 associated :class:`hooke.command.Command`\s for handling
3 :mod:`hooke.playlist` classes.
8 from ..command import Command, Argument, Failure
9 from ..playlist import FilePlaylist
10 from ..plugin import Builtin
13 class PlaylistPlugin (Builtin):
15 super(PlaylistPlugin, self).__init__(name='playlist')
18 return [NextCommand(), PreviousCommand(), JumpCommand(),
19 SaveCommand(), LoadCommand(),
20 AddCommand(), AddGlobCommand(),
21 RemoveCommand(), FilterCommand(), NoteFilterCommand()]
24 # Define common or complicated arguments
26 def current_playlist_callback(hooke, command, argument, value):
29 playlist = hooke.playlists.current()
31 raise Failure('No playlists loaded')
34 PlaylistArgument = Argument(
35 name='playlist', type='playlist', callback=current_playlist_callback,
37 :class:`hooke.plugin.playlist.Playlist` to act on. Defaults to the
41 def playlist_name_callback(hooke, command, argument, value):
42 return hooke.playlists.free_name()
44 PlaylistNameArgument = Argument(
45 name='name', type='string', optional=True, callback=playlist_name_callback,
47 Name of the new playlist (defaults to an auto-generated name).
53 class NextCommand (Command):
54 """Move playlist to the next curve.
57 super(NextCommand, self).__init__(
59 arguments=[PlaylistArgument],
62 def _run(self, hooke, inqueue, outqueue, params):
63 params['playlist'].next()
65 class PreviousCommand (Command):
66 """Move playlist to the previous curve.
69 super(PreviousCommand, self).__init__(
70 name='previous curve',
71 arguments=[PlaylistArgument],
74 def _run(self, hooke, inqueue, outqueue, params):
75 params['playlist'].previous()
77 class JumpCommand (Command):
78 """Move playlist to a given curve.
81 super(JumpCommand, self).__init__(
85 Argument(name='index', type='int', optional=False, help="""
86 Index of target curve.
91 def _run(self, hooke, inqueue, outqueue, params):
92 params['playlist'].jump(params['index'])
94 class IndexCommand (Command):
95 """Print the index of the current curve.
97 The first curve has index 0.
100 super(IndexCommand, self).__init__(
107 def _run(self, hooke, inqueue, outqueue, params):
108 outqueue.put(params['playlist']._index)
110 class SaveCommand (Command):
114 super(SaveCommand, self).__init__(
115 name='save playlist',
118 Argument(name='output', type='file',
120 File name for the output playlist. Defaults to overwriting the input
126 def _run(self, hooke, inqueue, outqueue, params):
127 params['playlist'].save(params['output'])
129 class LoadCommand (Command):
133 super(LoadCommand, self).__init__(
134 name='load playlist',
136 Argument(name='input', type='file', optional=False,
138 File name for the input playlist.
140 Argument(name='drivers', type='driver', optional=False,
143 Drivers for loading curves.
148 def _run(self, hooke, inqueue, outqueue, params):
149 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
151 hooke.playlists.append(p)
154 class AddCommand (Command):
155 """Add a curve to a playlist.
158 super(AddCommand, self).__init__(
159 name='add curve to playlist',
162 Argument(name='input', type='file', optional=False,
164 File name for the input :class:`hooke.curve.Curve`.
166 Argument(name='info', type='dict', optional=True,
168 Additional information for the input :class:`hooke.curve.Curve`.
173 def _run(self, hooke, inqueue, outqueue, params):
174 params['playlist'].append_curve_by_path(params['input'],
177 class AddGlobCommand (Command):
178 """Add curves to a playlist with file globbing.
180 Adding lots of files one at a time can be tedious. With this
181 command you can use globs (`data/curves/*.dat`) to add curves
182 for all matching files at once.
185 super(AddGlobCommand, self).__init__(
186 name='glob curves to playlist',
189 Argument(name='input', type='glob', optional=False,
191 File name glob for the input :class:`hooke.curve.Curve`.
193 Argument(name='info', type='dict', optional=True,
195 Additional information for the input :class:`hooke.curve.Curve`.
200 def _run(self, hooke, inqueue, outqueue, params):
201 for path in sorted(glob.glob(params['input'])):
202 params['playlist'].append_curve_by_path(path, params['info'])
204 class RemoveCommand (Command):
205 """Remove a curve from a playlist.
208 super(RemoveCommand, self).__init__(
209 name='remove curve from playlist',
212 Argument(name='index', type='int', optional=False, help="""
213 Index of target curve.
218 def _run(self, hooke, inqueue, outqueue, params):
219 params['playlist'].pop(params['index'])
220 params['playlist'].jump(params._index)
222 class FilterCommand (Command):
223 """Create a subset playlist via a selection function.
225 Removing lots of curves one at a time can be tedious. With this
226 command you can use a function `filter` to select the curves you
230 super(FilterCommand, self).__init__(
231 name='filter playlist',
234 PlaylistNameArgument,
235 Argument(name='filter', type='function', optional=False,
237 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
242 def _run(self, hooke, inqueue, outqueue, params):
243 p = params['playlist'].filter(params['filter'])
244 hooke.playlists.add(p)
247 class NoteFilterCommand (FilterCommand):
248 """Create a subset playlist of curves with `.info['note'] != None`.
251 super(NoteFilterCommand, self).__init__()
252 self.name = 'note filter playlist'
253 self.arguments = [a for a in self.arguments if a.name != 'filter']
255 def _run(self, hooke, inqueue, outqueue, params):
256 params['filter'] = lambda curve : \
257 'note' in curve.info and curve.info['note'] != None
258 return super(NoteFilterCommand, self)._run(
259 hooke, inqueue, outqueue, params)