class PluginController: def __init__(self, parent): self.plugin_handler = PluginHandler(parent, __MODULE_DIR__ \ , 'data_loaders') self.parent = parent self.plugin_handler.load_plugin('default') def load_default(self): try: plugin_name = self.parent.config.get('data handling', 'data loader') self.LoadPlugin(plugin_name) except Exception, S: print 'Could not locate the data loader parameter or the data loader. Error:' print S.__str__() print 'Proceeding with laoding the default data loader.' self.LoadPlugin('default')
def __init__(self, parent, menu, config): '''__init__(self, parent, menu) --> None Insert menu items for controlling plugins in menu. Parent is the main window. ''' self.plugin_handler = PluginHandler(parent, __MODULE_DIR__ \ , 'add_ons') self.parent = parent self.config = config # make the menus self.load_menu = wx.Menu() menu.InsertMenu(0, -1,'Load', self.load_menu, 'Load a plugin') self.unload_menu = wx.Menu() menu.InsertMenu(1, -1,'Unload', self.unload_menu, 'Unload a plugin') menu.Append(-1, 'Update module list') self.LoadDefaultPlugins()
class PluginController: def __init__(self, parent): self.plugin_handler = PluginHandler(parent, __MODULE_DIR__ \ , 'data_loaders') self.parent = parent self.plugin_handler.load_plugin('default') def LoadPlugin(self, plugin): '''LoadPlugin(self, plugin) --> None Loads a data handler note that there is no UnLoad function since only one DataHandler can be plugged in at a time. ''' # Unload the plugins names = self.plugin_handler.loaded_plugins.copy() try: [self.plugin_handler.unload_plugin(pl) for pl in names] self.parent.SetStatusText('Unloaded data loader %s'%names.keys()[0]) except: outp = StringIO.StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT unload plugin object'+ \ names.keys()[0] + '\nPython traceback below:\n\n' + tbtext) try: self.plugin_handler.load_plugin(plugin) self.parent.SetStatusText('Loaded data loader: %s'%plugin) except: outp = StringIO.StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT load plugin ' + plugin\ + '\nPython traceback below:\n\n' + tbtext) def ShowDialog(self): '''ShowDialog(self) --> None Shows a dialog boc for the user to choose a data loader. ''' cur_plugin = self.plugin_handler.loaded_plugins.keys()[0] plugin_list = self.plugin_handler.get_possible_plugins() dlg = PluginDialog(self.parent, plugin_list, cur_plugin,\ self.LoadPlugin) dlg.ShowModal() dlg.Destroy()
class PluginController: ''' A controller class to interact with the gui so we can load and unload modules as well as update the module list. ''' def __init__(self, parent, menu, config): '''__init__(self, parent, menu) --> None Insert menu items for controlling plugins in menu. Parent is the main window. ''' self.plugin_handler = PluginHandler(parent, __MODULE_DIR__ \ , 'add_ons') self.parent = parent self.config = config # make the menus self.load_menu = wx.Menu() menu.InsertMenu(0, -1,'Load', self.load_menu, 'Load a plugin') self.unload_menu = wx.Menu() menu.InsertMenu(1, -1,'Unload', self.unload_menu, 'Unload a plugin') menu.Append(-1, 'Update module list') self.LoadDefaultPlugins() #self.update_plugins() def update_plugins(self): '''update_modules(self) --> None Updates the list of modules that can be loaded. ''' # Remove all the items in load_menu items = self.load_menu.GetMenuItems() [self.load_menu.DeleteItem(item) for item in items] # Get the new list of plugin modules modlist = self.plugin_handler.get_plugins() modlist.sort() # Add new menu items for mod in modlist: menu = self.load_menu.Append(-1, mod) self.parent.Bind(wx.EVT_MENU, self.LoadPlugin, menu) self.update_config() def RegisterPlugin(self, plugin): ''' RegisterPlugin(self, plugin) --> None Adds a plugin to the unload list so that it can be removed later. ''' menu = self.unload_menu.Append(-1, plugin) self.parent.Bind(wx.EVT_MENU, self.UnLoadPlugin, menu) self.update_plugins() def update_config(self): '''update_config(self) --> None Updates the config object ''' loaded_plugins = self.plugin_handler.get_loaded_plugins() plugins_str = ';'.join(loaded_plugins) self.config.set('plugins', 'loaded plugins', plugins_str) def LoadDefaultPlugins(self): '''LoadDefaultPlugins(self) --> None Tries to load the default plugins from the config object if they are not already loaded. ''' plugin_str = self.config.get('plugins', 'loaded plugins') #print' plugins:', plugin_str # Check so we have any plugins to load else bail out #print 'Plugin string:', plugin_str if plugin_str == '': self.update_plugins() return existing_plugins = self.plugin_handler.get_possible_plugins() for plugin in plugin_str.split(';'): # Check so the plugin is not loaded and exists if not self.plugin_handler.is_loaded(plugin): if plugin in existing_plugins: try: self.plugin_handler.load_plugin(plugin) self.RegisterPlugin(plugin) except: outp = StringIO.StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT load plugin '\ + plugin + '\nPython traceback below:\n\n' + tbtext) self.RegisterPlugin(plugin) else: ShowInfoDialog(self.parent, 'Could not find plugin "%s"'\ '. Either there is an error in the config file'\ ' or the plugin is not installed.'%(plugin)) self.update_plugins() # Callbacks def LoadPlugin(self, event): '''OnLoadPlugin(self, event) --> None Loads a plugin from a menu choice. ''' # Get the name of the plugin menuitem = self.load_menu.FindItemById(event.GetId()) plugin = menuitem.GetText() try: self.plugin_handler.load_plugin(plugin) except: outp = StringIO.StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT load plugin ' + plugin\ + '\nPython traceback below:\n\n' + tbtext) else: self.RegisterPlugin(plugin) def UnLoadPlugin(self, event): '''UnLoadPlugin(self, event) --> None UnLoads (removes) a plugin module. ''' menuitem = self.unload_menu.FindItemById(event.GetId()) plugin = menuitem.GetText() try: self.plugin_handler.unload_plugin(plugin) except Exception, e: outp = StringIO.StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT unload plugin object'+ \ plugin + '\nPython traceback below:\n\n' + tbtext) else:
def __init__(self, parent): self.plugin_handler = PluginHandler(parent, __MODULE_DIR__ \ , 'data_loaders') self.parent = parent self.plugin_handler.load_plugin('default')