1 """Defines :class:`PlaylistPlugin` several associated
2 :class:`hooke.plugin.Command`\s for handling :mod:`hooke.playlist`
8 from ..command import Command, Argument
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()]
23 # Define common or complicated arguments
25 def current_playlist_callback(hooke, command, argument, value):
26 return hooke.playlists.current()
28 PlaylistArgument = Argument(
29 name='playlist', type='playlist', callback=current_playlist_callback,
31 :class:`hooke.plugin.playlist.Playlist` to act on. Defaults to the
35 def playlist_name_callback(hooke, command, argument, value):
36 return hooke.playlists.free_name()
38 PlaylistNameArgument = Argument(
39 name='name', type='string', optional=True, callback=playlist_name_callback,
41 Name of the new playlist (defaults to an auto-generated name).
47 class NextCommand (Command):
48 """Move playlist to the next curve.
51 super(NextCommand, self).__init__(
53 arguments=[PlaylistArgument],
56 def _run(self, hooke, inqueue, outqueue, params):
57 params['playlist'].next()
59 class PreviousCommand (Command):
60 """Move playlist to the previous curve.
63 super(PreviousCommand, self).__init__(
64 name='previous curve',
65 arguments=[PlaylistArgument],
68 def _run(self, hooke, inqueue, outqueue, params):
69 params['playlist'].previous()
71 class JumpCommand (Command):
72 """Move playlist to a given curve.
75 super(JumpCommand, self).__init__(
79 Argument(name='index', type='int', optional=False, help="""
80 Index of target curve.
85 def _run(self, hooke, inqueue, outqueue, params):
86 params['playlist'].jump(params['index'])
88 class SaveCommand (Command):
92 super(SaveCommand, self).__init__(
96 Argument(name='output', type='file',
98 File name for the output playlist. Defaults to overwriting the input
104 def _run(self, hooke, inqueue, outqueue, params):
105 params['playlist'].save(params['output'])
107 class LoadCommand (Command):
111 super(LoadCommand, self).__init__(
112 name='load playlist',
114 Argument(name='input', type='file', optional=False,
116 File name for the input playlist.
118 Argument(name='drivers', type='driver', optional=False,
121 Drivers for loading curves.
126 def _run(self, hooke, inqueue, outqueue, params):
127 p = FilePlaylist(drivers=params['drivers'], path=params['input'])
129 hooke.playlists.append(p)
132 class AddCommand (Command):
133 """Add a curve to a playlist.
136 super(AddCommand, self).__init__(
137 name='add curve to playlist',
140 Argument(name='input', type='file', optional=False,
142 File name for the input :class:`hooke.curve.Curve`.
144 Argument(name='info', type='dict', optional=True,
146 Additional information for the input :class:`hooke.curve.Curve`.
151 def _run(self, hooke, inqueue, outqueue, params):
152 params['playlist'].append_curve_by_path(params['input'],
155 class AddGlobCommand (Command):
156 """Add curves to a playlist with file globbing.
158 Adding lots of files one at a time can be tedious. With this
159 command you can use globs (`data/curves/*.dat`) to add curves
160 for all matching files at once.
163 super(AddGlobCommand, self).__init__(
164 name='glob curves to playlist',
167 Argument(name='input', type='glob', optional=False,
169 File name glob for the input :class:`hooke.curve.Curve`.
171 Argument(name='info', type='dict', optional=True,
173 Additional information for the input :class:`hooke.curve.Curve`.
178 def _run(self, hooke, inqueue, outqueue, params):
179 for path in sorted(glob.glob(params['input'])):
180 params['playlist'].append_curve_by_path(path, params['info'])
182 class RemoveCommand (Command):
183 """Remove a curve from a playlist.
186 super(RemoveCommand, self).__init__(
187 name='remove curve from playlist',
190 Argument(name='index', type='int', optional=False, help="""
191 Index of target curve.
196 def _run(self, hooke, inqueue, outqueue, params):
197 params['playlist'].pop(params['index'])
198 params['playlist'].jump(params._index)
200 class FilterCommand (Command):
201 """Create a subset playlist via a selection function.
203 Removing lots of curves one at a time can be tedious. With this
204 command you can use a function `filter` to select the curves you
208 super(FilterCommand, self).__init__(
209 name='filter playlist',
212 PlaylistNameArgument,
213 Argument(name='filter', type='function', optional=False,
215 Function returning `True` for "good" curves. `filter(curve) -> True/False`.
220 def _run(self, hooke, inqueue, outqueue, params):
221 p = params['playlist'].filter(params['filter'])
222 hooke.playlists.add(p)