Permalink
Browse files

passing `connection` dict as first arguments for plugins

  • Loading branch information...
igorsobreira committed Sep 20, 2010
1 parent 48bb10c commit 47aae9137417c7cd27ea3b4e6617b68ede1a9693
View
@@ -25,8 +25,11 @@ def onMessage(self, msg):
def answer(self, message):
resolver = PluginResolver()
+ conn_data = {
+ 'message': message,
+ }
try:
- defer = resolver.resolve(unicode(message.body))
+ defer = resolver.resolve(unicode(message.body), conn_data)
defer.addCallback(self.send_response, to=message['from'])
except LookupError:
self.send_response(self.CANT_UNDERSTANT, to=message['from'])
View
@@ -29,7 +29,6 @@ class PluginRegistry(BaseRegistry):
'''
def register(self, name, regex, callable_obj):
- self._validate_callable(callable_obj)
if name in self.plugins:
return
self._plugins[name] = (regex, callable_obj)
@@ -38,11 +37,6 @@ def unregister(self, name_or_callable):
name = getattr(name_or_callable, '__name__', name_or_callable)
self._plugins.pop(name, None)
- def _validate_callable(self, callable_obj):
- # FIXME: probably only works for functions
- if not inspect.getargspec(callable_obj).keywords:
- raise TypeError(u"The plugin %s must receive **kwargs", callable_obj.__name__)
-
class SessionPluginRegistry(BaseRegistry):
'''
View
@@ -5,7 +5,7 @@ class PluginResolver(object):
This has a way to find and call plugins.
'''
- def resolve(self, string):
+ def resolve(self, string, connection_data):
'''
Returns the result of a plugin call. ``string`` is the
message body received by the bot.
@@ -16,13 +16,13 @@ def resolve(self, string):
kwargs = self._clear_kwargs(match.groupdict())
if kwargs:
- return func(**kwargs)
+ return func(connection_data, **kwargs)
args = self._clear_args(match.groups())
if args:
- return func(*args)
+ return func(connection_data, *args)
- return func()
+ return func(connection_data)
def find(self, string):
'''
@@ -3,7 +3,7 @@
from eizzek import plugin, session_registry
@plugin(r'^begin (?P<plugin_name>\w+)$')
-def begin(plugin_name, **kwargs):
+def begin(connection, plugin_name):
'''
This plugin is used to start a Session with
any Session Plugin available.
@@ -15,7 +15,7 @@ def begin(plugin_name, **kwargs):
@plugin(r'^end$')
-def end(**kwargs):
+def end(connection):
'''
This plugin is used to finish a Session with
the current Session Plugin in use.
View
@@ -3,7 +3,7 @@
from eizzek import plugin, registry
@plugin(r'^help ?(?P<plugin>.*)$')
-def help(plugin=None, **kwargs):
+def help(connection, plugin=None):
'''
Eizzek help. Usage:
View
@@ -4,7 +4,7 @@
@plugin(r'^ping (?P<url>.+)$')
-def ping(url, **kwargs):
+def ping(connection, url):
'''
Ping plugin. Usage:
@@ -10,7 +10,7 @@
TAG_URL = 'http://stackoverflow.com/questions/tagged/%s'
@plugin(r'^stackoverflow ?(?P<limit>\d+)? ?(?P<tag>[a-zA-Z0-9\+\#\-\.]+)?$')
-def stackoverflow(limit=None, tag=None, **kwargs):
+def stackoverflow(connection, limit=None, tag=None):
'''
Stack Overflow plugin, get questions from http://stackoverflow.com/
@@ -25,15 +25,15 @@ def test_begin_plugin_not_found(self):
def assert_begin(response):
assert u"Plugin not found" == response
- deferred = begin('foo')
+ deferred = begin({}, 'foo')
deferred.addCallback(assert_begin)
return deferred
def test_begin_plugin_call_begin_method(self):
def assert_begin(response):
assert u"Welcome to reverse!" == response
- deferred = begin('reverse')
+ deferred = begin({}, 'reverse')
deferred.addCallback(assert_begin)
return deferred
@@ -11,22 +11,22 @@ def setUp(self):
self.resolver = PluginResolver()
def test_help_no_arguments(self):
- assert self.resolver.resolve('help')
+ assert self.resolver.resolve('help', {})
def test_help_with_argument(self):
- assert self.resolver.resolve('help plugin_name')
+ assert self.resolver.resolve('help plugin_name', {})
class HelpTest(unittest.TestCase):
def setUp(self):
@plugin(r'^simple (\d+)$')
- def simple_plugin(**kwargs):
+ def simple_plugin(conn, num):
'''nothing special'''
@plugin(r'^no_help_here$')
- def no_help_plugin(**kwargs):
+ def no_help_plugin(conn):
pass
def tearDown(self):
@@ -37,31 +37,31 @@ def test_error_message_for_plugin_not_found(self):
def assert_error(response):
assert u"Plugin not found" == response
- deferred = help(plugin='foo')
+ deferred = help({}, 'foo')
deferred.addCallback(assert_error)
return deferred
def test_show_plugin_help(self):
def assert_help(response):
assert u"nothing special" == response
- deferred = help(plugin='simple_plugin')
+ deferred = help({}, 'simple_plugin')
deferred.addCallback(assert_help)
return deferred
def test_show_self_help_if_no_plugin_is_passed(self):
def assert_self_help(response):
assert "Eizzek help" in response
- deferred = help()
+ deferred = help({})
deferred.addCallback(assert_self_help)
return deferred
def test_show_error_message_when_plugin_has_no_help(self):
def assert_no_help(response):
assert u"No help found" == response
- deferred = help('no_help_plugin')
+ deferred = help({}, 'no_help_plugin')
deferred.addCallback(assert_no_help)
return deferred
@@ -70,6 +70,6 @@ def assert_list(response):
assert u"no_help_plugin" in response
assert u"simple_plugin" in response
- deferred = help('-l')
+ deferred = help({}, '-l')
deferred.addCallback(assert_list)
return deferred
@@ -9,7 +9,7 @@ def test_ping(self):
def assert_ping(response):
assert 'PING igorsobreira.com (67.18.187.198): 56 data bytes' in response
- deferred = ping('igorsobreira.com')
+ deferred = ping({}, 'igorsobreira.com')
deferred.addCallback(assert_ping)
return deferred
@@ -11,7 +11,7 @@ def assert_questions(response):
assert 'Stack Overflow: Top Questions' in response
assert 51 == len(questions) # 50 + header
- deferred = stackoverflow()
+ deferred = stackoverflow({})
deferred.addCallback(assert_questions)
return deferred
@@ -22,7 +22,7 @@ def assert_questions(response):
assert 'Stack Overflow: Top Questions' in response
assert 11 == len(questions)
- deferred = stackoverflow(limit=10)
+ deferred = stackoverflow(connection={}, limit=10)
deferred.addCallback(assert_questions)
return deferred
@@ -32,7 +32,7 @@ def assert_questions(response):
assert 'Stack Overflow: python tag' in response
- deferred = stackoverflow(tag='python')
+ deferred = stackoverflow(connection={}, tag='python')
deferred.addCallback(assert_questions)
return deferred
@@ -43,6 +43,6 @@ def assert_questions(response):
assert 'Stack Overflow: python tag' in response
assert 4 == len(questions)
- deferred = stackoverflow(tag='python', limit=3)
+ deferred = stackoverflow(connection={}, tag='python', limit=3)
deferred.addCallback(assert_questions)
return deferred
@@ -16,7 +16,7 @@ def setUp(self):
self.tag = None
self.limit = 50
- def stackoverflow_mock(limit=None, tag=None, **kw):
+ def stackoverflow_mock(conn, limit=None, tag=None, **kw):
self.called = True
self.tag = tag
self.limit = int(limit) if limit else 50
@@ -31,28 +31,28 @@ def tearDown(self):
registry.register('stackoverflow', self.stackoverflow_regex, self.stackoverflow_function)
def test_simple(self):
- self.resolver.resolve('stackoverflow')
+ self.resolver.resolve('stackoverflow', {})
assert self.called
assert self.tag is None
assert 50 == self.limit
def test_tagged(self):
- self.resolver.resolve('stackoverflow python')
+ self.resolver.resolve('stackoverflow python', {})
assert self.called
assert 'python' == self.tag
assert 50 == self.limit
def test_limit(self):
- self.resolver.resolve('stackoverflow 10')
+ self.resolver.resolve('stackoverflow 10', {})
assert self.called
assert self.tag is None
assert 10 == self.limit
def test_tagged_limit(self):
- self.resolver.resolve('stackoverflow 15 python')
+ self.resolver.resolve('stackoverflow 15 python', {})
assert self.called
assert 'python' == self.tag
@@ -62,7 +62,7 @@ def test_different_tags(self):
tags = ('c++', 'c#', 'regular-language', 'asp.net', '.net', 'actionscript-3')
for tag in tags:
- self.resolver.resolve('stackoverflow ' + tag)
+ self.resolver.resolve('stackoverflow ' + tag, {})
assert self.called
assert tag == self.tag
@@ -31,16 +31,6 @@ def other_ping(**kw):
assert len(self.registry.plugins) == 1
assert self.registry.plugins['ping'] == (self.regex, self.ping)
- def test_register_verifies_the_plugin_receives_kwargs(self):
-
- def wrong(): return ''
-
- try:
- self.registry.register('wrong', self.regex, wrong)
- assert 0, u"Should raise ValueError"
- except TypeError:
- pass
-
def test_unregister_plugin_by_name(self):
assert len(self.registry.plugins) == 0
@@ -8,49 +8,48 @@ def setUp(self):
registry.clear()
@plugin(r"age (\d+)")
- def age(num, **kwargs):
+ def age(conn, num):
return "you're %d" % int(num)
@plugin(r"aged ?(\d+)?")
- def age_default(num=0, **kwargs):
+ def age_default(conn, num=0):
return "you're %s" % num
@plugin(r"hello ?(?P<name>\w+)?")
- def hello(name='stranger', **kwargs):
+ def hello(conn, name='stranger'):
return "hello %s" % name
self.resolver = PluginResolver()
def test_dont_find_plugin(self):
try:
- self.resolver.resolve("no plugin")
+ self.resolver.resolve("no plugin", {})
assert 0, u"Shouldn't find any plugin"
except LookupError:
pass
def test_call_plugin_with_args(self):
- result = self.resolver.resolve('age 22')
+ result = self.resolver.resolve('age 22', {})
assert u"you're 22" == result
def test_call_plugin_with_kwargs(self):
- result = self.resolver.resolve('hello igor')
+ result = self.resolver.resolve('hello igor', {})
assert u"hello igor" == result
def test_call_plugin_with_kwargs_and_default_argument(self):
- result = self.resolver.resolve('hello')
+ result = self.resolver.resolve('hello', {})
assert u"hello stranger" == result
def test_call_plugin_with_args_and_default_argument(self):
- result = self.resolver.resolve('aged')
+ result = self.resolver.resolve('aged', {})
assert "you're 0"
def test_find_callable_function(self):
func, _ = self.resolver.find('age 22')
- print func.__name__
assert 'age' == func.__name__

0 comments on commit 47aae91

Please sign in to comment.