示例#1
0
class ExtensionRegistryTestCase(TestCase):

    def setUp(self):
        self.registry = ExtensionRegistry(mock_bot(), load=False)

    def test_make_import_args(self):
        ret = self.registry._make_import_args('foo.bar.baz')
        assert ret[0] == 'foo.bar.baz'
        assert ret[-1] == ['baz']

    def test_load_module_members_adds_subclass(self):
        class MyClass(HelgaExtension):
            pass

        fake_module = Mock()
        fake_module.MyClass = MyClass

        self.registry._get_possible_extensions = Mock(return_value=['MyClass'])
        self.registry.load_module_members(fake_module)

        assert len(self.registry.ext) == 1
        assert isinstance(self.registry.ext.pop(), MyClass)

    def test_dispatch_returns_none(self):
        assert self.registry.dispatch('foo', 'bar', 'baz', True) is None

    def mock_extension(self, retval):
        ext = Mock()
        ext.dispatch.return_value = retval
        return ext

    def test_dispatch_returns_a_message(self):
        mock_ext = self.mock_extension('foo')
        self.registry.ext = [mock_ext]

        assert self.registry.dispatch('foo', 'bar', 'baz', True) == 'foo'

    def test_dispatch_returns_first_message(self):
        ext1 = self.mock_extension(None)
        ext2 = self.mock_extension('foo')
        ext3 = self.mock_extension('bar')

        self.registry.ext = [ext1, ext2, ext3]

        assert self.registry.dispatch('foo', 'bar', 'baz', True) == 'foo'
        assert ext1.dispatch.called
        assert ext2.dispatch.called
        assert not ext3.dispatch.called

    def test_get_possible_extensions(self):
        class TestModule(object):
            def __dir__(self):
                return ['foo', '__bar__', '_baz', 'qux__']
    
        ret = self.registry._get_possible_extensions(TestModule())

        assert len(ret) == 3
        assert '__bar__' not in ret
 def setUp(self):
     self.registry = ExtensionRegistry(mock_bot(), load=False)
class ExtensionRegistryTestCase(TestCase):

    def setUp(self):
        self.registry = ExtensionRegistry(mock_bot(), load=False)

    def test_make_import_args_with_dots(self):
        ret = self.registry._make_import_args('foo.bar.baz')
        assert ret[0] == 'foo.bar.baz'
        assert ret[-1] == ['baz']

    def test_make_import_args_without_dots(self):
        ret = self.registry._make_import_args('foo')
        assert ret[0] == 'foo'
        assert ret[-1] == ['foo']

    def test_get_enabled(self):
        self.registry.extension_names = set(['foo', 'bar', 'baz'])
        self.registry.disabled_extensions = {'#bots': set(['foo'])}

        enabled = self.registry.get_enabled('#bots')

        assert 'foo' not in enabled
        assert 'bar' in enabled
        assert 'baz' in enabled

    def test_get_disabled(self):
        self.registry.extension_names = set(['foo', 'bar', 'baz'])
        self.registry.disabled_extensions = {'#bots': set(['foo'])}

        disabled = self.registry.get_disabled('#bots')

        assert 'foo' in disabled

    def test_disable_unknown_extension_name(self):
        self.registry.disabled_extensions = {}
        self.registry.extension_names = set()

        assert not self.registry.disable('foo', '#bots')

    def test_disable(self):
        self.registry.disabled_extensions = {}
        self.registry.extension_names = set(['foo'])

        assert self.registry.disable('foo', '#bots')

    def test_is_disabled_for_class(self):
        class Fake(object):
            NAME = 'foo'

        self.registry.disabled_extensions = {'#bots': set(['foo'])}
        self.registry.extension_names = set(['foo'])

        assert self.registry.is_disabled(Fake, '#bots')

    def test_is_disabled_for_object(self):
        self.registry.disabled_extensions = {'#bots': set(['foo'])}
        self.registry.extension_names = set(['foo'])

        assert self.registry.is_disabled(Mock(NAME='foo'), '#bots')

    def test_is_disabled(self):
        self.registry.disabled_extensions = {'#bots': set(['foo'])}
        self.registry.extension_names = set(['foo'])

        assert self.registry.is_disabled('foo', '#bots')
示例#4
0
class Helga(object):

    users = {}
    channels = set()
    client = None
    last_response = {}

    def __init__(self, load=True):
        self.operators = set(getattr(settings, 'OPERATORS', []))
        self.extensions = ExtensionRegistry(bot=self, load=load)

    @property
    def nick(self):
        try:
            return self.client.nickname
        except AttributeError:
            return ''

    def join_channel(self, channel):
        self.channels.add(channel)

    def leave_channel(self, channel):
        self.channels.discard(channel)

    def get_current_nick(self, nick):
        if nick in self.users:
            return nick

        # Otherwise, try to figure out if it's stale
        for current, past in self.users.iteritems():
            if nick in past:
                return current

        # Otherwise, it's current
        return nick

    def update_user_nick(self, old, new):
        if not old:
            old = new

        if old not in self.users:
            self.users[new] = set([old])
        else:
            self.users[old].add(new)
            self.users[new] = self.users[old]
            del self.users[old]

    def on(self, event, *args, **kwargs):
        self.extensions.on(event, *args, **kwargs)

    def process(self, message):
        current_nick = self.nick

        # Some things should go first
        self.extensions.preprocess(message)

        if not message.has_response:
            self.extensions.process(message)

        if message.has_response:
            response = message.format_response(botnick=self.nick)
            self.client.msg(message.resp_channel, response)
            self.last_response[message.resp_channel] = response

        if getattr(settings, 'ALLOW_NICK_CHANGE', False):
            self.client.setNick(current_nick)
