GetCommand(self), IndexCommand(self), CurveListCommand(self),
SaveCommand(self), LoadCommand(self),
AddCommand(self), AddGlobCommand(self),
- RemoveCommand(self), FilterCommand(self), NoteFilterCommand(self)]
+ RemoveCommand(self), ApplyCommandStack(self),
+ FilterCommand(self), NoteFilterCommand(self),
+ ]
# Define common or complicated arguments
i += 1
PlaylistNameArgument = Argument(
- name='name', type='string', optional=True, callback=playlist_name_callback,
+ name='output playlist', type='string', optional=True,
+ callback=playlist_name_callback,
help="""
Name of the new playlist (defaults to an auto-generated name).
""".strip())
return hooke.drivers
+# Define useful command subclasses
+
+class PlaylistCommand (Command):
+ """A :class:`~hooke.command.Command` operating on a
+ :class:`~hooke.playlist.Playlist`.
+ """
+ def __init__(self, **kwargs):
+ if 'arguments' in kwargs:
+ kwargs['arguments'].insert(0, PlaylistArgument)
+ else:
+ kwargs['arguments'] = [PlaylistArgument]
+ super(PlaylistCommand, self).__init__(**kwargs)
+
+ def _playlist(self, hooke, params):
+ """Get the selected playlist.
+
+ Notes
+ -----
+ `hooke` is intended to attach the selected playlist to the
+ local hooke instance; the returned playlist should not be
+ effected by the state of `hooke`.
+ """
+ # HACK? rely on params['playlist'] being bound to the local
+ # hooke (i.e. not a copy, as you would get by passing a
+ # playlist through the queue). Ugh. Stupid queues. As an
+ # alternative, we could pass lookup information through the
+ # queue...
+ return params['playlist']
+
+
+class PlaylistAddingCommand (Command):
+ """A :class:`~hooke.command.Command` adding a
+ :class:`~hooke.playlist.Playlist`.
+ """
+ def __init__(self, **kwargs):
+ if 'arguments' in kwargs:
+ kwargs['arguments'].insert(0, PlaylistNameArgument)
+ else:
+ kwargs['arguments'] = [PlaylistNameArgument]
+ super(PlaylistAddingCommand, self).__init__(**kwargs)
+
+ def _set_playlist(self, hooke, params, playlist):
+ """Attach a new playlist.
+ """
+ playlist.name = params['output playlist']
+ hooke.playlists.append(playlist)
+
+
# Define commands
-class NextCommand (Command):
+class NextCommand (PlaylistCommand):
"""Move playlist to the next curve.
"""
def __init__(self, plugin):
super(NextCommand, self).__init__(
- name='next curve',
- arguments=[PlaylistArgument],
- help=self.__doc__, plugin=plugin)
+ name='next curve', help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].next()
+ self._playlist(hooke, params).next()
+
-class PreviousCommand (Command):
+class PreviousCommand (PlaylistCommand):
"""Move playlist to the previous curve.
"""
def __init__(self, plugin):
super(PreviousCommand, self).__init__(
- name='previous curve',
- arguments=[PlaylistArgument],
- help=self.__doc__, plugin=plugin)
+ name='previous curve', help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].previous()
+ self._playlist(hooke, params).previous()
+
-class JumpCommand (Command):
+class JumpCommand (PlaylistCommand):
"""Move playlist to a given curve.
"""
def __init__(self, plugin):
super(JumpCommand, self).__init__(
name='jump to curve',
arguments=[
- PlaylistArgument,
Argument(name='index', type='int', optional=False, help="""
Index of target curve.
""".strip()),
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].jump(params['index'])
+ self._playlist(hooke, params).jump(params['index'])
-class IndexCommand (Command):
+
+class IndexCommand (PlaylistCommand):
"""Print the index of the current curve.
The first curve has index 0.
"""
def __init__(self, plugin):
super(IndexCommand, self).__init__(
- name='curve index',
- arguments=[
- PlaylistArgument,
- ],
- help=self.__doc__, plugin=plugin)
+ name='curve index', help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- outqueue.put(params['playlist'].index())
+ outqueue.put(self._playlist(hooke, params).index())
+
-class GetCommand (Command):
+class GetCommand (PlaylistCommand):
"""Return a :class:`hooke.playlist.Playlist`.
"""
def __init__(self, plugin):
super(GetCommand, self).__init__(
- name='get playlist',
- arguments=[PlaylistArgument],
- help=self.__doc__, plugin=plugin)
+ name='get playlist', help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- outqueue.put(params['playlist'])
+ outqueue.put(self._playlist(hooke, params))
-class CurveListCommand (Command):
+
+class CurveListCommand (PlaylistCommand):
"""Get the curves in a playlist.
"""
def __init__(self, plugin):
super(CurveListCommand, self).__init__(
- name='playlist curves',
- arguments=[PlaylistArgument],
- help=self.__doc__, plugin=plugin)
+ name='playlist curves', help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- outqueue.put(list(params['playlist']))
+ outqueue.put(list(self._playlist(hooke, params)))
+
-class SaveCommand (Command):
+class SaveCommand (PlaylistCommand):
"""Save a playlist.
"""
def __init__(self, plugin):
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].save(params['output'])
+ self._playlist(hooke, params).save(params['output'])
-class LoadCommand (Command):
+
+class LoadCommand (PlaylistAddingCommand):
"""Load a playlist.
"""
def __init__(self, plugin):
def _run(self, hooke, inqueue, outqueue, params):
p = FilePlaylist(drivers=params['drivers'], path=params['input'])
p.load(hooke=hooke)
- hooke.playlists.append(p)
+ self._set_playlist(hooke, params, p)
outqueue.put(p)
-class AddCommand (Command):
+
+class AddCommand (PlaylistCommand):
"""Add a curve to a playlist.
"""
def __init__(self, plugin):
super(AddCommand, self).__init__(
name='add curve to playlist',
arguments=[
- PlaylistArgument,
Argument(name='input', type='file', optional=False,
help="""
File name for the input :class:`hooke.curve.Curve`.
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].append_curve_by_path(
+ self._playlist(hooke, params).append_curve_by_path(
params['input'], params['info'], hooke=hooke)
-class AddGlobCommand (Command):
+
+class AddGlobCommand (PlaylistCommand):
"""Add curves to a playlist with file globbing.
Adding lots of files one at a time can be tedious. With this
super(AddGlobCommand, self).__init__(
name='glob curves to playlist',
arguments=[
- PlaylistArgument,
Argument(name='input', type='string', optional=False,
help="""
File name glob for the input :class:`hooke.curve.Curve`.
def _run(self, hooke, inqueue, outqueue, params):
for path in sorted(glob.glob(params['input'])):
- params['playlist'].append_curve_by_path(
+ self._playlist(hooke, params).append_curve_by_path(
path, params['info'], hooke=hooke)
-class RemoveCommand (Command):
+
+class RemoveCommand (PlaylistCommand):
"""Remove a curve from a playlist.
"""
def __init__(self, plugin):
super(RemoveCommand, self).__init__(
name='remove curve from playlist',
arguments=[
- PlaylistArgument,
Argument(name='index', type='int', optional=False, help="""
Index of target curve.
""".strip()),
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].pop(params['index'])
- params['playlist'].jump(params.index())
+ self._playlist(hooke, params).pop(params['index'])
+ self._playlist(hooke, params).jump(params.index())
-class FilterCommand (Command):
+
+class ApplyCommandStack (PlaylistCommand):
+ """Apply a :class:`~hooke.command_stack.CommandStack` to each
+ curve in a playlist.
+
+ TODO: discuss `evaluate`.
+ """
+ def __init__(self, plugin):
+ super(ApplyCommandStack, self).__init__(
+ name='apply command stack',
+ arguments=[
+ Argument(name='commands', type='command stack', optional=False,
+ help="""
+Command stack to apply to each curve.
+""".strip()),
+ Argument(name='evaluate', type='bool', default=False,
+ help="""
+Evaluate the applied command stack immediately.
+""".strip()),
+ ],
+ help=self.__doc__, plugin=plugin)
+
+ def _run(self, hooke, inqueue, outqueue, params):
+ if len(params['commands']) == 0:
+ return
+ p = self._playlist(hooke, params)
+ if params['evaluate'] == True:
+ for curve in p.items():
+ for command in params['commands']:
+ curve.command_stack.execute_command(hooke, command)
+ curve.command_stack.append(command)
+ else:
+ for curve in p:
+ curve.command_stack.extend(params['commands'])
+ curve.unload() # force command stack execution on next access.
+
+
+class FilterCommand (PlaylistAddingCommand, PlaylistCommand):
"""Create a subset playlist via a selection function.
Removing lots of curves one at a time can be tedious. With this
"""
def __init__(self, plugin, name='filter playlist'):
super(FilterCommand, self).__init__(
- name=name,
- arguments=[
- PlaylistArgument,
- PlaylistNameArgument,
- ],
- help=self.__doc__, plugin=plugin)
+ name=name, help=self.__doc__, plugin=plugin)
if not hasattr(self, 'filter'):
self.arguments.append(
Argument(name='filter', type='function', optional=False,
filter_fn = params['filter']
else:
filter_fn = self.filter
- p = params['playlist'].filter(filter_fn,
+ p = self._playlist(hooke, params).filter(filter_fn,
hooke=hooke, inqueue=inqueue, outqueue=outqueue, params=params)
p.name = params['name']
if hasattr(p, 'path') and p.path != None:
p.set_path(os.path.join(os.path.dirname(p.path), p.name))
- hooke.playlists.append(p)
+ self._set_playlist(hooke, params, p)
outqueue.put(p)
+
class NoteFilterCommand (FilterCommand):
"""Create a subset playlist of curves with `.info['note'] != None`.
"""
--- /dev/null
+# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+#
+# This file is part of Hooke.
+#
+# Hooke is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# Hooke is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Hooke. If not, see
+# <http://www.gnu.org/licenses/>.
+
+"""
+>>> import logging
+>>> import sys
+>>> from hooke.command_stack import CommandStack
+>>> from hooke.engine import CommandMessage
+>>> from hooke.hooke import Hooke
+>>> h = Hooke()
+
+Setup logging so we can check command output in the doctest.
+
+>>> log = logging.getLogger('hooke')
+>>> stdout_handler = logging.StreamHandler(sys.stdout)
+>>> log.addHandler(stdout_handler)
+
+Setup a playlist to act on.
+
+>>> h.run_command('load playlist',
+... {'input': 'test/data/vclamp_picoforce/playlist'}) # doctest: +ELLIPSIS
+engine running internal <CommandMessage load playlist {input: test/data/vclamp_picoforce/playlist}>
+engine message from load playlist (<class 'hooke.playlist.FilePlaylist'>): <FilePlaylist ...>
+engine message from load playlist (<class 'hooke.command.Success'>):
+>>> stack = CommandStack([
+... CommandMessage('get curve'),
+... CommandMessage('zero surface contact point'),
+... ])
+
+Test `apply command stack`.
+
+>>> h.run_command('apply command stack',
+... {'commands': stack, 'evaluate': True}) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE, +REPORT_UDIFF
+engine running internal <CommandMessage apply command stack
+ {commands: [<CommandMessage get curve>,
+ <CommandMessage zero surface contact point>],
+ evaluate: True}>
+loading curve 20071120a_i27_t33.100 with driver ...
+engine running internal <CommandMessage get curve>
+engine message from get curve (<class 'hooke.curve.Curve'>): <Curve 20071120a_i27_t33.100>
+engine message from get curve (<class 'hooke.command.Success'>):
+engine running internal <CommandMessage zero surface contact point>
+engine message from zero surface contact point (<type 'dict'>): {...}
+engine message from zero surface contact point (<class 'hooke.command.Success'>):
+loading curve 20071120a_i27_t33.101 with driver ...
+engine running internal <CommandMessage get curve>
+engine message from get curve (<class 'hooke.curve.Curve'>): <Curve 20071120a_i27_t33.101>
+engine message from get curve (<class 'hooke.command.Success'>):
+engine running internal <CommandMessage zero surface contact point>
+engine message from zero surface contact point (<type 'dict'>): {...}
+engine message from zero surface contact point (<class 'hooke.command.Success'>):
+loading curve 20071120a_i27_t33.102 with driver ...
+...
+loading curve 20071120a_i27_t33.199 with driver ...
+engine running internal <CommandMessage get curve>
+engine message from get curve (<class 'hooke.curve.Curve'>): <Curve 20071120a_i27_t33.199>
+engine message from get curve (<class 'hooke.command.Success'>):
+engine running internal <CommandMessage zero surface contact point>
+engine message from zero surface contact point (<type 'dict'>): {...}
+engine message from zero surface contact point (<class 'hooke.command.Success'>):
+loading curve 0x06130001 with driver ...
+unloading curve 20071120a_i27_t33.100
+engine running internal <CommandMessage get curve>
+...
+engine message from apply command stack (<class 'hooke.command.Success'>):
+"""