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).
54 class NextCommand (Command):
55 """Move playlist to the next curve.
58 super(NextCommand, self).__init__(
60 arguments=[PlaylistArgument],
63 def _run(self, hooke, inqueue, outqueue, params):
64 params['playlist'].next()
66 class PreviousCommand (Command):
67 """Move playlist to the previous curve.
70 super(PreviousCommand, self).__init__(
71 name='previous curve',
72 arguments=[PlaylistArgument],
75 def _run(self, hooke, inqueue, outqueue, params):
76 params['playlist'].previous()
78 class JumpCommand (Command):
79 """Move playlist to a given curve.
82 super(JumpCommand, self).__init__(
86 Argument(name='index', type='int', optional=False, help="""
87 Index of target curve.
92 def _run(self, hooke, inqueue, outqueue, params):
93 params['playlist'].jump(params['index'])
95 class IndexCommand (Command):
96 """Print the index of the current curve.
98 The first curve has index 0.
101 super(IndexCommand, self).__init__(
108 def _run(self, hooke, inqueue, outqueue, params):
109 outqueue.put(params['playlist']._index)
111 class CurveListCommand (Command):
112 """Get the curves in a playlist.
115 super(CurveListCommand, self).__init__(
116 name='playlist curves',
122 def _run(self, hooke, inqueue, outqueue, params):
123 outqueue.put([c for c in params['playlist']])
125 class SaveCommand (Command):
129 super(SaveCommand, self).__init__(
130 name='save playlist',
133 Argument(name='output', type='file',
135 File name for the output playlist. Defaults to overwriting the input
141 def _run(self, hooke, inqueue, outqueue, params):
142 params['playlist'].save(params['output'])
144 class LoadCommand (Command):
148 super(LoadCommand, self).__init__(
149 name='load playlist',
151 Argument(name='input', type='file', optional=False,
153 File name for the input playlist.
155 Argument(name='drivers', type='driver', optional=False,
158 Drivers for loading curves.
163 def _run(self, hooke, inqueue, outqueue, params):
164 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
166 hooke.playlists.append(p)
169 class AddCommand (Command):
170 """Add a curve to a playlist.
173 super(AddCommand, self).__init__(
174 name='add curve to playlist',
177 Argument(name='input', type='file', optional=False,
179 File name for the input :class:`hooke.curve.Curve`.
181 Argument(name='info', type='dict', optional=True,
183 Additional information for the input :class:`hooke.curve.Curve`.
188 def _run(self, hooke, inqueue, outqueue, params):
189 params['playlist'].append_curve_by_path(params['input'],
192 class AddGlobCommand (Command):
193 """Add curves to a playlist with file globbing.
195 Adding lots of files one at a time can be tedious. With this
196 command you can use globs (`data/curves/*.dat`) to add curves
197 for all matching files at once.
200 super(AddGlobCommand, self).__init__(
201 name='glob curves to playlist',
204 Argument(name='input', type='glob', optional=False,
206 File name glob for the input :class:`hooke.curve.Curve`.
208 Argument(name='info', type='dict', optional=True,
210 Additional information for the input :class:`hooke.curve.Curve`.
215 def _run(self, hooke, inqueue, outqueue, params):
216 for path in sorted(glob.glob(params['input'])):
217 params['playlist'].append_curve_by_path(path, params['info'])
219 class RemoveCommand (Command):
220 """Remove a curve from a playlist.
223 super(RemoveCommand, self).__init__(
224 name='remove curve from playlist',
227 Argument(name='index', type='int', optional=False, help="""
228 Index of target curve.
233 def _run(self, hooke, inqueue, outqueue, params):
234 params['playlist'].pop(params['index'])
235 params['playlist'].jump(params._index)
237 class FilterCommand (Command):
238 """Create a subset playlist via a selection function.
240 Removing lots of curves one at a time can be tedious. With this
241 command you can use a function `filter` to select the curves you
245 super(FilterCommand, self).__init__(
246 name='filter playlist',
249 PlaylistNameArgument,
250 Argument(name='filter', type='function', optional=False,
252 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
257 def _run(self, hooke, inqueue, outqueue, params):
258 p = params['playlist'].filter(params['filter'])
259 hooke.playlists.add(p)
262 class NoteFilterCommand (FilterCommand):
263 """Create a subset playlist of curves with `.info['note'] != None`.
266 super(NoteFilterCommand, self).__init__()
267 self.name = 'note filter playlist'
268 self.arguments = [a for a in self.arguments if a.name != 'filter']
270 def _run(self, hooke, inqueue, outqueue, params):
271 params['filter'] = lambda curve : \
272 'note' in curve.info and curve.info['note'] != None
273 return super(NoteFilterCommand, self)._run(
274 hooke, inqueue, outqueue, params)