From 62cfb3bde4faa22da8d131571dc6f093682a0cca Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 18 May 2010 07:34:02 -0400 Subject: [PATCH] Rework Plugin.commands() to include _setup_commands(). The previous implementation gave Commands no way to access the Plugin's configuration. --- hooke/plugin/__init__.py | 18 +++++++++++++++--- hooke/plugin/config.py | 5 ++--- hooke/plugin/curve.py | 10 +++++----- hooke/plugin/cut.py | 5 ++--- hooke/plugin/debug.py | 5 ++--- hooke/plugin/multifit.py | 4 ++-- hooke/plugin/note.py | 11 +++-------- hooke/plugin/playlist.py | 14 +++++++------- hooke/plugin/system.py | 8 ++++---- 9 files changed, 42 insertions(+), 38 deletions(-) diff --git a/hooke/plugin/__init__.py b/hooke/plugin/__init__.py index 89a23b2..2418b69 100644 --- a/hooke/plugin/__init__.py +++ b/hooke/plugin/__init__.py @@ -34,7 +34,7 @@ PLUGIN_MODULES = [ ('cut', True), # ('fit', True), # ('flatfilts-rolf', True), -# ('flatfilts', True), + ('flatfilt', True), # ('generalclamp', True), # ('generaltccd', True), # ('generalvclamp', True), @@ -83,9 +83,10 @@ class Plugin (object): self.name = name self.setting_section = '%s plugin' % self.name self.config = {} + self._commands = [] def dependencies(self): - """Return a list of :class:`Plugin`\s we require.""" + """Return a list of names of :class:`Plugin`\s we require.""" return [] def default_settings(self): @@ -98,10 +99,21 @@ class Plugin (object): """ return [] + def _setup_commands(self): + """Perform internal setup on stored commands. + + Currently: + + * Adds a `plugin` attribute to each command so they can access + the plugin configuration with `.plugin.config`. + """ + for command in self._commands: + command.plugin = self + def commands(self): """Return a list of :class:`hooke.command.Command`\s provided. """ - return [] + return list(self._commands) class Builtin (Plugin): """A required collection of Hooke commands. diff --git a/hooke/plugin/config.py b/hooke/plugin/config.py index ff76d8f..7cb4ef5 100644 --- a/hooke/plugin/config.py +++ b/hooke/plugin/config.py @@ -31,9 +31,8 @@ from ..plugin import Builtin class ConfigPlugin (Builtin): def __init__(self): super(ConfigPlugin, self).__init__(name='config') - - def commands(self): - return [GetCommand(), SetCommand(), PrintCommand()] + self._commands = [GetCommand(), SetCommand(), PrintCommand()] + self._setup_commands() # Define common or complicated arguments diff --git a/hooke/plugin/curve.py b/hooke/plugin/curve.py index af427d6..27fb0cd 100644 --- a/hooke/plugin/curve.py +++ b/hooke/plugin/curve.py @@ -35,11 +35,11 @@ from ..util.fft import unitary_avg_power_spectrum class CurvePlugin (Builtin): def __init__(self): super(CurvePlugin, self).__init__(name='curve') - - def commands(self): - return [InfoCommand(), ExportCommand(), - DifferenceCommand(), DerivativeCommand(), - PowerSpectrumCommand()] + self._commands = [ + InfoCommand(), ExportCommand(), + DifferenceCommand(), DerivativeCommand(), + PowerSpectrumCommand()] + self._setup_commands() # Define common or complicated arguments diff --git a/hooke/plugin/cut.py b/hooke/plugin/cut.py index f3c13c9..7a2e5ea 100644 --- a/hooke/plugin/cut.py +++ b/hooke/plugin/cut.py @@ -29,9 +29,8 @@ from ..plugin import Plugin class CutPlugin (Plugin): def __init__(self): super(CutPlugin, self).__init__(name='cut') - - def commands(self): - return [CutCommand()] + self._commands = [CutCommand()] + self._setup_commands() # Define common or complicated arguments diff --git a/hooke/plugin/debug.py b/hooke/plugin/debug.py index a786af0..cc9101d 100644 --- a/hooke/plugin/debug.py +++ b/hooke/plugin/debug.py @@ -32,9 +32,8 @@ from ..plugin import Builtin class DebugPlugin (Builtin): def __init__(self): super(DebugPlugin, self).__init__(name='debug') - - def commands(self): - return [VersionCommand(), DebugCommand()] + self._commands = [VersionCommand(), DebugCommand()] + self._setup_commands() class VersionCommand (Command): diff --git a/hooke/plugin/multifit.py b/hooke/plugin/multifit.py index 986b781..ee42ea5 100644 --- a/hooke/plugin/multifit.py +++ b/hooke/plugin/multifit.py @@ -31,9 +31,9 @@ from ..plugin.playlist import PlaylistArgument class MultifitPlugin (Plugin): def __init__(self): super(MultifitPlugin, self).__init__(name='multifit') + self._commands = [MultifitCommand()] + self._setup_commands() - def commands(self): - return [MultifitCommand()] class MultifitCommand (Command): """Presents curves for interactive manual analysis. diff --git a/hooke/plugin/note.py b/hooke/plugin/note.py index 168cbd3..722b208 100644 --- a/hooke/plugin/note.py +++ b/hooke/plugin/note.py @@ -30,14 +30,9 @@ from ..plugin.playlist import current_playlist_callback class NotePlugin (Builtin): def __init__(self): super(NotePlugin, self).__init__(name='note') - - def commands(self): - return [AddNoteCommand(), ClearNoteCommand(), GetNoteCommand()] - - def dependencies(self): - return [ - 'playlist', # for current_playlist_callback - ] + self._commands = [ + AddNoteCommand(), ClearNoteCommand(), GetNoteCommand()] + self._setup_commands() class AddNoteCommand (Command): diff --git a/hooke/plugin/playlist.py b/hooke/plugin/playlist.py index 371cd1e..47975f4 100644 --- a/hooke/plugin/playlist.py +++ b/hooke/plugin/playlist.py @@ -31,13 +31,13 @@ from ..plugin import Builtin class PlaylistPlugin (Builtin): def __init__(self): super(PlaylistPlugin, self).__init__(name='playlist') - - def commands(self): - return [NextCommand(), PreviousCommand(), JumpCommand(), - IndexCommand(), CurveListCommand(), - SaveCommand(), LoadCommand(), - AddCommand(), AddGlobCommand(), - RemoveCommand(), FilterCommand(), NoteFilterCommand()] + self._commands = [ + NextCommand(), PreviousCommand(), JumpCommand(), + IndexCommand(), CurveListCommand(), + SaveCommand(), LoadCommand(), + AddCommand(), AddGlobCommand(), + RemoveCommand(), FilterCommand(), NoteFilterCommand()] + self._setup_commands() # Define common or complicated arguments diff --git a/hooke/plugin/system.py b/hooke/plugin/system.py index 42e9cbe..185fa8e 100644 --- a/hooke/plugin/system.py +++ b/hooke/plugin/system.py @@ -33,10 +33,10 @@ from ..plugin import Builtin class SystemPlugin (Builtin): def __init__(self): super(SystemPlugin, self).__init__(name='system') - - def commands(self): - return [ListDirectoryCommand(), GetWorkingDirectoryCommand(), - ChangeDirectoryCommand(), SystemCommand()] + self._commands = [ + ListDirectoryCommand(), GetWorkingDirectoryCommand(), + ChangeDirectoryCommand(), SystemCommand()] + self._setup_commands() class ListDirectoryCommand (Command): -- 2.26.2