Remove leftover curve.InfoCommand cruft from ExportCommand.__init__.
[hooke.git] / hooke / plugin / curve.py
1 # Copyright (C) 2010 Fibrin's Benedetti
2 #                    W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 """The ``curve`` module provides :class:`CurvePlugin` and several
21 associated :class:`hooke.command.Command`\s for handling
22 :mod:`hooke.curve` classes.
23 """
24
25 from ..command import Command, Argument, Failure
26 from ..plugin import Builtin
27 from ..plugin.playlist import current_playlist_callback
28
29
30 class CurvePlugin (Builtin):
31     def __init__(self):
32         super(CurvePlugin, self).__init__(name='curve')
33
34     def commands(self):
35         return [InfoCommand(), ]
36
37
38 # Define common or complicated arguments
39
40 def current_curve_callback(hooke, command, argument, value):
41     if value != None:
42         return value
43     playlist = current_playlist_callback(hooke, command, argument, value)
44     curve = playlist.current()
45     if curve == None:
46         raise Failure('No curves in %s' % playlist)
47     return curve
48
49 CurveArgument = Argument(
50     name='curve', type='curve', callback=current_curve_callback,
51     help="""
52 :class:`hooke.curve.Curve` to act on.  Defaults to the current curve
53 of the current playlist.
54 """.strip())
55
56
57 # Define commands
58
59 class InfoCommand (Command):
60     """Get selected information about a :class:`hooke.curve.Curve`.
61     """
62     def __init__(self):
63         args = [
64             CurveArgument,                    
65             Argument(name='all', type='bool', default=False, count=1,
66                      help='Get all curve information.'),
67             ]
68         self.fields = ['name', 'path', 'experiment', 'driver', 'filetype', 'note',
69                        'blocks', 'block sizes']
70         for field in self.fields:
71             args.append(Argument(
72                     name=field, type='bool', default=False, count=1,
73                     help='Get curve %s' % field))
74         super(InfoCommand, self).__init__(
75             name='curve info', arguments=args, help=self.__doc__)
76
77     def _run(self, hooke, inqueue, outqueue, params):
78         fields = {}
79         for key in self.fields:
80             fields[key] = params[key]
81         if reduce(lambda x,y: x and y, fields.values()) == False:
82             params['all'] = True # No specific fields set, default to 'all'
83         if params['all'] == True:
84             for key in self.fields:
85                 fields[key] = True
86         lines = []
87         for key in self.fields:
88             if fields[key] == True:
89                 get = getattr(self, '_get_%s' % key.replace(' ', '_'))
90                 lines.append('%s: %s' % (key, get(params['curve'])))
91         outqueue.put('\n'.join(lines))
92
93     def _get_name(self, curve):
94         return curve.name
95
96     def _get_path(self, curve):
97         return curve.path
98
99     def _get_experiment(self, curve):
100         return curve.info.get('experiment', None)
101
102     def _get_driver(self, curve):
103         return curve.driver
104
105     def _get_filetype(self, curve):
106         return curve.info.get('filetype', None)
107
108     def _get_note(self, curve):
109         return curve.info.get('note', None)
110                               
111     def _get_blocks(self, curve):
112         return len(curve.data)
113
114     def _get_block_sizes(self, curve):
115         return [block.shape for block in curve.data]
116
117
118 class ExportCommand (Command):
119     """Export a :class:`hooke.curve.Curve` data block as TAB-delimeted
120     ASCII text.
121     """
122     def __init__(self):
123         super(InfoCommand, self).__init__(
124             name='curve info',
125             arguments=[
126                 CurveArgument,
127                 Argument(name='block', aliases=['set'], type='int', default=0,
128                     help="""
129 Data block to save.  For an approach/retract force curve, `0` selects
130 the approacing curve and `1` selects the retracting curve.
131 """.strip()),
132                 Argument(name='output', type='file', default='curve.dat',
133                          help="""
134 File name for the output data.  Defaults to 'curve.dat'
135 """.strip()),
136                 ],
137             help=self.__doc__)
138
139     def _run(self, hooke, inqueue, outqueue, params):
140         data = params['curve'].data[params['index']]
141         f = open(params['output'], 'w')
142         data.tofile(f, sep='\t')
143         f.close()