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):
27 playlist = hooke.playlists.current()
29 raise Failure('No playlists loaded')
32 PlaylistArgument = Argument(
33 name='playlist', type='playlist', callback=current_playlist_callback,
35 :class:`hooke.plugin.playlist.Playlist` to act on. Defaults to the
39 def playlist_name_callback(hooke, command, argument, value):
40 return hooke.playlists.free_name()
42 PlaylistNameArgument = Argument(
43 name='name', type='string', optional=True, callback=playlist_name_callback,
45 Name of the new playlist (defaults to an auto-generated name).
51 class NextCommand (Command):
52 """Move playlist to the next curve.
55 super(NextCommand, self).__init__(
57 arguments=[PlaylistArgument],
60 def _run(self, hooke, inqueue, outqueue, params):
61 params['playlist'].next()
63 class PreviousCommand (Command):
64 """Move playlist to the previous curve.
67 super(PreviousCommand, self).__init__(
68 name='previous curve',
69 arguments=[PlaylistArgument],
72 def _run(self, hooke, inqueue, outqueue, params):
73 params['playlist'].previous()
75 class JumpCommand (Command):
76 """Move playlist to a given curve.
79 super(JumpCommand, self).__init__(
83 Argument(name='index', type='int', optional=False, help="""
84 Index of target curve.
89 def _run(self, hooke, inqueue, outqueue, params):
90 params['playlist'].jump(params['index'])
92 class SaveCommand (Command):
96 super(SaveCommand, self).__init__(
100 Argument(name='output', type='file',
102 File name for the output playlist. Defaults to overwriting the input
108 def _run(self, hooke, inqueue, outqueue, params):
109 params['playlist'].save(params['output'])
111 class LoadCommand (Command):
115 super(LoadCommand, self).__init__(
116 name='load playlist',
118 Argument(name='input', type='file', optional=False,
120 File name for the input playlist.
122 Argument(name='drivers', type='driver', optional=False,
125 Drivers for loading curves.
130 def _run(self, hooke, inqueue, outqueue, params):
131 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
133 hooke.playlists.append(p)
136 class AddCommand (Command):
137 """Add a curve to a playlist.
140 super(AddCommand, self).__init__(
141 name='add curve to playlist',
144 Argument(name='input', type='file', optional=False,
146 File name for the input :class:`hooke.curve.Curve`.
148 Argument(name='info', type='dict', optional=True,
150 Additional information for the input :class:`hooke.curve.Curve`.
155 def _run(self, hooke, inqueue, outqueue, params):
156 params['playlist'].append_curve_by_path(params['input'],
159 class AddGlobCommand (Command):
160 """Add curves to a playlist with file globbing.
162 Adding lots of files one at a time can be tedious. With this
163 command you can use globs (`data/curves/*.dat`) to add curves
164 for all matching files at once.
167 super(AddGlobCommand, self).__init__(
168 name='glob curves to playlist',
171 Argument(name='input', type='glob', optional=False,
173 File name glob for the input :class:`hooke.curve.Curve`.
175 Argument(name='info', type='dict', optional=True,
177 Additional information for the input :class:`hooke.curve.Curve`.
182 def _run(self, hooke, inqueue, outqueue, params):
183 for path in sorted(glob.glob(params['input'])):
184 params['playlist'].append_curve_by_path(path, params['info'])
186 class RemoveCommand (Command):
187 """Remove a curve from a playlist.
190 super(RemoveCommand, self).__init__(
191 name='remove curve from playlist',
194 Argument(name='index', type='int', optional=False, help="""
195 Index of target curve.
200 def _run(self, hooke, inqueue, outqueue, params):
201 params['playlist'].pop(params['index'])
202 params['playlist'].jump(params._index)
204 class FilterCommand (Command):
205 """Create a subset playlist via a selection function.
207 Removing lots of curves one at a time can be tedious. With this
208 command you can use a function `filter` to select the curves you
212 super(FilterCommand, self).__init__(
213 name='filter playlist',
216 PlaylistNameArgument,
217 Argument(name='filter', type='function', optional=False,
219 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
224 def _run(self, hooke, inqueue, outqueue, params):
225 p = params['playlist'].filter(params['filter'])
226 hooke.playlists.add(p)
229 class NoteFilterCommand (FilterCommand):
230 """Create a subset playlist of curves with `.info['note'] != None`.
233 super(NoteFilterCommand, self).__init__()
234 self.name = 'note filter playlist'
235 self.arguments = [a for a in self.arguments if a.name != 'filter']
237 def _run(self, hooke, inqueue, outqueue, params):
238 params['filter'] = lambda curve : \
239 'note' in curve.info and curve.info['note'] != None
240 return super(NoteFilterCommand, self)._run(
241 hooke, inqueue, outqueue, params)