Simplifies event mechanism.
- moves RemoveEvent to events - makes listen a classmethod of the event
This commit is contained in:
parent
4fe3b45836
commit
6d303b2c4c
@ -3,13 +3,8 @@ from .. import color
|
|||||||
from .. import configs
|
from .. import configs
|
||||||
from .helpers import add_references_argument, parse_references
|
from .helpers import add_references_argument, parse_references
|
||||||
|
|
||||||
from ..events import Event
|
from ..events import RemoveEvent
|
||||||
|
|
||||||
class RemoveEvent(Event):
|
|
||||||
def __init__(self, config, ui, citekey):
|
|
||||||
self.config = config
|
|
||||||
self.ui = ui
|
|
||||||
self.citekey = citekey
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('remove', help='removes a paper')
|
parser = subparsers.add_parser('remove', help='removes a paper')
|
||||||
@ -30,4 +25,3 @@ def command(config, ui, references):
|
|||||||
rmevent.send()
|
rmevent.send()
|
||||||
|
|
||||||
rp.remove(c)
|
rp.remove(c)
|
||||||
|
|
||||||
|
@ -1,23 +1,4 @@
|
|||||||
listener = {}
|
_listener = {}
|
||||||
|
|
||||||
|
|
||||||
def listen(EventClass, *args):
|
|
||||||
def wrap(f):
|
|
||||||
if isinstance(EventClass, type) \
|
|
||||||
and issubclass(EventClass, Event) \
|
|
||||||
and EventClass != Event:
|
|
||||||
|
|
||||||
if not EventClass.__name__ in listener:
|
|
||||||
listener[EventClass.__name__] = []
|
|
||||||
listener[EventClass.__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
|
|
||||||
else:
|
|
||||||
raise IOError('{} is not an Event subclass'.format(EventClass))
|
|
||||||
return wrap
|
|
||||||
|
|
||||||
|
|
||||||
class Event(object):
|
class Event(object):
|
||||||
@ -29,28 +10,49 @@ class Event(object):
|
|||||||
self.string = string
|
self.string = string
|
||||||
|
|
||||||
def send(self):
|
def send(self):
|
||||||
""" This function send the instance of the class, i.e. the event to be sent,
|
""" This function sends the instance of the class, i.e. the event
|
||||||
to all function that listen to it
|
to be sent, to all function that listen to it.
|
||||||
"""
|
"""
|
||||||
if self.__class__.__name__ in listener:
|
if self.__class__.__name__ in _listener:
|
||||||
for f, args in listener[self.__class__.__name__]:
|
for f, args in _listener[self.__class__.__name__]:
|
||||||
f(self, *args)
|
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
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
class TestEvent(Event):
|
class TestEvent(Event):
|
||||||
def print_one(self):
|
def print_one(self):
|
||||||
print 'one'
|
print 'one'
|
||||||
|
|
||||||
@listen(TestEvent, 12, 15)
|
@TestEvent.listen(12, 15)
|
||||||
def Display(TestEventInstance, nb1, nb2):
|
def Display(TestEventInstance, nb1, nb2):
|
||||||
print TestEventInstance.string, nb1, nb2
|
print TestEventInstance.string, nb1, nb2
|
||||||
|
|
||||||
@listen(TestEvent)
|
@TestEvent.listen()
|
||||||
def Helloword(TestEventInstance):
|
def Helloword(TestEventInstance):
|
||||||
print 'Helloword'
|
print 'Helloword'
|
||||||
|
|
||||||
@listen(TestEvent)
|
@TestEvent.listen()
|
||||||
def PrintIt(TestEventInstance):
|
def PrintIt(TestEventInstance):
|
||||||
TestEventInstance.print_one()
|
TestEventInstance.print_one()
|
||||||
|
|
||||||
@ -61,7 +63,7 @@ if __name__ == "__main__":
|
|||||||
def add(self, a, b):
|
def add(self, a, b):
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
@listen(AddEvent)
|
@AddEvent.listen()
|
||||||
def DoIt(AddEventInstance):
|
def DoIt(AddEventInstance):
|
||||||
print AddEventInstance.add(17, 25)
|
print AddEventInstance.add(17, 25)
|
||||||
|
|
||||||
|
@ -8,8 +8,8 @@ from ... import files
|
|||||||
from ...plugins import PapersPlugin
|
from ...plugins import PapersPlugin
|
||||||
from ...commands.helpers import add_references_argument, parse_reference
|
from ...commands.helpers import add_references_argument, parse_reference
|
||||||
|
|
||||||
from ...events import listen
|
from ...events import RemoveEvent
|
||||||
from ...commands.remove_cmd import RemoveEvent
|
|
||||||
|
|
||||||
TEXNOTE_SECTION = 'texnote'
|
TEXNOTE_SECTION = 'texnote'
|
||||||
TEXNOTE_SAMPLE_FILE = os.path.join(os.path.dirname(__file__), 'note_sample.tex')
|
TEXNOTE_SAMPLE_FILE = os.path.join(os.path.dirname(__file__), 'note_sample.tex')
|
||||||
@ -39,14 +39,17 @@ class TexnotePlugin(PapersPlugin):
|
|||||||
print "toto"
|
print "toto"
|
||||||
|
|
||||||
|
|
||||||
@listen(RemoveEvent)
|
@RemoveEvent.listen()
|
||||||
def remove(rmevent):
|
def remove(rmevent):
|
||||||
texplug = TexnotePlugin.get_instance()
|
texplug = TexnotePlugin.get_instance()
|
||||||
texplug.toto()
|
texplug.toto()
|
||||||
rp = repo.Repository.from_directory(rmevent.config)
|
rp = repo.Repository.from_directory(rmevent.config)
|
||||||
paper = rp.get_paper(parse_reference(rmevent.ui, rp, rmevent.citekey))
|
paper = rp.get_paper(parse_reference(rmevent.ui, rp, rmevent.citekey))
|
||||||
if 'texnote' in paper.metadata:
|
if 'texnote' in paper.metadata:
|
||||||
os.remove(paper.metadata['texnote'])
|
try:
|
||||||
|
os.remove(paper.metadata['texnote'])
|
||||||
|
except OSError:
|
||||||
|
pass # For some reason, the texnote file didn't exist
|
||||||
paper.metadata.pop('texnote')
|
paper.metadata.pop('texnote')
|
||||||
metapath = rp.path_to_paper_file(paper.citekey, 'meta')
|
metapath = rp.path_to_paper_file(paper.citekey, 'meta')
|
||||||
files.save_meta(paper.metadata, metapath)
|
files.save_meta(paper.metadata, metapath)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user