~adamblackburn/wicd/plugin

« back to all changes in this revision

Viewing changes to plugin.py

  • Committer: Adam Blackburn
  • Date: 2009-03-30 02:16:00 UTC
  • Revision ID: compwiz18@gmail.com-20090330021600-xojza0qgy5hlbr4g
Refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
# borrowed from PyDispatcher
9
9
import robustapply
10
10
 
 
11
def activate_plugin(func, *args, **kwargs):
 
12
    ''' Activates a plugin using robustapply. '''
 
13
    return robustapply.robustApply(func, *args, **kwargs)
 
14
 
11
15
# Plugin Managers
12
16
# These control where and how the plugins are loaded
13
17
 
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)}
86
90
 
87
91
# AllActivationManager
88
92
# activates all of the plugins in order
92
96
        ''' Activates all of the loaded plugins. '''
93
97
        return_values = {}
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
97
101
 
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)
 
112
            max([1, 2, 3])
108
113
            if not retval:
109
114
                return False
110
115
        return True
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)
124
129
 
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)}
138
141
 
139
142
# ExtensionPoint
140
143
# Glues the PluginManager and the ActivationManager together
148
151
        self.default_kwargs = {}
149
152
 
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
153
157