3 """The `playlist` module provides :class:`PlaylistPlugin` several
4 associated :class:`hooke.command.Command`\s for handling
5 :mod:`hooke.playlist` classes.
10 from ..command import Command, Argument, Failure
11 from ..playlist import FilePlaylist
12 from ..plugin import Builtin
15 class PlaylistPlugin (Builtin):
17 super(PlaylistPlugin, self).__init__(name='playlist')
20 return [NextCommand(), PreviousCommand(), JumpCommand(),
21 IndexCommand(), CurveListCommand(),
22 SaveCommand(), LoadCommand(),
23 AddCommand(), AddGlobCommand(),
24 RemoveCommand(), FilterCommand(), NoteFilterCommand()]
27 # Define common or complicated arguments
29 def current_playlist_callback(hooke, command, argument, value):
32 playlist = hooke.playlists.current()
34 raise Failure('No playlists loaded')
37 PlaylistArgument = Argument(
38 name='playlist', type='playlist', callback=current_playlist_callback,
40 :class:`hooke.plugin.playlist.Playlist` to act on. Defaults to the
44 def playlist_name_callback(hooke, command, argument, value):
45 return hooke.playlists.free_name()
47 PlaylistNameArgument = Argument(
48 name='name', type='string', optional=True, callback=playlist_name_callback,
50 Name of the new playlist (defaults to an auto-generated name).
53 def all_drivers_callback(hooke, command, argument, value):
59 class NextCommand (Command):
60 """Move playlist to the next curve.
63 super(NextCommand, self).__init__(
65 arguments=[PlaylistArgument],
68 def _run(self, hooke, inqueue, outqueue, params):
69 params['playlist'].next()
71 class PreviousCommand (Command):
72 """Move playlist to the previous curve.
75 super(PreviousCommand, self).__init__(
76 name='previous curve',
77 arguments=[PlaylistArgument],
80 def _run(self, hooke, inqueue, outqueue, params):
81 params['playlist'].previous()
83 class JumpCommand (Command):
84 """Move playlist to a given curve.
87 super(JumpCommand, self).__init__(
91 Argument(name='index', type='int', optional=False, help="""
92 Index of target curve.
97 def _run(self, hooke, inqueue, outqueue, params):
98 params['playlist'].jump(params['index'])
100 class IndexCommand (Command):
101 """Print the index of the current curve.
103 The first curve has index 0.
106 super(IndexCommand, self).__init__(
113 def _run(self, hooke, inqueue, outqueue, params):
114 outqueue.put(params['playlist']._index)
116 class CurveListCommand (Command):
117 """Get the curves in a playlist.
120 super(CurveListCommand, self).__init__(
121 name='playlist curves',
127 def _run(self, hooke, inqueue, outqueue, params):
128 outqueue.put([c for c in params['playlist']])
130 class SaveCommand (Command):
134 super(SaveCommand, self).__init__(
135 name='save playlist',
138 Argument(name='output', type='file',
140 File name for the output playlist. Defaults to overwriting the input
146 def _run(self, hooke, inqueue, outqueue, params):
147 params['playlist'].save(params['output'])
149 class LoadCommand (Command):
153 super(LoadCommand, self).__init__(
154 name='load playlist',
156 Argument(name='input', type='file', optional=False,
158 File name for the input playlist.
160 Argument(name='drivers', type='driver', optional=True,
161 count=-1, callback=all_drivers_callback,
163 Drivers for loading curves.
168 def _run(self, hooke, inqueue, outqueue, params):
169 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
171 hooke.playlists.append(p)
174 class AddCommand (Command):
175 """Add a curve to a playlist.
178 super(AddCommand, self).__init__(
179 name='add curve to playlist',
182 Argument(name='input', type='file', optional=False,
184 File name for the input :class:`hooke.curve.Curve`.
186 Argument(name='info', type='dict', optional=True,
188 Additional information for the input :class:`hooke.curve.Curve`.
193 def _run(self, hooke, inqueue, outqueue, params):
194 params['playlist'].append_curve_by_path(params['input'],
197 class AddGlobCommand (Command):
198 """Add curves to a playlist with file globbing.
200 Adding lots of files one at a time can be tedious. With this
201 command you can use globs (`data/curves/*.dat`) to add curves
202 for all matching files at once.
205 super(AddGlobCommand, self).__init__(
206 name='glob curves to playlist',
209 Argument(name='input', type='glob', optional=False,
211 File name glob for the input :class:`hooke.curve.Curve`.
213 Argument(name='info', type='dict', optional=True,
215 Additional information for the input :class:`hooke.curve.Curve`.
220 def _run(self, hooke, inqueue, outqueue, params):
221 for path in sorted(glob.glob(params['input'])):
222 params['playlist'].append_curve_by_path(path, params['info'])
224 class RemoveCommand (Command):
225 """Remove a curve from a playlist.
228 super(RemoveCommand, self).__init__(
229 name='remove curve from playlist',
232 Argument(name='index', type='int', optional=False, help="""
233 Index of target curve.
238 def _run(self, hooke, inqueue, outqueue, params):
239 params['playlist'].pop(params['index'])
240 params['playlist'].jump(params._index)
242 class FilterCommand (Command):
243 """Create a subset playlist via a selection function.
245 Removing lots of curves one at a time can be tedious. With this
246 command you can use a function `filter` to select the curves you
250 super(FilterCommand, self).__init__(
251 name='filter playlist',
254 PlaylistNameArgument,
255 Argument(name='filter', type='function', optional=False,
257 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
262 def _run(self, hooke, inqueue, outqueue, params):
263 p = params['playlist'].filter(params['filter'])
264 hooke.playlists.add(p)
267 class NoteFilterCommand (FilterCommand):
268 """Create a subset playlist of curves with `.info['note'] != None`.
271 super(NoteFilterCommand, self).__init__()
272 self.name = 'note filter playlist'
273 self.arguments = [a for a in self.arguments if a.name != 'filter']
275 def _run(self, hooke, inqueue, outqueue, params):
276 params['filter'] = lambda curve : \
277 'note' in curve.info and curve.info['note'] != None
278 return super(NoteFilterCommand, self)._run(
279 hooke, inqueue, outqueue, params)