示例#5
0
 def __init__(self, load=True):
     self.operators = set(getattr(settings, 'OPERATORS', []))
     self.extensions = ExtensionRegistry(bot=self, load=load)
示例#6
0
 def setUp(self):
     self.registry = ExtensionRegistry(mock_bot(), load=False)
示例#7
0
class ExtensionRegistryTestCase(TestCase):
    def setUp(self):
        self.registry = ExtensionRegistry(mock_bot(), load=False)

    def test_make_import_args_with_dots(self):
        ret = self.registry._make_import_args('foo.bar.baz')
        assert ret[0] == 'foo.bar.baz'
        assert ret[-1] == ['baz']

    def test_make_import_args_without_dots(self):
        ret = self.registry._make_import_args('foo')
        assert ret[0] == 'foo'
        assert ret[-1] == ['foo']

    def test_get_possible_extensions(self):
        class FakeModule(object):
            def __ignored(self):
                pass

            def not_ignored(self):
                pass

            def _also_not_ignored(self):
                pass

        fake = FakeModule()
        ret = self.registry._get_possible_extensions(fake)

        assert '__ignored' not in ret
        assert 'not_ignored' in ret
        assert '_also_not_ignored' in ret

    def test_get_enabled(self):
        self.registry.extension_names = set(['foo', 'bar', 'baz'])
        self.registry.disabled_extensions = {'#bots': set(['foo'])}

        enabled = self.registry.get_enabled('#bots')

        assert 'foo' not in enabled
        assert 'bar' in enabled
        assert 'baz' in enabled

    def test_get_disabled(self):
        self.registry.extension_names = set(['foo', 'bar', 'baz'])
        self.registry.disabled_extensions = {'#bots': set(['foo'])}

        disabled = self.registry.get_disabled('#bots')

        assert 'foo' in disabled

    def test_disable_unknown_extension_name(self):
        self.registry.disabled_extensions = {}
        self.registry.extension_names = set()

        assert not self.registry.disable('foo', '#bots')

    def test_disable(self):
        self.registry.disabled_extensions = {}
        self.registry.extension_names = set(['foo'])

        assert self.registry.disable('foo', '#bots')

    def test_is_disabled_for_class(self):
        class Fake(object):
            NAME = 'foo'

        self.registry.disabled_extensions = {'#bots': set(['foo'])}
        self.registry.extension_names = set(['foo'])

        assert self.registry.is_disabled(Fake, '#bots')

    def test_is_disabled_for_object(self):
        self.registry.disabled_extensions = {'#bots': set(['foo'])}
        self.registry.extension_names = set(['foo'])

        assert self.registry.is_disabled(Mock(NAME='foo'), '#bots')

    def test_is_disabled(self):
        self.registry.disabled_extensions = {'#bots': set(['foo'])}
        self.registry.extension_names = set(['foo'])

        assert self.registry.is_disabled('foo', '#bots')
示例#8
0
文件: bot.py 项目: denete/helga
class Helga(object):

    users = {}
    channels = set()
    client = None
    last_response = {}

    def __init__(self, load=True):
        self.operators = set(getattr(settings, 'OPERATORS', []))
        self.extensions = ExtensionRegistry(bot=self, load=load)

    @property
    def nick(self):
        try:
            return self.client.nickname
        except AttributeError:
            return ''

    def join_channel(self, channel):
        self.channels.add(channel)

    def leave_channel(self, channel):
        self.channels.discard(channel)

    def get_current_nick(self, nick):
        if nick in self.users:
            return nick

        # Otherwise, try to figure out if it's stale
        for current, past in self.users.iteritems():
            if nick in past:
                return current

        # Otherwise, it's current
        return nick

    def update_user_nick(self, old, new):
        if not old:
            old = new

        if old not in self.users:
            self.users[new] = set([old])
        else:
            self.users[old].add(new)
            self.users[new] = self.users[old]
            del self.users[old]

    def process(self, message):
        current_nick = self.nick

        # Some things should go first
        self.extensions.preprocess(message)

        if not message.has_response:
            self.extensions.process(message)

        if message.has_response:
            response = message.format_response(botnick=self.nick)
            self.client.msg(message.resp_channel, response)
            self.last_response[message.resp_channel] = response

        if getattr(settings, 'ALLOW_NICK_CHANGE', False):
            self.client.setNick(current_nick)
示例#9
0
文件: bot.py 项目: denete/helga
 def __init__(self, load=True):
     self.operators = set(getattr(settings, 'OPERATORS', []))
     self.extensions = ExtensionRegistry(bot=self, load=load)