args in event mechanism
This commit is contained in:
parent
ed44a2c262
commit
415b1b37c5
@ -1,14 +1,16 @@
|
||||
msg_list = {}
|
||||
listener = {}
|
||||
|
||||
def listen(EventClass):
|
||||
|
||||
def listen(EventClass, *args):
|
||||
def wrap(f):
|
||||
if isinstance(EventClass, type) \
|
||||
and issubclass(EventClass, Event) \
|
||||
and EventClass != Event:
|
||||
|
||||
if not EventClass.__name__ in msg_list:
|
||||
msg_list[EventClass.__name__] = []
|
||||
msg_list[EventClass.__name__].append(f)
|
||||
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)
|
||||
@ -30,9 +32,9 @@ class Event(object):
|
||||
""" This function send the instance of the class, i.e. the event to be sent,
|
||||
to all function that listen to it
|
||||
"""
|
||||
if self.__class__.__name__ in msg_list:
|
||||
for f in msg_list[self.__class__.__name__]:
|
||||
f(self)
|
||||
if self.__class__.__name__ in listener:
|
||||
for f, args in listener[self.__class__.__name__]:
|
||||
f(self, *args)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -40,9 +42,9 @@ if __name__ == "__main__":
|
||||
def print_one(self):
|
||||
print 'one'
|
||||
|
||||
@listen(TestEvent)
|
||||
def Display(TestEventInstance):
|
||||
print TestEventInstance.string
|
||||
@listen(TestEvent, 12, 15)
|
||||
def Display(TestEventInstance, nb1, nb2):
|
||||
print TestEventInstance.string, nb1, nb2
|
||||
|
||||
@listen(TestEvent)
|
||||
def Helloword(TestEventInstance):
|
||||
@ -71,7 +73,7 @@ if __name__ == "__main__":
|
||||
addevent.send()
|
||||
|
||||
# but also work without the event raising system!
|
||||
Display(myevent)
|
||||
Display(myevent, 12, 15)
|
||||
Helloword(myevent)
|
||||
PrintIt(myevent)
|
||||
DoIt(addevent)
|
||||
|
@ -38,7 +38,7 @@ parser = argparse.ArgumentParser(description="research papers repository")
|
||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||
|
||||
for cmd_mod in cmds.values():
|
||||
subparser = cmd_mod.parser(subparsers, config)
|
||||
subparser = cmd_mod.parser(subparsers, config) # why do we return the subparser ?
|
||||
|
||||
args = parser.parse_args()
|
||||
args.config = config
|
||||
|
@ -39,6 +39,13 @@ class PapersPlugin(object):
|
||||
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
|
||||
|
@ -20,21 +20,31 @@ class TexnotePlugin(PapersPlugin):
|
||||
|
||||
def parser(self, subparsers, config):
|
||||
parser = subparsers.add_parser(self.name, help="edit advance note in latex")
|
||||
add_references_argument(parser, single=True)
|
||||
sub = parser.add_subparsers(title="valid texnote commands", dest="texcmd")
|
||||
p = sub.add_parser("remove", help="remove a reference")
|
||||
add_references_argument(p, single=True)
|
||||
p = sub.add_parser("edit", help="edit the reference texnote")
|
||||
add_references_argument(p, single=True)
|
||||
#add_references_argument(parser, single=True)
|
||||
parser.add_argument('-v', '--view', action='store_true', help='open the paper in a pdf viewer', default=None)
|
||||
return parser
|
||||
|
||||
def command(self, config, ui, reference, view):
|
||||
def command(self, config, ui, texcmd, reference, view):
|
||||
if view is not None:
|
||||
subprocess.Popen(['papers', 'open', reference])
|
||||
open_texnote(config, ui, reference)
|
||||
if texcmd == 'edit':
|
||||
open_texnote(config, ui, reference)
|
||||
|
||||
def toto(self):
|
||||
print "toto"
|
||||
|
||||
|
||||
@listen(RemoveEvent)
|
||||
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'])
|
||||
paper.metadata.pop('texnote')
|
||||
|
Loading…
x
Reference in New Issue
Block a user