Пример #1
0
    def _hamper_send(self, func, comm, message, encode, tag, vars, kwvars):
        if type(message) == str:
            log.warning('Warning, passing message as ascii instead of unicode '
                        'will cause problems. The message is: {0}'
                        .format(message))

        format_kwargs = {}
        format_kwargs.update(kwvars)
        format_kwargs.update(comm)
        try:
            message = message.format(*vars, **format_kwargs)
        except (ValueError, KeyError, IndexError) as e:
            log.error('Could not format message: {e}'.format(e=e))

        if encode:
            message = message.encode('utf8')

        if comm['pm']:
            func(comm['user'], message)
        else:
            func(comm['channel'], message)

        (self.factory.sent_messages
            .setdefault(comm['channel'], deque(maxlen=100))
            .append({
                'comm': comm,
                'message': message,
                'tag': tag,
            }))
Пример #2
0
    def loadAll(self):
        plugins_to_load = set()

        # Gather plugins
        for plugin in iter_entry_points(group='hamperbot.plugins', name=None):
            if plugin.name in self.config['plugins']:
                plugins_to_load.add(plugin.load())

        # Sort by priority, highest first
        plugins_to_load = sorted(plugins_to_load, key=lambda p: -p.priority)

        # Check dependencies and load plugins.
        for plugin_class in plugins_to_load:
            plugin_obj = plugin_class()
            if not self.dependencies_satisfied(plugin_obj):
                log.warning('Dependency not satisfied for {0}. Not loading.'
                            .format(plugin_class.__name__))
                continue
            log.info('Loading plugin {0}.'.format(plugin_class.__name__))
            plugin_obj.setup(self)
            self.plugins.append(plugin_obj)

        # Check for missing plugins
        plugin_names = {x.name for x in self.plugins}
        # Don't allow karma and karma_adv to be loaded at once
        if ('karma' in self.config['plugins'] and
                'karma_adv' in self.config['plugins']):
            quit(
                "Unable to load both karma and karma_adv at the same time")

        for pattern in self.config['plugins']:
            if pattern not in plugin_names:
                log.warning('Sorry, I couldn\'t find a plugin named "%s"',
                            pattern)
Пример #3
0
    def __init__(self, config):
        self.channels = config['channels']
        self.nickname = config['nickname']
        self.history = {}

        self.loader = PluginLoader()
        self.loader.config = config

        if 'db' in config:
            print('Loading db from config: ' + config['db'])
            db_engine = sqlalchemy.create_engine(config['db'])
        else:
            print('Using in-memory db')
            db_engine = sqlalchemy.create_engine('sqlite:///:memory:')
        DBSession = orm.sessionmaker(db_engine)
        session = DBSession()

        self.loader.db = DB(db_engine, session)

        # Load all plugins mentioned in the configuration. Allow globbing.
        config_matches = set()
        for plugin in getPlugins(BaseInterface, package=plugins):
            for pattern in config['plugins']:
                if fnmatch(plugin.name, pattern):
                    self.loader.registerPlugin(plugin)
                    config_matches.add(pattern)
                    break
        for pattern in config['plugins']:
            if pattern not in config_matches:
                log.warning('No plugin matched pattern "%s"', pattern)
Пример #4
0
    def loadAll(self):
        plugins_to_load = set()

        # Gather plugins
        for plugin in iter_entry_points(group='hamperbot.plugins', name=None):
            if plugin.name in self.config['plugins']:
                plugins_to_load.add(plugin.load())

        # Sort by priority, highest first
        plugins_to_load = sorted(plugins_to_load, key=lambda p: -p.priority)

        # Check dependencies and load plugins.
        for plugin_class in plugins_to_load:
            plugin_obj = plugin_class()
            if not self.dependencies_satisfied(plugin_obj):
                log.warning('Dependency not satisfied for {0}. Not loading.'
                            .format(plugin_class.__name__))
                continue
            log.info('Loading plugin {0}.'.format(plugin_class.__name__))
            plugin_obj.setup(self)
            self.plugins.append(plugin_obj)

        # Check for missing plugins
        plugin_names = {x.name for x in self.plugins}
        for pattern in self.config['plugins']:
            if pattern not in plugin_names:
                log.warning('Sorry, I couldn\'t find a plugin named "%s"',
                            pattern)
Пример #5
0
    def _hamper_send(self, func, comm, message, encode, tag, vars, kwvars):
        if type(message) == str:
            log.warning('Warning, passing message as ascii instead of unicode '
                        'will cause problems. The message is: {0}'
                        .format(message))

        format_kwargs = {}
        format_kwargs.update(kwvars)
        format_kwargs.update(comm)
        try:
            message = message.format(*vars, **format_kwargs)
        except (ValueError, KeyError, IndexError) as e:
            log.error('Could not format message: {e}'.format(e=e))

        if encode:
            message = message.encode('utf8')

        if comm['pm']:
            func(comm['user'], message)
        else:
            func(comm['channel'], message)

        (self.factory.sent_messages
            .setdefault(comm['channel'], deque(maxlen=100))
            .append({
                'comm': comm,
                'message': message,
                'tag': tag,
            }))
