Finished off hooke.hooke and fleshed out hooke.ui.
[hooke.git] / hooke / plugin / playlist.py
1 """Defines :class:`PlaylistPlugin` several associated
2 :class:`hooke.plugin.Command`\s for handling :mod:`hooke.playlist`
3 classes.
4 """
5
6 import glob
7
8 from ..command import Command, Argument
9 from ..playlist import FilePlaylist
10 from ..plugin import Builtin
11
12
13 class PlaylistPlugin (Builtin):
14     def __init__(self):
15         super(PlaylistPlugin, self).__init__(name='playlist')
16
17     def commands(self):
18         return [NextCommand(), PreviousCommand(), JumpCommand(),
19                 SaveCommand(), LoadCommand(),
20                 AddCommand(), AddGlobCommand(),
21                 RemoveCommand(), FilterCommand()]
22
23 class NextCommand (Command):
24     """Move playlist to the next curve.
25     """
26     def __init__(self):
27         super(NextCommand, self).__init__(
28             name='next curve',
29             arguments=[
30                 Argument(name='playlist', type='playlist', optional=False,
31                          help="""
32 :class:``hooke.plugin.playlist.Playlist`` to act on.
33 """.strip()),
34                 ],
35             help=self.__doc__)
36
37     def _run(inqueue, outqueue, params):
38         params['playlist'].next()
39
40 class PreviousCommand (Command):
41     """Move playlist to the previous curve.
42     """
43     def __init__(self):
44         super(PreviousCommand, self).__init__(
45             name='previous curve',
46             arguments=[
47                 Argument(name='playlist', type='playlist', optional=False,
48                          help="""
49 :class:``hooke.plugin.playlist.Playlist`` to act on.
50 """.strip()),
51                 ],
52             help=self.__doc__)
53
54     def _run(inqueue, outqueue, params):
55         params['playlist'].previous()
56
57 class JumpCommand (Command):
58     """Move playlist to a given curve.
59     """
60     def __init__(self):
61         super(JumpCommand, self).__init__(
62             name='jump to curve',
63             arguments=[
64                 Argument(name='playlist', type='playlist', optional=False,
65                          help="""
66 :class:``hooke.plugin.playlist.Playlist`` to act on.
67 """.strip()),
68                 Argument(name='index', type='int', optional=False, help="""
69 Index of target curve.
70 """.strip()),
71                 ],
72             help=self.__doc__)
73
74     def _run(inqueue, outqueue, params):
75         params['playlist'].jump(params['index'])
76
77 class SaveCommand (Command):
78     """Save a playlist.
79     """
80     def __init__(self):
81         super(SaveCommand, self).__init__(
82             name='save playlist',
83             arguments=[
84                 Argument(name='playlist', type='playlist', optional=False,
85                          help="""
86 :class:``hooke.plugin.playlist.Playlist`` to act on.
87 """.strip()),
88                 Argument(name='output', type='file',
89                          help="""
90 File name for the output playlist.  Defaults to overwring the input
91 playlist.
92 """.strip()),
93                 ],
94             help=self.__doc__)
95
96     def _run(inqueue, outqueue, params):
97         params['playlist'].save(params['output'])
98
99 class LoadCommand (Command):
100     """Load a playlist.
101     """
102     def __init__(self):
103         super(LoadCommand, self).__init__(
104             name='load playlist',
105             arguments=[
106                 Argument(name='input', type='file', optional=False,
107                          help="""
108 File name for the input playlist.
109 """.strip()),
110                 Argument(name='digests', type='digest', optional=False,
111                          count=-1,
112                          help="""
113 Digests for loading curves.
114 """.strip()),
115                 ],
116             help=self.__doc__)
117
118     def _run(inqueue, outqueue, params):
119         p = FilePlaylist(drivers=params['drivers'], path=params['input'])
120         p.load()
121         outqueue.put(p)
122
123 class AddCommand (Command):
124     """Add a curve to a playlist.
125     """
126     def __init__(self):
127         super(AddCommand, self).__init__(
128             name='add curve to playlist',
129             arguments=[
130                 Argument(name='playlist', type='playlist', optional=False,
131                          help="""
132 :class:``hooke.plugin.playlist.Playlist`` to act on.
133 """.strip()),
134                 Argument(name='input', type='file', optional=False,
135                          help="""
136 File name for the input :class:`hooke.curve.Curve`.
137 """.strip()),
138                 Argument(name='info', type='dict', optional=True,
139                          help="""
140 Additional information for the input :class:`hooke.curve.Curve`.
141 """.strip()),
142                 ],
143             help=self.__doc__)
144
145     def _run(inqueue, outqueue, params):
146         params['playlist'].append_curve_by_path(params['input'],
147                                                 params['info'])
148
149 class AddGlobCommand (Command):
150     """Add curves to a playlist with file globbing.
151
152     Adding lots of files one at a time can be tedious.  With this
153     command you can use globs (`data/curves/*.dat`) to add curves
154     for all matching files at once.
155     """
156     def __init__(self):
157         super(AddGlobCommand, self).__init__(
158             name='glob curves to playlist',
159             arguments=[
160                 Argument(name='playlist', type='playlist', optional=False,
161                          help="""
162 :class:``hooke.plugin.playlist.Playlist`` to act on.
163 """.strip()),
164                 Argument(name='input', type='glob', optional=False,
165                          help="""
166 File name glob for the input :class:`hooke.curve.Curve`.
167 """.strip()),
168                 Argument(name='info', type='dict', optional=True,
169                          help="""
170 Additional information for the input :class:`hooke.curve.Curve`.
171 """.strip()),
172                 ],
173             help=self.__doc__)
174
175     def _run(inqueue, outqueue, params):
176         for path in sorted(glob.glob(params['input'])):
177             params['playlist'].append_curve_by_path(path, params['info'])
178
179 class RemoveCommand (Command):
180     """Remove a curve from a playlist.
181     """
182     def __init__(self):
183         super(RemoveCommand, self).__init__(
184             name='remove curve from playlist',
185             arguments=[
186                 Argument(name='playlist', type='playlist', optional=False,
187                          help="""
188 :class:``hooke.plugin.playlist.Playlist`` to act on.
189 """.strip()),
190                 Argument(name='index', type='int', optional=False, help="""
191 Index of target curve.
192 """.strip()),
193                 ],
194             help=self.__doc__)
195
196     def _run(inqueue, outqueue, params):
197         params['playlist'].pop(params['index'])
198         params['playlist'].jump(params._index)
199
200 class FilterCommand (Command):
201     """Create a subset playlist via a selection function.
202
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
205     wish to keep.
206     """
207     def __init__(self):
208         super(FilterCommand, self).__init__(
209             name='filter playlist',
210             arguments=[
211                 Argument(name='playlist', type='playlist', optional=False,
212                          help="""
213 :class:``hooke.plugin.playlist.Playlist`` to act on.
214 """.strip()),
215                 Argument(name='filter', type='function', optional=False,
216                          help="""
217 Function returning `True` for "good" curves.  `filter(curve) -> True/False`.
218 """.strip()),
219                 ],
220             help=self.__doc__)
221
222     def _run(inqueue, outqueue, params):
223         p = params['playlist'].filter(params['filter'])
224         outqueue.put(p)