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