Rework Plugin.commands() to include _setup_commands().
[hooke.git] / hooke / plugin / note.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """The `note` module provides :class:`NotePlugin` the associated
20 :class:`hooke.command.Command`\s for annotating several Hooke classes
21 (:mod:`hooke.playlist.Playlist`, :mod:`hooke.curve.Curve`, ...).
22 """
23
24 from ..command import Command, Argument, Failure
25 from ..playlist import FilePlaylist
26 from ..plugin import Builtin
27 from ..plugin.playlist import current_playlist_callback
28
29
30 class NotePlugin (Builtin):
31     def __init__(self):
32         super(NotePlugin, self).__init__(name='note')
33         self._commands = [
34             AddNoteCommand(), ClearNoteCommand(), GetNoteCommand()]
35         self._setup_commands()
36
37
38 class AddNoteCommand (Command):
39     """Add a note to one of several Hooke objects.
40     """
41     def __init__(self):
42         super(AddNoteCommand, self).__init__(
43             name='add note',
44             arguments=[
45                 Argument(
46                     name='target', type='object',
47                     callback=current_playlist_callback,
48                     help="""
49 Target object for the note.  Defaults to the current playlist.
50 """.strip()),
51                 Argument(
52                     name='note', type='string', optional=False,
53                     help="""
54 The note text.
55 """.strip()),
56                 ],
57             help=self.__doc__)
58
59     def _run(self, hooke, inqueue, outqueue, params):
60         params['target'].info['note'].append(params['note'])
61
62 class ClearNoteCommand (Command):
63     """Remove a note or notes from one of several Hooke objects.
64     """
65     def __init__(self):
66         super(ClearNoteCommand, self).__init__(
67             name='clear note',
68             arguments=[
69                 Argument(
70                     name='target', type='object',
71                     callback=current_playlist_callback,
72                     help="""
73 Target object for the note.  Defaults to the current playlist.
74 """.strip()),
75                 Argument(name='count', type='int', default=-1,
76                          help="""
77 Number of notes to remove.  Defaults to all notes.
78 """.strip()),
79                 Argument(name='force', type='bool', default=False,
80                          help="""
81 Run without prompting the user.  Use if you save often or don't make
82 typing mistakes ;).
83 """.strip()),
84                 ],
85             help=self.__doc__)
86
87     def _run(self, hooke, inqueue, outqueue, params):
88         num_notes = len(params['target'].info['note'])
89         if params['count'] == -1:
90             num_notes_removed = num_notes
91         else:
92             num_notes_removed = min(num_notes, params['count'])
93         if params['force'] == False and num_notes_removed > 0:
94             msg = 'Remove %d notes?' % num_notes_removed
95             default = False
96             outqueue.put(BooleanRequest(msg, default))
97             result = inqueue.get()
98             assert result.type == 'boolean'
99             if result.value == False:
100                 return
101         params['target'].info['note'] = \
102             params['target'].info['note'][:-num_notes_removed]
103
104 class GetNoteCommand (Command):
105     """Retrieve notes from one of several Hooke objects.
106     """
107     def __init__(self):
108         super(GetNoteCommand, self).__init__(
109             name='get note',
110             arguments=[
111                 Argument(
112                     name='target', type='object',
113                     callback=current_playlist_callback,
114                     help="""
115 Target object for the note.  Defaults to the current playlist.
116 """.strip()),
117                 ],
118             help=self.__doc__)
119
120     def _run(self, hooke, inqueue, outqueue, params):
121         outqueue.put(params['target'].info['note'])