8
8
# borrowed from PyDispatcher
11
def activate_plugin(func, *args, **kwargs):
12
''' Activates a plugin using robustapply. '''
13
return robustapply.robustApply(func, *args, **kwargs)
12
16
# These control where and how the plugins are loaded
82
86
def activate(self, *args, **kwargs):
83
87
''' Activates the first plugin. '''
84
88
func = self.plugins[0]
85
return {func : robustapply.robustApply(func, *args, **kwargs)}
89
return {func : activate_plugin(func, *args, **kwargs)}
87
91
# AllActivationManager
88
92
# activates all of the plugins in order
92
96
''' Activates all of the loaded plugins. '''
94
98
for func in self.plugins:
95
return_values[func] = robustapply.robustApply(func, *args, **kwargs)
99
return_values[func] = activate_plugin(func, *args, **kwargs)
96
100
return return_values
98
102
# CancelOnFalseActivationManager
104
108
''' Activates plugins until all are activated or one returns False. '''
105
109
return_values = {}
106
110
for func in self.plugins:
107
retval = robustapply.robustApply(func, *args, **kwargs)
111
retval = activate_plugin(func, *args, **kwargs)
114
119
# otherwise will use the first plugin
115
120
class UseSavedOrDefaultActivationManager(ActivationManager):
116
121
''' Activate a plugin stored in a configuration file or the default. '''
117
def __init__(self, name, default):
122
def __init__(self, config_path, name, default):
118
123
''' Uses the plugin stored in 'config', name otherwise default. '''
119
124
super(UseSavedOrDefaultActivationManager, self).__init__()
120
125
# FIXME: the path below
121
saved_path = os.path.abspath('./config/plugins.config')
126
saved_path = os.path.abspath(config_path)
122
127
config = configmanager.ConfigManager(saved_path)
123
128
self.default = config.get_option('plugins', name, default=default)
125
130
def activate(self, *args, **kwargs):
126
131
''' Activates the decided plugin. '''
127
def activate_plugin(func):
128
return {func : robustapply.robustApply(func, *args, **kwargs)}
129
for item in self.plugins:
132
for func in self.plugins:
130
133
# change the plugin's full path to
131
134
# simply the filename without .py
132
plugin_name = os.path.split(inspect.getfile(item))[1][:-3]
135
plugin_name = os.path.split(inspect.getfile(func))[1][:-3]
133
136
if plugin_name == self.default:
134
137
logging.debug('found saved/default plugin: %s', plugin_name)
135
return activate_plugin(item)
138
return {func : activate_plugin(func, *args, **kwargs)}
136
139
logging.warning('plugin %s not found, using [0]', self.default)
137
return activate_plugin(self.plugins[0])
140
return {self.plugins[0] : activate_plugin(self.plugins[0], *args, **kwargs)}
140
143
# Glues the PluginManager and the ActivationManager together
148
151
self.default_kwargs = {}
150
153
def set_default_args(self, *args, **kwargs):
154
''' Set values that are always passed when activate is called. '''
151
155
self.default_args = list(args)
152
156
self.default_kwargs = kwargs