"""Defines :class:`PlaylistPlugin` several associated :class:`hooke.plugin.Command`\s for handling :mod:`hooke.playlist` classes. """ import glob from ..command import Command, Argument from ..playlist import FilePlaylist from ..plugin import Builtin class PlaylistPlugin (Builtin): def __init__(self): super(PlaylistPlugin, self).__init__(name='playlist') def commands(self): return [NextCommand(), PreviousCommand(), JumpCommand(), SaveCommand(), LoadCommand(), AddCommand(), AddGlobCommand(), RemoveCommand(), FilterCommand()] class NextCommand (Command): """Move playlist to the next curve. """ def __init__(self): super(NextCommand, self).__init__( name='next curve', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): params['playlist'].next() class PreviousCommand (Command): """Move playlist to the previous curve. """ def __init__(self): super(PreviousCommand, self).__init__( name='previous curve', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): params['playlist'].previous() class JumpCommand (Command): """Move playlist to a given curve. """ def __init__(self): super(JumpCommand, self).__init__( name='jump to curve', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), Argument(name='index', type='int', optional=False, help=""" Index of target curve. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): params['playlist'].jump(params['index']) class SaveCommand (Command): """Save a playlist. """ def __init__(self): super(SaveCommand, self).__init__( name='save playlist', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), Argument(name='output', type='file', help=""" File name for the output playlist. Defaults to overwring the input playlist. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): params['playlist'].save(params['output']) class LoadCommand (Command): """Load a playlist. """ def __init__(self): super(LoadCommand, self).__init__( name='load playlist', arguments=[ Argument(name='input', type='file', optional=False, help=""" File name for the input playlist. """.strip()), Argument(name='digests', type='digest', optional=False, count=-1, help=""" Digests for loading curves. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): p = FilePlaylist(drivers=params['drivers'], path=params['input']) p.load() outqueue.put(p) class AddCommand (Command): """Add a curve to a playlist. """ def __init__(self): super(AddCommand, self).__init__( name='add curve to playlist', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), Argument(name='input', type='file', optional=False, help=""" File name for the input :class:`hooke.curve.Curve`. """.strip()), Argument(name='info', type='dict', optional=True, help=""" Additional information for the input :class:`hooke.curve.Curve`. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): params['playlist'].append_curve_by_path(params['input'], params['info']) class AddGlobCommand (Command): """Add curves to a playlist with file globbing. Adding lots of files one at a time can be tedious. With this command you can use globs (`data/curves/*.dat`) to add curves for all matching files at once. """ def __init__(self): super(AddGlobCommand, self).__init__( name='glob curves to playlist', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), Argument(name='input', type='glob', optional=False, help=""" File name glob for the input :class:`hooke.curve.Curve`. """.strip()), Argument(name='info', type='dict', optional=True, help=""" Additional information for the input :class:`hooke.curve.Curve`. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): for path in sorted(glob.glob(params['input'])): params['playlist'].append_curve_by_path(path, params['info']) class RemoveCommand (Command): """Remove a curve from a playlist. """ def __init__(self): super(RemoveCommand, self).__init__( name='remove curve from playlist', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), Argument(name='index', type='int', optional=False, help=""" Index of target curve. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): params['playlist'].pop(params['index']) params['playlist'].jump(params._index) class FilterCommand (Command): """Create a subset playlist via a selection function. Removing lots of curves one at a time can be tedious. With this command you can use a function `filter` to select the curves you wish to keep. """ def __init__(self): super(FilterCommand, self).__init__( name='filter playlist', arguments=[ Argument(name='playlist', type='playlist', optional=False, help=""" :class:``hooke.plugin.playlist.Playlist`` to act on. """.strip()), Argument(name='filter', type='function', optional=False, help=""" Function returning `True` for "good" curves. `filter(curve) -> True/False`. """.strip()), ], help=self.__doc__) def _run(inqueue, outqueue, params): p = params['playlist'].filter(params['filter']) outqueue.put(p)