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