Exemple #1
0
 def load(self):
     if os.path.isfile(self.FILE_PATH):
         fd = open(self.FILE_PATH, 'rb')
         try:
             self.data = pickle.load(fd)
             fd.close()
         except:
             fd.close()
             try:
                 import shelve
                 s = shelve.open(self.FILE_PATH)
                 for (k, v) in s.items():
                     self.data[k] = v
                 if not isinstance(self.data, dict):
                     raise GajimPluginException
                 s.close()
                 self.save()
             except:
                 log.warning('%s plugin config file not readable. Saving it as '
                     '%s and creating a new one' % (self.plugin.short_name,
                     self.FILE_PATH + '.bak'))
                 if os.path.exists(self.FILE_PATH + '.bak'):
                     os.remove(self.FILE_PATH + '.bak')
                 os.rename(self.FILE_PATH, self.FILE_PATH + '.bak')
                 self.data = {}
                 self.save()
     else:
         self.data = {}
         self.save()
Exemple #2
0
    def deactivate_plugin(self, plugin):
        # remove GUI extension points handlers (provided by plug-in) from
        # handlers list
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.items():
            self.gui_extension_points_handlers[gui_extpoint_name].remove(
                gui_extpoint_handlers)

        # detaching plug-in from handler GUI extension points (calling
        # cleaning up method that must be provided by plug-in developer
        # for each handled GUI extension point)
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.items():
            if gui_extpoint_name in self.gui_extension_points:
                for gui_extension_point_args in self.gui_extension_points[
                gui_extpoint_name]:
                    handler = gui_extpoint_handlers[1]
                    if handler:
                        try:
                            handler(*gui_extension_point_args)
                        except Exception as e:
                            log.warning('Error executing %s', handler,
                                exc_info=True)

        self._remove_events_handler_from_ged(plugin)
        self._remove_network_events_from_nec(plugin)

        # removing plug-in from active plug-ins list
        plugin.deactivate()
        self.active_plugins.remove(plugin)
        self._set_plugin_active_in_global_config(plugin, False)
        plugin.active = False
Exemple #3
0
 def load(self):
     if os.path.isfile(self.FILE_PATH):
         fd = open(self.FILE_PATH, 'rb')
         try:
             self.data = pickle.load(fd)
             fd.close()
         except:
             fd.close()
             try:
                 import shelve
                 s = shelve.open(self.FILE_PATH)
                 for (k, v) in s.items():
                     self.data[k] = v
                 if not isinstance(self.data, dict):
                     raise GajimPluginException
                 s.close()
                 self.save()
             except:
                 log.warning(
                     '%s plugin config file not readable. Saving it as '
                     '%s and creating a new one' %
                     (self.plugin.short_name,
                      self.FILE_PATH.decode(locale.getpreferredencoding()) +
                      '.bak'))
                 if os.path.exists(self.FILE_PATH + '.bak'):
                     os.remove(self.FILE_PATH + '.bak')
                 os.rename(self.FILE_PATH, self.FILE_PATH + '.bak')
                 self.data = {}
                 self.save()
     else:
         self.data = {}
         self.save()
Exemple #4
0
    def deactivate_plugin(self, plugin):
        # remove GUI extension points handlers (provided by plug-in) from
        # handlers list
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.items():
            self.gui_extension_points_handlers[gui_extpoint_name].remove(
                gui_extpoint_handlers)

        # detaching plug-in from handler GUI extension points (calling
        # cleaning up method that must be provided by plug-in developer
        # for each handled GUI extension point)
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.items():
            if gui_extpoint_name in self.gui_extension_points:
                for gui_extension_point_args in self.gui_extension_points[
                        gui_extpoint_name]:
                    handler = gui_extpoint_handlers[1]
                    if handler:
                        try:
                            handler(*gui_extension_point_args)
                        except Exception as e:
                            log.warning('Error executing %s',
                                        handler,
                                        exc_info=True)

        self._remove_events_handler_from_ged(plugin)
        self._remove_network_events_from_nec(plugin)

        # removing plug-in from active plug-ins list
        plugin.deactivate()
        self.active_plugins.remove(plugin)
        self._set_plugin_active_in_global_config(plugin, False)
        plugin.active = False
