Merged Rolf Schmidt's illysam branch
[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(self), ClearNoteCommand(self), GetNoteCommand(self)]
35
36
37 class AddNoteCommand (Command):
38     """Add a note to one of several Hooke objects.
39     """
40     def __init__(self, plugin):
41         super(AddNoteCommand, self).__init__(
42             name='add note',
43             arguments=[
44                 Argument(
45                     name='target', type='object',
46                     callback=current_playlist_callback,
47                     help="""
48 Target object for the note.  Defaults to the current playlist.
49 """.strip()),
50                 Argument(
51                     name='note', type='string', optional=False,
52                     help="""
53 The note text.
54 """.strip()),
55                 ],
56             help=self.__doc__, plugin=plugin)
57
58     def _run(self, hooke, inqueue, outqueue, params):
59         params['target'].info['note'].append(params['note'])
60
61 class ClearNoteCommand (Command):
62     """Remove a note or notes from one of several Hooke objects.
63     """
64     def __init__(self, plugin):
65         super(ClearNoteCommand, self).__init__(
66             name='clear note',
67             arguments=[
68                 Argument(
69                     name='target', type='object',
70                     callback=current_playlist_callback,
71                     help="""
72 Target object for the note.  Defaults to the current playlist.
73 """.strip()),
74                 Argument(name='count', type='int', default=-1,
75                          help="""
76 Number of notes to remove.  Defaults to all notes.
77 """.strip()),
78                 Argument(name='force', type='bool', default=False,
79                          help="""
80 Run without prompting the user.  Use if you save often or don't make
81 typing mistakes ;).
82 """.strip()),
83                 ],
84             help=self.__doc__, plugin=plugin)
85
86     def _run(self, hooke, inqueue, outqueue, params):
87         num_notes = len(params['target'].info['note'])
88         if params['count'] == -1:
89             num_notes_removed = num_notes
90         else:
91             num_notes_removed = min(num_notes, params['count'])
92         if params['force'] == False and num_notes_removed > 0:
93             msg = 'Remove %d notes?' % num_notes_removed
94             default = False
95             outqueue.put(BooleanRequest(msg, default))
96             result = inqueue.get()
97             assert result.type == 'boolean'
98             if result.value == False:
99                 return
100         params['target'].info['note'] = \
101             params['target'].info['note'][:-num_notes_removed]
102
103 class GetNoteCommand (Command):
104     """Retrieve notes from one of several Hooke objects.
105     """
106     def __init__(self, plugin):
107         super(GetNoteCommand, self).__init__(
108             name='get note',
109             arguments=[
110                 Argument(
111                     name='target', type='object',
112                     callback=current_playlist_callback,
113                     help="""
114 Target object for the note.  Defaults to the current playlist.
115 """.strip()),
116                 ],
117             help=self.__doc__, plugin=plugin)
118
119     def _run(self, hooke, inqueue, outqueue, params):
120         outqueue.put(params['target'].info['note'])