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):
|
def wrap(f):
|
||||||
if isinstance(EventClass, type) \
|
if isinstance(EventClass, type) \
|
||||||
and issubclass(EventClass, Event) \
|
and issubclass(EventClass, Event) \
|
||||||
and EventClass != Event:
|
and EventClass != Event:
|
||||||
|
|
||||||
if not EventClass.__name__ in msg_list:
|
if not EventClass.__name__ in listener:
|
||||||
msg_list[EventClass.__name__] = []
|
listener[EventClass.__name__] = []
|
||||||
msg_list[EventClass.__name__].append(f)
|
listener[EventClass.__name__].append((f, args))
|
||||||
|
|
||||||
# next step allow us to call the function itself without Event raised
|
# next step allow us to call the function itself without Event raised
|
||||||
def wrapped_f(*args):
|
def wrapped_f(*args):
|
||||||
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,
|
""" This function send the instance of the class, i.e. the event to be sent,
|
||||||
to all function that listen to it
|
to all function that listen to it
|
||||||
"""
|
"""
|
||||||
if self.__class__.__name__ in msg_list:
|
if self.__class__.__name__ in listener:
|
||||||
for f in msg_list[self.__class__.__name__]:
|
for f, args in listener[self.__class__.__name__]:
|
||||||
f(self)
|
f(self, *args)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
@ -40,9 +42,9 @@ if __name__ == "__main__":
|
|||||||
def print_one(self):
|
def print_one(self):
|
||||||
print 'one'
|
print 'one'
|
||||||
|
|
||||||
@listen(TestEvent)
|
@listen(TestEvent, 12, 15)
|
||||||
def Display(TestEventInstance):
|
def Display(TestEventInstance, nb1, nb2):
|
||||||
print TestEventInstance.string
|
print TestEventInstance.string, nb1, nb2
|
||||||
|
|
||||||
@listen(TestEvent)
|
@listen(TestEvent)
|
||||||
def Helloword(TestEventInstance):
|
def Helloword(TestEventInstance):
|
||||||
@ -71,7 +73,7 @@ if __name__ == "__main__":
|
|||||||
addevent.send()
|
addevent.send()
|
||||||
|
|
||||||
# but also work without the event raising system!
|
# but also work without the event raising system!
|
||||||
Display(myevent)
|
Display(myevent, 12, 15)
|
||||||
Helloword(myevent)
|
Helloword(myevent)
|
||||||
PrintIt(myevent)
|
PrintIt(myevent)
|
||||||
DoIt(addevent)
|
DoIt(addevent)
|
||||||
|
@ -38,7 +38,7 @@ parser = argparse.ArgumentParser(description="research papers repository")
|
|||||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||||
|
|
||||||
for cmd_mod in cmds.values():
|
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 = parser.parse_args()
|
||||||
args.config = config
|
args.config = config
|
||||||
|
@ -39,6 +39,13 @@ class PapersPlugin(object):
|
|||||||
for s in strings:
|
for s in strings:
|
||||||
print s
|
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):
|
def load_plugins(config, ui, names):
|
||||||
"""Imports the modules for a sequence of plugin names. Each name
|
"""Imports the modules for a sequence of plugin names. Each name
|
||||||
|
@ -20,21 +20,31 @@ class TexnotePlugin(PapersPlugin):
|
|||||||
|
|
||||||
def parser(self, subparsers, config):
|
def parser(self, subparsers, config):
|
||||||
parser = subparsers.add_parser(self.name, help="edit advance note in latex")
|
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)
|
parser.add_argument('-v', '--view', action='store_true', help='open the paper in a pdf viewer', default=None)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def command(self, config, ui, reference, view):
|
def command(self, config, ui, texcmd, reference, view):
|
||||||
if view is not None:
|
if view is not None:
|
||||||
subprocess.Popen(['papers', 'open', reference])
|
subprocess.Popen(['papers', 'open', reference])
|
||||||
|
if texcmd == 'edit':
|
||||||
open_texnote(config, ui, reference)
|
open_texnote(config, ui, reference)
|
||||||
|
|
||||||
|
def toto(self):
|
||||||
|
print "toto"
|
||||||
|
|
||||||
|
|
||||||
@listen(RemoveEvent)
|
@listen(RemoveEvent)
|
||||||
def remove(rmevent):
|
def remove(rmevent):
|
||||||
|
texplug = TexnotePlugin.get_instance()
|
||||||
|
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'])
|
os.remove(paper.metadata['texnote'])
|
||||||
paper.metadata.pop('texnote')
|
paper.metadata.pop('texnote')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user