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 .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):
|
||||
parser = subparsers.add_parser('remove', help='removes a paper')
|
||||
@ -30,4 +25,3 @@ def command(config, ui, references):
|
||||
rmevent.send()
|
||||
|
||||
rp.remove(c)
|
||||
|
||||
|
@ -1,23 +1,4 @@
|
||||
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
|
||||
_listener = {}
|
||||
|
||||
|
||||
class Event(object):
|
||||
@ -29,28 +10,49 @@ class Event(object):
|
||||
self.string = string
|
||||
|
||||
def send(self):
|
||||
""" This function send the instance of the class, i.e. the event to be sent,
|
||||
to all function that listen to it
|
||||
""" 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__]:
|
||||
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
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
class TestEvent(Event):
|
||||
def print_one(self):
|
||||
print 'one'
|
||||
|
||||
@listen(TestEvent, 12, 15)
|
||||
@TestEvent.listen(12, 15)
|
||||
def Display(TestEventInstance, nb1, nb2):
|
||||
print TestEventInstance.string, nb1, nb2
|
||||
|
||||
@listen(TestEvent)
|
||||
@TestEvent.listen()
|
||||
def Helloword(TestEventInstance):
|
||||
print 'Helloword'
|
||||
|
||||
@listen(TestEvent)
|
||||
@TestEvent.listen()
|
||||
def PrintIt(TestEventInstance):
|
||||
TestEventInstance.print_one()
|
||||
|
||||
@ -61,7 +63,7 @@ if __name__ == "__main__":
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
@listen(AddEvent)
|
||||
@AddEvent.listen()
|
||||
def DoIt(AddEventInstance):
|
||||
print AddEventInstance.add(17, 25)
|
||||
|
||||
|
@ -8,8 +8,8 @@ from ... import files
|
||||
from ...plugins import PapersPlugin
|
||||
from ...commands.helpers import add_references_argument, parse_reference
|
||||
|
||||
from ...events import listen
|
||||
from ...commands.remove_cmd import RemoveEvent
|
||||
from ...events import RemoveEvent
|
||||
|
||||
|
||||
TEXNOTE_SECTION = 'texnote'
|
||||
TEXNOTE_SAMPLE_FILE = os.path.join(os.path.dirname(__file__), 'note_sample.tex')
|
||||
@ -39,14 +39,17 @@ class TexnotePlugin(PapersPlugin):
|
||||
print "toto"
|
||||
|
||||
|
||||
@listen(RemoveEvent)
|
||||
@RemoveEvent.listen()
|
||||
def remove(rmevent):
|
||||
texplug = TexnotePlugin.get_instance()
|
||||
texplug.toto()
|
||||
rp = repo.Repository.from_directory(rmevent.config)
|
||||
paper = rp.get_paper(parse_reference(rmevent.ui, rp, rmevent.citekey))
|
||||
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')
|
||||
metapath = rp.path_to_paper_file(paper.citekey, 'meta')
|
||||
files.save_meta(paper.metadata, metapath)
|
||||
|
Loading…
x
Reference in New Issue
Block a user