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 SaveCommand (Command):
98 super(SaveCommand, self).__init__(
102 Argument(name='output', type='file',
104 File name for the output playlist. Defaults to overwriting the input
110 def _run(self, hooke, inqueue, outqueue, params):
111 params['playlist'].save(params['output'])
113 class LoadCommand (Command):
117 super(LoadCommand, self).__init__(
118 name='load playlist',
120 Argument(name='input', type='file', optional=False,
122 File name for the input playlist.
124 Argument(name='drivers', type='driver', optional=False,
127 Drivers for loading curves.
132 def _run(self, hooke, inqueue, outqueue, params):
133 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
135 hooke.playlists.append(p)
138 class AddCommand (Command):
139 """Add a curve to a playlist.
142 super(AddCommand, self).__init__(
143 name='add curve to playlist',
146 Argument(name='input', type='file', optional=False,
148 File name for the input :class:`hooke.curve.Curve`.
150 Argument(name='info', type='dict', optional=True,
152 Additional information for the input :class:`hooke.curve.Curve`.
157 def _run(self, hooke, inqueue, outqueue, params):
158 params['playlist'].append_curve_by_path(params['input'],
161 class AddGlobCommand (Command):
162 """Add curves to a playlist with file globbing.
164 Adding lots of files one at a time can be tedious. With this
165 command you can use globs (`data/curves/*.dat`) to add curves
166 for all matching files at once.
169 super(AddGlobCommand, self).__init__(
170 name='glob curves to playlist',
173 Argument(name='input', type='glob', optional=False,
175 File name glob for the input :class:`hooke.curve.Curve`.
177 Argument(name='info', type='dict', optional=True,
179 Additional information for the input :class:`hooke.curve.Curve`.
184 def _run(self, hooke, inqueue, outqueue, params):
185 for path in sorted(glob.glob(params['input'])):
186 params['playlist'].append_curve_by_path(path, params['info'])
188 class RemoveCommand (Command):
189 """Remove a curve from a playlist.
192 super(RemoveCommand, self).__init__(
193 name='remove curve from playlist',
196 Argument(name='index', type='int', optional=False, help="""
197 Index of target curve.
202 def _run(self, hooke, inqueue, outqueue, params):
203 params['playlist'].pop(params['index'])
204 params['playlist'].jump(params._index)
206 class FilterCommand (Command):
207 """Create a subset playlist via a selection function.
209 Removing lots of curves one at a time can be tedious. With this
210 command you can use a function `filter` to select the curves you
214 super(FilterCommand, self).__init__(
215 name='filter playlist',
218 PlaylistNameArgument,
219 Argument(name='filter', type='function', optional=False,
221 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
226 def _run(self, hooke, inqueue, outqueue, params):
227 p = params['playlist'].filter(params['filter'])
228 hooke.playlists.add(p)
231 class NoteFilterCommand (FilterCommand):
232 """Create a subset playlist of curves with `.info['note'] != None`.
235 super(NoteFilterCommand, self).__init__()
236 self.name = 'note filter playlist'
237 self.arguments = [a for a in self.arguments if a.name != 'filter']
239 def _run(self, hooke, inqueue, outqueue, params):
240 params['filter'] = lambda curve : \
241 'note' in curve.info and curve.info['note'] != None
242 return super(NoteFilterCommand, self)._run(
243 hooke, inqueue, outqueue, params)