Run update-copyright.py.
[hooke.git] / hooke / plugin / note.py
1 # Copyright (C) 2010-2012 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 modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 """The `note` module provides :class:`NotePlugin` the associated
19 :class:`hooke.command.Command`\s for annotating several Hooke classes
20 (:mod:`hooke.playlist.Playlist`, :mod:`hooke.curve.Curve`, ...).
21 """
22
23 from ..command import Command, Argument, Failure
24 from . import Builtin
25 from .curve import current_curve_callback
26 from .playlist import FilterCommand
27
28 class NotePlugin (Builtin):
29     def __init__(self):
30         super(NotePlugin, self).__init__(name='note')
31         self._commands = [
32             SetNoteCommand(self), GetNoteCommand(self),
33             NoteFilterCommand(self),
34             ]
35
36
37 class SetNoteCommand (Command):
38     """Set the note on one of several Hooke objects.
39     """
40     def __init__(self, plugin):
41         super(SetNoteCommand, self).__init__(
42             name='set note',
43             arguments=[
44                 Argument(
45                     name='target', type='object',
46                     callback=current_curve_callback,
47                     help="""
48 Target object for the note.  Defaults to the current curve.
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'] = params['note']
60
61
62 class GetNoteCommand (Command):
63     """Retrieve the note from one of several Hooke objects.
64     """
65     def __init__(self, plugin):
66         super(GetNoteCommand, self).__init__(
67             name='get note',
68             arguments=[
69                 Argument(
70                     name='target', type='object',
71                     callback=current_curve_callback,
72                     help="""
73 Target object for the note.  Defaults to the current curve.
74 """.strip()),
75                 ],
76             help=self.__doc__, plugin=plugin)
77
78     def _run(self, hooke, inqueue, outqueue, params):
79         outqueue.put(params['target'].info.get('note', None))
80
81
82 class NoteFilterCommand (FilterCommand):
83     """Create a subset playlist of curves with `.info['note'] != None`.
84     """
85     def __init__(self, plugin):
86         super(NoteFilterCommand, self).__init__(
87             plugin, name='note filter playlist', load_curves=False)
88
89     def filter(self, curve, hooke, inqueue, outqueue, params):
90         return 'note' in curve.info and curve.info['note'] != None