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 IndexCommand(), CurveListCommand(),
20 SaveCommand(), LoadCommand(),
21 AddCommand(), AddGlobCommand(),
22 RemoveCommand(), FilterCommand(), NoteFilterCommand()]
25 # Define common or complicated arguments
27 def current_playlist_callback(hooke, command, argument, value):
30 playlist = hooke.playlists.current()
32 raise Failure('No playlists loaded')
35 PlaylistArgument = Argument(
36 name='playlist', type='playlist', callback=current_playlist_callback,
38 :class:`hooke.plugin.playlist.Playlist` to act on. Defaults to the
42 def playlist_name_callback(hooke, command, argument, value):
43 return hooke.playlists.free_name()
45 PlaylistNameArgument = Argument(
46 name='name', type='string', optional=True, callback=playlist_name_callback,
48 Name of the new playlist (defaults to an auto-generated name).
51 def all_drivers_callback(hooke, command, argument, value):
57 class NextCommand (Command):
58 """Move playlist to the next curve.
61 super(NextCommand, self).__init__(
63 arguments=[PlaylistArgument],
66 def _run(self, hooke, inqueue, outqueue, params):
67 params['playlist'].next()
69 class PreviousCommand (Command):
70 """Move playlist to the previous curve.
73 super(PreviousCommand, self).__init__(
74 name='previous curve',
75 arguments=[PlaylistArgument],
78 def _run(self, hooke, inqueue, outqueue, params):
79 params['playlist'].previous()
81 class JumpCommand (Command):
82 """Move playlist to a given curve.
85 super(JumpCommand, self).__init__(
89 Argument(name='index', type='int', optional=False, help="""
90 Index of target curve.
95 def _run(self, hooke, inqueue, outqueue, params):
96 params['playlist'].jump(params['index'])
98 class IndexCommand (Command):
99 """Print the index of the current curve.
101 The first curve has index 0.
104 super(IndexCommand, self).__init__(
111 def _run(self, hooke, inqueue, outqueue, params):
112 outqueue.put(params['playlist']._index)
114 class CurveListCommand (Command):
115 """Get the curves in a playlist.
118 super(CurveListCommand, self).__init__(
119 name='playlist curves',
125 def _run(self, hooke, inqueue, outqueue, params):
126 outqueue.put([c for c in params['playlist']])
128 class SaveCommand (Command):
132 super(SaveCommand, self).__init__(
133 name='save playlist',
136 Argument(name='output', type='file',
138 File name for the output playlist. Defaults to overwriting the input
144 def _run(self, hooke, inqueue, outqueue, params):
145 params['playlist'].save(params['output'])
147 class LoadCommand (Command):
151 super(LoadCommand, self).__init__(
152 name='load playlist',
154 Argument(name='input', type='file', optional=False,
156 File name for the input playlist.
158 Argument(name='drivers', type='driver', optional=True,
159 count=-1, callback=all_drivers_callback,
161 Drivers for loading curves.
166 def _run(self, hooke, inqueue, outqueue, params):
167 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
169 hooke.playlists.append(p)
172 class AddCommand (Command):
173 """Add a curve to a playlist.
176 super(AddCommand, self).__init__(
177 name='add curve to playlist',
180 Argument(name='input', type='file', optional=False,
182 File name for the input :class:`hooke.curve.Curve`.
184 Argument(name='info', type='dict', optional=True,
186 Additional information for the input :class:`hooke.curve.Curve`.
191 def _run(self, hooke, inqueue, outqueue, params):
192 params['playlist'].append_curve_by_path(params['input'],
195 class AddGlobCommand (Command):
196 """Add curves to a playlist with file globbing.
198 Adding lots of files one at a time can be tedious. With this
199 command you can use globs (`data/curves/*.dat`) to add curves
200 for all matching files at once.
203 super(AddGlobCommand, self).__init__(
204 name='glob curves to playlist',
207 Argument(name='input', type='glob', optional=False,
209 File name glob for the input :class:`hooke.curve.Curve`.
211 Argument(name='info', type='dict', optional=True,
213 Additional information for the input :class:`hooke.curve.Curve`.
218 def _run(self, hooke, inqueue, outqueue, params):
219 for path in sorted(glob.glob(params['input'])):
220 params['playlist'].append_curve_by_path(path, params['info'])
222 class RemoveCommand (Command):
223 """Remove a curve from a playlist.
226 super(RemoveCommand, self).__init__(
227 name='remove curve from playlist',
230 Argument(name='index', type='int', optional=False, help="""
231 Index of target curve.
236 def _run(self, hooke, inqueue, outqueue, params):
237 params['playlist'].pop(params['index'])
238 params['playlist'].jump(params._index)
240 class FilterCommand (Command):
241 """Create a subset playlist via a selection function.
243 Removing lots of curves one at a time can be tedious. With this
244 command you can use a function `filter` to select the curves you
248 super(FilterCommand, self).__init__(
249 name='filter playlist',
252 PlaylistNameArgument,
253 Argument(name='filter', type='function', optional=False,
255 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
260 def _run(self, hooke, inqueue, outqueue, params):
261 p = params['playlist'].filter(params['filter'])
262 hooke.playlists.add(p)
265 class NoteFilterCommand (FilterCommand):
266 """Create a subset playlist of curves with `.info['note'] != None`.
269 super(NoteFilterCommand, self).__init__()
270 self.name = 'note filter playlist'
271 self.arguments = [a for a in self.arguments if a.name != 'filter']
273 def _run(self, hooke, inqueue, outqueue, params):
274 params['filter'] = lambda curve : \
275 'note' in curve.info and curve.info['note'] != None
276 return super(NoteFilterCommand, self)._run(
277 hooke, inqueue, outqueue, params)