Exemple #5
0
 def _execute_all_handlers_of_gui_extension_point(self, gui_extpoint_name,
 *args):
     if gui_extpoint_name in self.gui_extension_points_handlers:
         for handlers in self.gui_extension_points_handlers[
         gui_extpoint_name]:
             try:
                 handlers[0](*args)
             except Exception as e:
                 log.warning('Error executing %s', handlers[0],
                     exc_info=True)
Exemple #6
0
 def _execute_all_handlers_of_gui_extension_point(self, gui_extpoint_name,
 *args):
     if gui_extpoint_name in self.gui_extension_points_handlers:
         for handlers in self.gui_extension_points_handlers[
         gui_extpoint_name]:
             try:
                 handlers[0](*args)
             except Exception as e:
                 log.warning('Error executing %s', handlers[0],
                     exc_info=True)
Exemple #7
0
    def inject(self, msg, appdata=None):
        log.warning('inject(appdata=%s)', appdata)
        msg = unicode(msg)
        account = self.user.accountname

        stanza = common.xmpp.Message(to=self.peer, body=msg, typ='chat')
        if appdata and 'session' in appdata:
            session = appdata['session']
            stanza.setThread(session.thread_id)
        gajim.connections[account].connection.send(stanza, now=True)
        return
Exemple #8
0
 def _handle_all_gui_extension_points_with_plugin(self, plugin):
     for gui_extpoint_name, gui_extpoint_handlers in \
     plugin.gui_extension_points.items():
         if gui_extpoint_name in self.gui_extension_points:
             for gui_extension_point_args in self.gui_extension_points[
             gui_extpoint_name]:
                 handler = gui_extpoint_handlers[0]
                 if handler:
                     try:
                         handler(*gui_extension_point_args)
                     except Exception as e:
                         log.warning('Error executing %s', handler,
                             exc_info=True)
Exemple #9
0
 def _handle_all_gui_extension_points_with_plugin(self, plugin):
     for gui_extpoint_name, gui_extpoint_handlers in \
     plugin.gui_extension_points.items():
         if gui_extpoint_name in self.gui_extension_points:
             for gui_extension_point_args in self.gui_extension_points[
             gui_extpoint_name]:
                 handler = gui_extpoint_handlers[0]
                 if handler:
                     try:
                         handler(*gui_extension_point_args)
                     except Exception as e:
                         log.warning('Error executing %s', handler,
                             exc_info=True)
Exemple #10
0
    def deactivate_plugin(self, plugin):
        # remove GUI extension points handlers (provided by plug-in) from
        # handlers list
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.iteritems():
            self.gui_extension_points_handlers[gui_extpoint_name].remove(
                gui_extpoint_handlers)

        # detaching plug-in from handler GUI extension points (calling
        # cleaning up method that must be provided by plug-in developer
        # for each handled GUI extension point)
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.iteritems():
            if gui_extpoint_name in self.gui_extension_points:
                for gui_extension_point_args in self.gui_extension_points[
                gui_extpoint_name]:
                    handler = gui_extpoint_handlers[1]
                    if handler:
                        try:
                            handler(*gui_extension_point_args)
                        except Exception, e:
                            log.warning('Error executing %s', handler,
                                exc_info=True)
    def deactivate_plugin(self, plugin):
        # remove GUI extension points handlers (provided by plug-in) from
        # handlers list
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.iteritems():
            self.gui_extension_points_handlers[gui_extpoint_name].remove(
                gui_extpoint_handlers)

        # detaching plug-in from handler GUI extension points (calling
        # cleaning up method that must be provided by plug-in developer
        # for each handled GUI extension point)
        for gui_extpoint_name, gui_extpoint_handlers in \
        plugin.gui_extension_points.iteritems():
            if gui_extpoint_name in self.gui_extension_points:
                for gui_extension_point_args in self.gui_extension_points[
                        gui_extpoint_name]:
                    handler = gui_extpoint_handlers[1]
                    if handler:
                        try:
                            handler(*gui_extension_point_args)
                        except Exception, e:
                            log.warning('Error executing %s',
                                        handler,
                                        exc_info=True)
Exemple #12
0
 def getPolicy(self, key):
     jid = gajim.get_room_and_nick_from_fjid(self.peer)[0]
     ret = self.user.plugin.get_flags(self.user.accountname, jid)[key]
     log.warning('getPolicy(key=%s) = %s', key, ret)
     return ret
