Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganizing structure. adding basic plugin engine
- Loading branch information
1 parent
b10a9fc
commit 57a5732
Showing
16 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
start: | ||
twistd -ny eizzek/twistd.tac | ||
|
||
start_daemon: | ||
twistd -y eizzek/twistd.tac | ||
|
||
test: | ||
nosetests tests/ --with-coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
from eizzek.lib.registry import registry | ||
|
||
class plugin(object): | ||
''' | ||
Decorator used do create a plugin. | ||
''' | ||
|
||
def __init__(self, regex, name=None): | ||
self.regex = regex | ||
self.name = name | ||
|
||
def __call__(self, func): | ||
name = self.name or func.__name__ | ||
registry.register(name, self.regex, func) | ||
return func | ||
|
||
|
||
|
||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# from eizzek.plugins import plugin | ||
# | ||
# @plugin(regex=r'^ping (?P<url>.+)$') | ||
# def ping(url): | ||
# | ||
# return '' | ||
# | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
class PluginRegistry(object): | ||
''' | ||
Register Eizzek plugins. | ||
''' | ||
|
||
_plugins = {} | ||
|
||
def register(self, name, regex, callable_obj): | ||
if name in self.plugins: | ||
return | ||
self._plugins[name] = (regex, callable_obj) | ||
|
||
def unregister(self, name_or_callable): | ||
if hasattr(name_or_callable, '__name__'): | ||
name_or_callable = name_or_callable.__name__ | ||
self._plugins.pop(name_or_callable, None) | ||
|
||
@property | ||
def plugins(self): | ||
return self._plugins | ||
|
||
|
||
@classmethod | ||
def clear(cls): | ||
cls._plugins = {} | ||
|
||
|
||
|
||
|
||
registry = PluginRegistry() |
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from unittest import TestCase | ||
|
||
from eizzek.lib.decorators import plugin | ||
from eizzek.lib.registry import registry | ||
|
||
class PluginTest(TestCase): | ||
|
||
def setUp(self): | ||
registry.clear() | ||
|
||
|
||
def test_plugin(self): | ||
|
||
assert len(registry.plugins) == 0 | ||
|
||
@plugin(r'^ping (.+)$') | ||
def ping(): | ||
return '' | ||
|
||
assert len(registry.plugins) == 1 | ||
assert registry.plugins.has_key('ping') | ||
|
||
|
||
def test_named_plugin(self): | ||
|
||
assert len(registry.plugins) == 0 | ||
|
||
@plugin(r'^ping (.+)$', name='ping_plugin') | ||
def ping(): | ||
return '' | ||
|
||
assert len(registry.plugins) == 1 | ||
assert registry.plugins.has_key('ping_plugin') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from unittest import TestCase | ||
|
||
from eizzek.lib.registry import PluginRegistry | ||
|
||
class PluginRegistryTest(TestCase): | ||
|
||
def setUp(self): | ||
PluginRegistry.clear() | ||
self.registry = PluginRegistry() | ||
|
||
def ping(): | ||
return '' | ||
self.ping = ping | ||
self.regex = r'^ping (.+)$' | ||
|
||
|
||
def test_register_plugin(self): | ||
|
||
assert len(self.registry.plugins) == 0 | ||
|
||
self.registry.register(self.ping.__name__, self.regex, self.ping) | ||
|
||
assert len(self.registry.plugins) == 1 | ||
assert self.registry.plugins['ping'] == (self.regex, self.ping) | ||
|
||
def other_ping(): | ||
return '' | ||
|
||
# ignore duplicates | ||
self.registry.register('ping', self.regex, other_ping) | ||
|
||
assert len(self.registry.plugins) == 1 | ||
assert self.registry.plugins['ping'] == (self.regex, self.ping) | ||
|
||
|
||
def test_unregister_plugin(self): | ||
|
||
assert len(self.registry.plugins) == 0 | ||
|
||
self.registry.register(self.ping.__name__, self.regex, self.ping) | ||
|
||
assert len(self.registry.plugins) == 1 | ||
|
||
self.registry.unregister('ping') # by name | ||
|
||
assert len(self.registry.plugins) == 0 | ||
|
||
self.registry.register(self.ping.__name__, self.regex, self.ping) | ||
|
||
assert len(self.registry.plugins) == 1 | ||
|
||
self.registry.unregister(self.ping) # by callable, with __name__ attribute | ||
|
||
assert len(self.registry.plugins) == 0 | ||
|
||
|
||
def test_clear(self): | ||
self.registry.register(self.ping.__name__, self.regex, self.ping) | ||
|
||
assert len(self.registry.plugins) == 1 | ||
|
||
self.registry.clear() | ||
|
||
assert len(self.registry.plugins) == 0 | ||
|