Moved note handling commands to hooke.plugin.note.
[hooke.git] / hooke / plugin / note.py
1 """The `note` module provides :class:`NotePlugin` the associated
2 :class:`hooke.command.Command`\s for annotating several Hooke classes
3 (:mod:`hooke.playlist.Playlist`, :mod:`hooke.curve.Curve`, ...).
4 """
5
6 from ..command import Command, Argument, Failure
7 from ..playlist import FilePlaylist
8 from ..plugin import Builtin
9 from ..plugin.playlist import current_playlist_callback
10
11
12 class NotePlugin (Builtin):
13     def __init__(self):
14         super(NotePlugin, self).__init__(name='note')
15
16     def commands(self):
17         return [AddNoteCommand(), ClearNoteCommand(), GetNoteCommand()]
18
19     def dependencies(self):
20         return [
21             'playlist', # for current_playlist_callback
22             ]
23
24
25 class AddNoteCommand (Command):
26     """Add a note to one of several Hooke objects.
27     """
28     def __init__(self):
29         super(AddNoteCommand, self).__init__(
30             name='add note',
31             arguments=[
32                 Argument(
33                     name='target', type='object',
34                     callback=current_playlist_callback,
35                     help="""
36 Target object for the note.  Defaults to the current playlist.
37 """.strip()),
38                 Argument(
39                     name='note', type='string', optional=False,
40                     help="""
41 The note text.
42 """.strip()),
43                 ],
44             help=self.__doc__)
45
46     def _run(self, hooke, inqueue, outqueue, params):
47         params['target'].info['note'].append(params['note'])
48
49 class ClearNoteCommand (Command):
50     """Remove a note or notes from one of several Hooke objects.
51     """
52     def __init__(self):
53         super(ClearNoteCommand, self).__init__(
54             name='clear note',
55             arguments=[
56                 Argument(
57                     name='target', type='object',
58                     callback=current_playlist_callback,
59                     help="""
60 Target object for the note.  Defaults to the current playlist.
61 """.strip()),
62                 Argument(name='count', type='int', default=-1,
63                          help="""
64 Number of notes to remove.  Defaults to all notes.
65 """.strip()),
66                 Argument(name='force', type='bool', default=False,
67                          help="""
68 Run without prompting the user.  Use if you save often or don't make
69 typing mistakes ;).
70 """.strip()),
71                 ],
72             help=self.__doc__)
73
74     def _run(self, hooke, inqueue, outqueue, params):
75         num_notes = len(params['target'].info['note'])
76         if params['count'] == -1:
77             num_notes_removed = num_notes
78         else:
79             num_notes_removed = min(num_notes, params['count'])
80         if params['force'] == False and num_notes_removed > 0:
81             msg = 'Remove %d notes?' % num_notes_removed
82             default = False
83             outqueue.put(BooleanRequest(msg, default))
84             result = inqueue.get()
85             assert result.type == 'boolean'
86             if result.value == False:
87                 return
88         params['target'].info['note'] = \
89             params['target'].info['note'][:-num_notes_removed]
90
91 class GetNoteCommand (Command):
92     """Retrieve notes from one of several Hooke objects.
93     """
94     def __init__(self):
95         super(GetNoteCommand, self).__init__(
96             name='get note',
97             arguments=[
98                 Argument(
99                     name='target', type='object',
100                     callback=current_playlist_callback,
101                     help="""
102 Target object for the note.  Defaults to the current playlist.
103 """.strip()),
104                 ],
105             help=self.__doc__)
106
107     def _run(self, hooke, inqueue, outqueue, params):
108         outqueue.put(params['target'].info['note'])