Exemple #13
0
    def scan_dir_for_plugins(path, scan_dirs=True):
        r'''
        Scans given directory for plugin classes.

        :param path: directory to scan for plugins
        :type path: str

        :return: list of found plugin classes (subclasses of `GajimPlugin`
        :rtype: [] of class objects

        :note: currently it only searches for plugin classes in '\*.py' files
                present in given direcotory `path` (no recursion here)

        :todo: add scanning packages
        :todo: add scanning zipped modules
        '''
        from plugins.plugins_i18n import _
        plugins_found = []
        conf = configparser.ConfigParser()
        fields = ('name', 'short_name', 'version', 'description', 'authors',
                  'homepage')
        if not os.path.isdir(path):
            return plugins_found

        dir_list = os.listdir(path)

        sys.path.insert(0, path)

        for elem_name in dir_list:
            file_path = os.path.join(path, elem_name)

            if os.path.isfile(file_path) and fnmatch.fnmatch(
                    file_path, '*.py'):
                module_name = os.path.splitext(elem_name)[0]
            elif os.path.isdir(file_path) and scan_dirs:
                module_name = elem_name
                file_path += os.path.sep

            manifest_path = os.path.join(os.path.dirname(file_path),
                                         'manifest.ini')
            if scan_dirs and (not os.path.isfile(manifest_path)):
                continue

            # read metadata from manifest.ini
            conf.remove_section('info')
            with open(manifest_path, encoding='utf-8') as conf_file:
                try:
                    conf.read_file(conf_file)
                except configparser.Error:
                    log.warning(("Plugin {plugin} not loaded, error loading"
                                 " manifest").format(plugin=elem_name),
                                exc_info=True)
                    continue

            min_v = conf.get('info', 'min_gajim_version', fallback=None)
            max_v = conf.get('info', 'max_gajim_version', fallback=None)

            gajim_v = gajim.config.get('version').split('-', 1)[0]
            gajim_v_cmp = parse_version(gajim_v)

            if min_v and gajim_v_cmp < parse_version(min_v):
                log.warning(('Plugin {plugin} not loaded, newer version of'
                             'gajim required: {gajim_v} < {min_v}').format(
                                 plugin=elem_name,
                                 gajim_v=gajim_v,
                                 min_v=min_v))
                continue
            if max_v and gajim_v_cmp > parse_version(max_v):
                log.warning(('Plugin {plugin} not loaded, plugin incompatible '
                             'with current version of gajim: '
                             '{gajim_v} > {max_v}').format(plugin=elem_name,
                                                           gajim_v=gajim_v,
                                                           max_v=max_v))
                continue

            module = None

            if module_name in sys.modules:
                # do not load the module twice
                continue
            try:
                module = __import__(module_name)
            except Exception as error:
                log.warning(
                    "While trying to load {plugin}, exception occurred".format(
                        plugin=elem_name),
                    exc_info=sys.exc_info())
                continue

            if module is None:
                continue

            log.debug('Attributes processing started')
            for module_attr_name in [
                    attr_name for attr_name in dir(module) if not (
                        attr_name.startswith('__') or attr_name.endswith('__'))
            ]:
                module_attr = getattr(module, module_attr_name)
                log.debug('%s : %s' % (module_attr_name, module_attr))

                try:
                    if not issubclass(module_attr, GajimPlugin) or \
                    module_attr is GajimPlugin:
                        continue
                    log.debug('is subclass of GajimPlugin')
                    module_attr.__path__ = os.path.abspath(
                        os.path.dirname(file_path))

                    for option in fields:
                        if conf.get('info', option) is '':
                            raise configparser.NoOptionError('field empty')
                        if option == 'description':
                            setattr(module_attr, option,
                                    _(conf.get('info', option)))
                            continue
                        setattr(module_attr, option, conf.get('info', option))

                    plugins_found.append(module_attr)
                except TypeError:
                    # set plugin localization
                    try:
                        module_attr._ = _
                    except AttributeError:
                        pass
                except configparser.NoOptionError:
                    # all fields are required
                    log.debug(
                        '%s : %s' %
                        (module_attr_name,
                         'wrong manifest file. all fields are required!'))
                except configparser.NoSectionError:
                    # info section are required
                    log.debug(
                        '%s : %s' %
                        (module_attr_name,
                         'wrong manifest file. info section are required!'))
                except configparser.MissingSectionHeaderError:
                    # info section are required
                    log.debug('%s : %s' %
                              (module_attr_name,
                               'wrong manifest file. section are required!'))

        return plugins_found
