commit
7a21780ec9
@ -0,0 +1,34 @@
|
|||||||
|
_listener = {}
|
||||||
|
|
||||||
|
|
||||||
|
class Event(object):
|
||||||
|
"""Base event that can be sent to listeners.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def send(self):
|
||||||
|
""" This function sends the instance of the class, i.e. the event
|
||||||
|
to be sent, to all function that listen to it.
|
||||||
|
"""
|
||||||
|
if self.__class__.__name__ in _listener:
|
||||||
|
for f, args in _listener[self.__class__.__name__]:
|
||||||
|
f(self, *args)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def listen(cls, *args):
|
||||||
|
def wrap(f):
|
||||||
|
if cls.__name__ not in _listener:
|
||||||
|
_listener[cls.__name__] = []
|
||||||
|
_listener[cls.__name__].append((f, args))
|
||||||
|
|
||||||
|
# next step allow us to call the function itself without Event raised
|
||||||
|
def wrapped_f(*args):
|
||||||
|
f(*args)
|
||||||
|
return wrapped_f
|
||||||
|
return wrap
|
||||||
|
|
||||||
|
|
||||||
|
class RemoveEvent(Event):
|
||||||
|
def __init__(self, config, ui, citekey):
|
||||||
|
self.config = config
|
||||||
|
self.ui = ui
|
||||||
|
self.citekey = citekey
|
@ -0,0 +1,79 @@
|
|||||||
|
import importlib
|
||||||
|
|
||||||
|
PLUGIN_NAMESPACE = 'plugs'
|
||||||
|
|
||||||
|
_classes = []
|
||||||
|
_instances = {}
|
||||||
|
|
||||||
|
|
||||||
|
class PapersPlugin(object):
|
||||||
|
"""The base class for all plugins. Plugins provide
|
||||||
|
functionality by defining a subclass of PapersPlugin and overriding
|
||||||
|
the abstract methods defined here.
|
||||||
|
"""
|
||||||
|
def __init__(self, config, ui):
|
||||||
|
"""Perform one-time plugin setup.
|
||||||
|
"""
|
||||||
|
self.name = self.__module__.split('.')[-1]
|
||||||
|
self.config = config
|
||||||
|
self.ui = ui
|
||||||
|
|
||||||
|
#config and ui and given again to stay consistent with the core papers cmd.
|
||||||
|
#two options:
|
||||||
|
#- create specific cases in script papers/papers
|
||||||
|
#- do not store self.config and self.ui and use them if needed when command is called
|
||||||
|
#this may end up with a lot of function with config/ui in argument
|
||||||
|
#or just keep it that way...
|
||||||
|
def parser(self, subparsers, config):
|
||||||
|
""" Should retrun the parser with plugins specific command.
|
||||||
|
This is a basic example
|
||||||
|
"""
|
||||||
|
parser = subparsers.add_parser(self.name, help="echo string in argument")
|
||||||
|
parser.add_argument('strings', nargs='*', help='the strings')
|
||||||
|
return parser
|
||||||
|
|
||||||
|
def command(self, config, ui, strings):
|
||||||
|
"""This function will be called with argument defined in the parser above
|
||||||
|
This is a basic example
|
||||||
|
"""
|
||||||
|
for s in strings:
|
||||||
|
print s
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_instance(cls):
|
||||||
|
if cls in _instances:
|
||||||
|
return _instances[cls]
|
||||||
|
else:
|
||||||
|
raise RuntimeError("{} instance not created".format(cls.__name__))
|
||||||
|
|
||||||
|
|
||||||
|
def load_plugins(config, ui, names):
|
||||||
|
"""Imports the modules for a sequence of plugin names. Each name
|
||||||
|
must be the name of a Python module under the "PLUGIN_NAMESPACE" namespace
|
||||||
|
package in sys.path; the module indicated should contain the
|
||||||
|
PapersPlugin subclasses desired.
|
||||||
|
"""
|
||||||
|
for name in names:
|
||||||
|
modname = '%s.%s.%s.%s' % ('papers', PLUGIN_NAMESPACE, name, name)
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
namespace = importlib.import_module(modname)
|
||||||
|
except ImportError as exc:
|
||||||
|
# Again, this is hacky:
|
||||||
|
if exc.args[0].endswith(' ' + name):
|
||||||
|
ui.warning('plugin {} not found'.format(name))
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
for obj in namespace.__dict__.values():
|
||||||
|
if isinstance(obj, type) and issubclass(obj, PapersPlugin) \
|
||||||
|
and obj != PapersPlugin:
|
||||||
|
_classes.append(obj)
|
||||||
|
_instances[obj] = obj(config, ui)
|
||||||
|
|
||||||
|
except:
|
||||||
|
ui.warning('error loading plugin {}'.format(name))
|
||||||
|
|
||||||
|
|
||||||
|
def get_plugins():
|
||||||
|
return _instances
|
@ -0,0 +1,65 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from papers.events import Event
|
||||||
|
|
||||||
|
|
||||||
|
_output = None
|
||||||
|
|
||||||
|
|
||||||
|
class TestEvent(Event):
|
||||||
|
def __init__(self, string):
|
||||||
|
self.string = string
|
||||||
|
|
||||||
|
def print_one(self):
|
||||||
|
_output.append('one')
|
||||||
|
|
||||||
|
|
||||||
|
class AddEvent(Event):
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def add(self, a, b):
|
||||||
|
return a + b
|
||||||
|
|
||||||
|
|
||||||
|
@TestEvent.listen(12, 15)
|
||||||
|
def display(TestEventInstance, nb1, nb2):
|
||||||
|
_output.append("%s %s %s"
|
||||||
|
% (TestEventInstance.string, nb1, nb2))
|
||||||
|
|
||||||
|
|
||||||
|
@TestEvent.listen()
|
||||||
|
def hello_word(TestEventInstance):
|
||||||
|
_output.append('Helloword')
|
||||||
|
|
||||||
|
|
||||||
|
@TestEvent.listen()
|
||||||
|
def print_it(TestEventInstance):
|
||||||
|
TestEventInstance.print_one()
|
||||||
|
|
||||||
|
|
||||||
|
@AddEvent.listen()
|
||||||
|
def do_it(AddEventInstance):
|
||||||
|
_output.append(AddEventInstance.add(17, 25))
|
||||||
|
|
||||||
|
|
||||||
|
class TestEvents(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
global _output
|
||||||
|
_output = []
|
||||||
|
|
||||||
|
def test_listen_TestEvent(self):
|
||||||
|
# using the callback system
|
||||||
|
myevent = TestEvent('abcdefghijklmnopqrstuvwxyz')
|
||||||
|
myevent.send() # this one call three function
|
||||||
|
correct = ['abcdefghijklmnopqrstuvwxyz 12 15',
|
||||||
|
'Helloword',
|
||||||
|
'one']
|
||||||
|
self.assertEquals(_output, correct)
|
||||||
|
|
||||||
|
def test_listen_AddEvent(self):
|
||||||
|
addevent = AddEvent()
|
||||||
|
addevent.send()
|
||||||
|
correct = [42]
|
||||||
|
self.assertEquals(_output, correct)
|
Loading…
Reference in new issue