Esempio n. 1
0
    def importPluginClass(self, pluginName):
        """Import a plugin module by its name

        Args:
            pluginName (str): The name of the plugin

        Returns:
            obj:  An instance of the plubin
        """

        # If plugin is already loaded, return reference
        if pluginName in sys.modules:
            return sys.modules[pluginName]

        # Next, try to load plugin from an egg
        for module in iter_entry_points(group='PI.MicroView.MicroViewPlugIn.v1', name=pluginName):

            # Load module
            try:
                _class = module.load()
                return _class
            except:
                logging.exception("PluginManager")
                return None

        logging.error("Can't find plugin %s" % pluginName)

        return None
def LoadImageReaders(reader=None, directories=['.'], cache=True):

    if reader is None:
        reader = vtkMultiImageReader.vtkMultiImageReader()

    # Make directory entries absolute
    for i in range(len(directories)):
        directories[i] = os.path.abspath(directories[i])

    PluginHelper.SetupPlugins(directories, cache=cache)

    for module in iter_entry_points(group='PI.vtk.ImageReader', name=None):

        try:

            # Load module
            _class = module.load()

            if vtkImageReaderBase.WHOLE_FILENAME & _class.__capabilities__:
                reader.registerWholeFileName(
                    _class.__extensions__, _class, _class.__capabilities__)
            else:
                reader.registerFileType(
                    _class.__extensions__, _class, _class.__magic__, _class.__capabilities__)

        except:
            logging.exception("vtkLoadReaders")

    return reader, reader.GetMatchingFormatStrings()
def GetImageReaderByClassName(classname, directories=['.']):
    """
    Get an instance of a specific reader, specified by classname
    """

    global _plugin_cache

    # Make directory entries absolute
    for i in range(len(directories)):
        directories[i] = os.path.abspath(directories[i])

    if _plugin_cache:
        distributions, errors = _plugin_cache
    else:
        distributions, errors = _plugin_cache = working_set.find_plugins(
            Environment(directories))

    for dist in distributions:
        working_set.add(dist)

    for module in iter_entry_points(group='PI.vtk.ImageReader'):

        if module.module_name.endswith(classname):
            # Load module
            _class = module.load()
            return _class()

    logging.error(
        "Unable to find plugin that contains a '%s' reader!!" % classname)
def LoadGeometryReaders(reader=None, directories=['.'], cache=True):

    if reader is None:
        reader = vtkMultiPolyDataReader.vtkMultiPolyDataReader()

    # Make directory entries absolute
    for i in range(len(directories)):
        directories[i] = os.path.abspath(directories[i])

    PluginHelper.SetupPlugins(directories)

    for module in iter_entry_points(group='PI.vtk.PolyDataReader', name=None):

        # Load module
        _class = module.load()

        reader.registerFileType(_class.__extensions__, _class)

    return reader, reader.GetMatchingFormatStrings()
Esempio n. 5
0
    def LoadPlugins(self):
        """Load all available plugins"""

        # some plugins may request keybindings that should be mapped to them - we'll instantiate plugins
        # and reroute keyboard events there as needed

        self._keybindings = {}
        gsm = component.getGlobalSiteManager()

        # iterate over all plugins
        for module in iter_entry_points(group='PI.MicroView.MicroViewPlugIn.v1'):

            # Load module
            try:
                plugin_class = module.load()
            except:
                logging.exception("PluginManager")
                continue

            # does this class provide a standard ROI interface?
            if IStandardROIProvider.implementedBy(plugin_class):
                self._standard_roi_plugins.append(plugin_class)

            # does this class provide a generic ROI interface?
            if IROIProvider.implementedBy(plugin_class):
                # register the plugin with zope as an ROI provider too (prior to loading plugin)
                gsm.registerUtility(
                    plugin_class, IROIProvider, name=plugin_class.GetShortName())

            # bind a menu entry to this plugin
            menu = []

            m = plugin_class.GetMenuEntry()
            if m:
                menu = m.split('|')
            else:
                menu.append('Plugins')
                menu.append(plugin_class.GetClassName().replace('...', ''))

            sep_front = sep_back = 0
            if menu[0] == '':
                sep_front = 1
                menu = menu[1:]
            if menu[-1] == '':
                sep_back = 1
                menu = menu[:-1]
            if sep_front == 1:
                m = self._menubar.GetMenu(self._menubar.FindMenu(menu[0]))
                m.AppendSeparator()
            if len(menu) > 2:
                try:
                    self._menubar.addcascademenu(menu[0], menu[1], '')
                except ValueError:
                    pass
                menu = menu[1:]

            try:
                button_image = plugin_class.GetToolbarBitmap()
                menu_image = plugin_class.GetMenuBitmap()
            except:
                logging.exception("PluginManager")

            # Find menu, create ID, create menu item, add optional icon
            m = self._menubar.GetMenu(self._menubar.FindMenu(menu[0]))
            _id = wx.NewId()
            item = wx.MenuItem(m, _id, menu[
                               1] + '...', plugin_class.GetDescription())
            if menu_image:
                item.SetBitmap(menu_image)
            m.AppendItem(item)

            # Wire up callback for this menu item
            self.GetTopLevelParent().Bind(
                wx.EVT_MENU, lambda event, i=plugin_class.GetShortName(), s=self: s.ActivatePlugin(i), id=_id)

            if sep_back == 1:
                m = self._menubar.GetMenu(self._menubar.FindMenu(menu[0]))
                m.AppendSeparator()

            managergroup = plugin_class.GetManagerGroup()

            if managergroup:
                group = getattr(self, managergroup + 'Group')
                sizer = getattr(self, managergroup + 'Sizer')

                # create Button
                # TODO: convert to a bitmap button
                _id = wx.NewId()
                if button_image is None:
                    button_image = wx.EmptyBitmap(16, 16)

                button = wx.Button(
                    group, id=_id, label=menu[1], style=wx.BU_LEFT)
                button.SetBitmap(button_image)

                sizer.Add(button, 0, wx.EXPAND)
                self.Bind(wx.EVT_BUTTON, lambda event, s=self,
                          i=plugin_class.GetShortName(): s.ActivatePlugin(i), button)