Exemple #14
0
    def scan_dir_for_plugins(path, scan_dirs=True):
        '''
        Scans given directory for plugin classes.

        :param path: directory to scan for plugins
        :type path: str

        :return: list of found plugin classes (subclasses of `GajimPlugin`
        :rtype: [] of class objects

        :note: currently it only searches for plugin classes in '\*.py' files
                present in given direcotory `path` (no recursion here)

        :todo: add scanning packages
        :todo: add scanning zipped modules
        '''
        from plugins.plugins_i18n import _
        plugins_found = []
        conf = configparser.ConfigParser()
        fields = ('name', 'short_name', 'version', 'description', 'authors',
            'homepage')
        if not os.path.isdir(path):
            return plugins_found

        dir_list = os.listdir(path)

        sys.path.insert(0, path)

        for elem_name in dir_list:
            file_path = os.path.join(path, elem_name)

            if os.path.isfile(file_path) and fnmatch.fnmatch(file_path, '*.py'):
                module_name = os.path.splitext(elem_name)[0]
            elif os.path.isdir(file_path) and scan_dirs:
                module_name = elem_name
                file_path += os.path.sep

            manifest_path = os.path.join(os.path.dirname(file_path),
                'manifest.ini')
            if scan_dirs and (not os.path.isfile(manifest_path)):
                continue

            # read metadata from manifest.ini
            conf.remove_section('info')
            conf_file = open(manifest_path)
            conf.read_file(conf_file)
            conf_file.close()

            min_v = conf.get('info', 'min_gajim_version', fallback=None)
            max_v = conf.get('info', 'max_gajim_version', fallback=None)

            gajim_v = gajim.config.get('version').split('-', 1)[0]
            gajim_v_cmp = parse_version(gajim_v)

            if min_v and gajim_v_cmp < parse_version(min_v):
                log.warning(('Plugin {plugin} not loaded, newer version of'
                             'gajim required: {gajim_v} < {min_v}').format(
                                 plugin=elem_name,
                                 gajim_v=gajim_v,
                                 min_v=min_v
                           ))
                continue
            if max_v and gajim_v_cmp > parse_version(max_v):
                log.warning(('Plugin {plugin} not loaded, plugin incompatible '
                             'with current version of gajim: '
                             '{gajim_v} > {max_v}').format(
                                 plugin=elem_name,
                                 gajim_v=gajim_v,
                                 max_v=max_v
                           ))
                continue

            module = None

            if module_name in sys.modules:
            # do not load the module twice
                continue
            try:
                module = __import__(module_name)
            except Exception as error:
                log.warning(
                    "While trying to load {plugin}, exception occurred".format(plugin=elem_name),
                    exc_info=sys.exc_info()
                )
                continue

            if module is None:
                continue

            log.debug('Attributes processing started')
            for module_attr_name in [attr_name for attr_name in dir(module)
            if not (attr_name.startswith('__') or attr_name.endswith('__'))]:
                module_attr = getattr(module, module_attr_name)
                log.debug('%s : %s' % (module_attr_name, module_attr))

                try:
                    if not issubclass(module_attr, GajimPlugin) or \
                    module_attr is GajimPlugin:
                        continue
                    log.debug('is subclass of GajimPlugin')
                    module_attr.__path__ = os.path.abspath(
                        os.path.dirname(file_path))

                    for option in fields:
                        if conf.get('info', option) is '':
                            raise configparser.NoOptionError('field empty')
                        if option == 'description':
                            setattr(module_attr, option, _(conf.get('info', option)))
                            continue
                        setattr(module_attr, option, conf.get('info', option))

                    plugins_found.append(module_attr)
                except TypeError:
                    # set plugin localization
                    try:
                        module_attr._ = _
                    except AttributeError:
                        pass
                except configparser.NoOptionError:
                    # all fields are required
                    log.debug('%s : %s' % (module_attr_name,
                        'wrong manifest file. all fields are required!'))
                except configparser.NoSectionError:
                    # info section are required
                    log.debug('%s : %s' % (module_attr_name,
                        'wrong manifest file. info section are required!'))
                except configparser.MissingSectionHeaderError:
                    # info section are required
                    log.debug('%s : %s' % (module_attr_name,
                        'wrong manifest file. section are required!'))

        return plugins_found