From 57ec24079821d67e2bc8764ac7386dbf041abfc9 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sat, 7 Aug 2010 14:23:50 -0400 Subject: [PATCH] Added event system I combined various parts of the Ocarina 4.0 event system into one file. The new event file has threading abilities built in, so I no longer need an external file for that --- libsaria/__init__.py | 15 +++++--- libsaria/event.py | 81 ++++++++++++++++++++++++++++++++++++++++++++ libsaria/fns.py | 7 ++++ saria-test.py | 2 ++ tests/event.py | 19 +++++++++++ tests/map.py | 2 ++ 6 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 libsaria/event.py create mode 100644 libsaria/fns.py create mode 100644 tests/event.py diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 39a623eb..2537f2c9 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -1,9 +1,16 @@ # Bryan Schumaker (8/7/2010) -__all__ = ["data", "map", "path"] +__all__ = ["data", "event", "fns", "map", "path"] +import event +from map import Map -import map +# Variables are not saved across sessions, but preferences are +vars = None +prefs = None -# Runtime variables are not saved -vars = map.Map() +def startup(): + global vars + global prefs + vars = Map() + prefs = Map() diff --git a/libsaria/event.py b/libsaria/event.py new file mode 100644 index 00000000..2cdab671 --- /dev/null +++ b/libsaria/event.py @@ -0,0 +1,81 @@ +# Bryan Schumaker (8/7/2010) + +events = dict() +from threading import Thread + + +class EventThread(Thread): + def __init__(self, func, action, arg): + Thread.__init__(self) + self.func = func + self.action = action + self.arg = arg + + def run(self): + res = call("BGTHREAD", self.func, self.arg) + start("POST%s" % self.action, res) + + +def invite(key, func): + global events + if key not in events: + events[key] = set() + events[key].add(func) + + +def uninvite(key, func): + global events + if key not in events: + return + events[key].discard(func) + + +def start(key, arg=None): + global events + if key not in events: + return + for func in events[key]: + try: + if arg == None: + func() + else: + func(arg) + except Exception,e: + print + print "===== EVENT ERROR =====" + print "Event:", key + print "Function:", func + print "Args:", arg + print e + print "The function has been uninvited from this event" + print "=====" + + +def start_thread(func, action, arg): + try: + th = EventThread(func, action, arg) + th.start() + return True + except Exception,e: + print e + return False + + +def call(action, func=None, arg=None, bg=False): + start("PRE%s" % action, arg) + if func == None: + return None + + if bg == True: + res = start_thread(func, action, arg) + else: + if arg == None: + res = func() + else: + res = func(arg) + start("POST%s" % action, res) + return res + + + + diff --git a/libsaria/fns.py b/libsaria/fns.py new file mode 100644 index 00000000..a4b6e2ef --- /dev/null +++ b/libsaria/fns.py @@ -0,0 +1,7 @@ +# Bryan Schumaker (8/7/2010) + +import libsaria +from libsaria.event import call + +def startup(): + call("START", libsaria.startup) diff --git a/saria-test.py b/saria-test.py index 7dbfbfb5..359d1b8e 100644 --- a/saria-test.py +++ b/saria-test.py @@ -14,6 +14,8 @@ after = now() print "libsaria loaded (", after-before, ")" if len(sys.argv) == 1: + from libsaria import fns + fns.startup() print print "Available tests:" for file in os.listdir("tests"): diff --git a/tests/event.py b/tests/event.py new file mode 100644 index 00000000..653aead3 --- /dev/null +++ b/tests/event.py @@ -0,0 +1,19 @@ +# Bryan Schumaker (8/7/2010) +# Use to test the libsaria event system + +from libsaria import event + +def pre_test(arg=None): + print "pre_test(", arg, ") running" + +def post_test(arg=None): + print "post_test(", arg, ") running" + +def test(arg=None): + print "test(", arg, ") running" + return False + + +event.invite("PRETEST", pre_test) +event.invite("POSTTEST", post_test) +event.call("TEST", test, True) diff --git a/tests/map.py b/tests/map.py index c539de55..2e4b0d2d 100644 --- a/tests/map.py +++ b/tests/map.py @@ -1,3 +1,5 @@ +# Bryan Schumaker (8/7/2010) +# Use to test the Map class provided by libsaria from libsaria import path from libsaria.map import Map