Transition from v0.1 XML playlists to v0.2 YAML playlists.
[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 modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # 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 . import Builtin
26 from .curve import current_curve_callback
27 from .playlist import FilterCommand
28
29 class NotePlugin (Builtin):
30     def __init__(self):
31         super(NotePlugin, self).__init__(name='note')
32         self._commands = [
33             SetNoteCommand(self), GetNoteCommand(self),
34             NoteFilterCommand(self),
35             ]
36
37
38 class SetNoteCommand (Command):
39     """Set the note on one of several Hooke objects.
40     """
41     def __init__(self, plugin):
42         super(SetNoteCommand, self).__init__(
43             name='set note',
44             arguments=[
45                 Argument(
46                     name='target', type='object',
47                     callback=current_curve_callback,
48                     help="""
49 Target object for the note.  Defaults to the current curve.
50 """.strip()),
51                 Argument(
52                     name='note', type='string', optional=False,
53                     help="""
54 The note text.
55 """.strip()),
56                 ],
57             help=self.__doc__, plugin=plugin)
58
59     def _run(self, hooke, inqueue, outqueue, params):
60         params['target'].info['note'] = params['note']
61
62
63 class GetNoteCommand (Command):
64     """Retrieve the note from one of several Hooke objects.
65     """
66     def __init__(self, plugin):
67         super(GetNoteCommand, self).__init__(
68             name='get note',
69             arguments=[
70                 Argument(
71                     name='target', type='object',
72                     callback=current_curve_callback,
73                     help="""
74 Target object for the note.  Defaults to the current curve.
75 """.strip()),
76                 ],
77             help=self.__doc__, plugin=plugin)
78
79     def _run(self, hooke, inqueue, outqueue, params):
80         outqueue.put(params['target'].info.get('note', None))
81
82
83 class NoteFilterCommand (FilterCommand):
84     """Create a subset playlist of curves with `.info['note'] != None`.
85     """
86     def __init__(self, plugin):
87         super(NoteFilterCommand, self).__init__(
88             plugin, name='note filter playlist', load_curves=False)
89
90     def filter(self, curve, hooke, inqueue, outqueue, params):
91         return 'note' in curve.info and curve.info['note'] != None