Пример #6
0
    def loadAll(self):
        """
        Find and load all plugins mentioned in config['plugins'].

        This will allow for globbing and external plugins. To load an
        external plugin, give an import path like

            base/plugin

        where base is a import path to a package, and plugin is the name
        of a plugin that can be found *in one of that package's modules.*

        In other words, if you have a package foo, and in that package
        a module bar, and in that module a plugin named baz, listing
        'foo/bar/baz' *WILL NOT WORK*.

        Instead, list 'foo/baz', since the importer will look for a
        module that contains a plugin name 'baz' in the package 'foo'.
        Twisted's plugin loader is weird.

        For confusion's sake, I recommend naming modules in packages
        after the plugin they contain. So in the last example, either
        rename the plugin to bar or rename the module to baz.
        """
        modules = [('', hamper.plugins)]
        for spec in self.config['plugins']:
            # if this is not a qualified name, `hamper.plugins` will cover it.
            if '/' not in spec:
                continue
            # Given something with some dots, get everything up to but
            # excluding the last dot.
            index = spec.rindex('/')
            base = spec[:index].replace('/', '.')

            modules.append((base + '/', importlib.import_module(base)))

        config_matches = set()
        for base, module in modules:
            for plugin in getPlugins(BaseInterface, module):
                for pattern in self.config['plugins']:
                    full_name = base + plugin.name
                    if fnmatch(full_name, pattern):
                        self.registerPlugin(plugin)
                        config_matches.add(pattern)
                        break

        for pattern in self.config['plugins']:
            if pattern not in config_matches:
                log.warning('No plugin loaded for "%s"', pattern)
Пример #7
0
    def loadAll(self):
        """
        Find and load all plugins mentioned in config['plugins'].

        This will allow for globbing and external plugins. To load an
        external plugin, give an import path like

            base/plugin

        where base is a import path to a package, and plugin is the name
        of a plugin that can be found *in one of that package's modules.*

        In other words, if you have a package foo, and in that package
        a module bar, and in that module a plugin named baz, listing
        'foo/bar/baz' *WILL NOT WORK*.

        Instead, list 'foo/baz', since the importer will look for a
        module that contains a plugin name 'baz' in the package 'foo'.
        Twisted's plugin loader is weird.

        For confusion's sake, I recommend naming modules in packages
        after the plugin they contain. So in the last example, either
        rename the plugin to bar or rename the module to baz.
        """
        modules = [('', hamper.plugins)]
        for spec in self.config['plugins']:
            # if this is not a qualified name, `hamper.plugins` will cover it.
            if '/' not in spec:
                continue
            # Given something with some dots, get everything up to but
            # excluding the last dot.
            index = spec.rindex('/')
            base = spec[:index].replace('/', '.')

            modules.append((base + '/', importlib.import_module(base)))

        config_matches = set()
        for base, module in modules:
            for plugin in getPlugins(BaseInterface, module):
                for pattern in self.config['plugins']:
                    full_name = base + plugin.name
                    if fnmatch(full_name, pattern):
                        self.registerPlugin(plugin)
                        config_matches.add(pattern)
                        break

        for pattern in self.config['plugins']:
            if pattern not in config_matches:
                log.warning('No plugin loaded for "%s"', pattern)
Пример #8
0
 def loadAll(self):
     for plugin in iter_entry_points(group='hamperbot.plugins', name=None):
         if plugin.name in self.config['plugins']:
             plugin_class = plugin.load()
             plugin_obj = plugin_class()
             if not self.dependencies_satisfied(plugin_obj):
                 # don't load the plugin.
                 continue
             self.plugins.add(plugin_obj)
             plugin_obj.setup(self)
     plugin_names = {x.name for x in self.plugins}
     for pattern in self.config['plugins']:
         if pattern not in plugin_names:
             log.warning('Sorry, I couldn\'t find a plugin named "%s"',
                         pattern)
Пример #9
0
 def loadAll(self):
     for plugin in iter_entry_points(group='hamperbot.plugins', name=None):
         if plugin.name in self.config['plugins']:
             plugin_class = plugin.load()
             plugin_obj = plugin_class()
             if not self.dependencies_satisfied(plugin_obj):
                 # don't load the plugin.
                 continue
             self.plugins.add(plugin_obj)
             plugin_obj.setup(self)
     plugin_names = {x.name for x in self.plugins}
     for pattern in self.config['plugins']:
         if pattern not in plugin_names:
             log.warning('Sorry, I couldn\'t find a plugin named "%s"',
                         pattern)
Пример #10
0
    def __init__(self, config):
        self.channels = [c.split(' ', 1) for c in config['channels']]
        self.nickname = config['nickname']
        self.password = config.get('password', None)
        self.history = {}
        acl_fname = config.get('acl', None)

        if acl_fname:
            # Bubble up an IOError if they passed a bad file
            with open(acl_fname, 'r') as acl_fd:
                self.acl = ACL(acl_fd.read())
        else:
            self.acl = AllowAllACL()

        self.loader = PluginLoader()
        self.loader.config = config

        if 'db' in config:
            print('Loading db from config: ' + config['db'])
            db_engine = sqlalchemy.create_engine(config['db'])
        else:
            print('Using in-memory db')
            db_engine = sqlalchemy.create_engine('sqlite:///:memory:')
        DBSession = orm.sessionmaker(db_engine)
        session = DBSession()

        self.loader.db = DB(db_engine, session)

        # Load all plugins mentioned in the configuration. Allow globbing.
        config_matches = set()
        for plugin in getPlugins(BaseInterface, package=plugins):
            for pattern in config['plugins']:
                if fnmatch(plugin.name, pattern):
                    self.loader.registerPlugin(plugin)
                    config_matches.add(pattern)
                    break
        for pattern in config['plugins']:
            if pattern not in config_matches:
                log.warning('No plugin matched pattern "%s"', pattern)