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
This commit is contained in:
parent
4d71ae824d
commit
57ec240798
|
@ -1,9 +1,16 @@
|
||||||
# Bryan Schumaker (8/7/2010)
|
# 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
|
def startup():
|
||||||
vars = map.Map()
|
global vars
|
||||||
|
global prefs
|
||||||
|
vars = Map()
|
||||||
|
prefs = Map()
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
|
import libsaria
|
||||||
|
from libsaria.event import call
|
||||||
|
|
||||||
|
def startup():
|
||||||
|
call("START", libsaria.startup)
|
|
@ -14,6 +14,8 @@ after = now()
|
||||||
print "libsaria loaded (", after-before, ")"
|
print "libsaria loaded (", after-before, ")"
|
||||||
|
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
|
from libsaria import fns
|
||||||
|
fns.startup()
|
||||||
print
|
print
|
||||||
print "Available tests:"
|
print "Available tests:"
|
||||||
for file in os.listdir("tests"):
|
for file in os.listdir("tests"):
|
||||||
|
|
|
@ -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)
|
|
@ -1,3 +1,5 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
# Use to test the Map class provided by libsaria
|
||||||
|
|
||||||
from libsaria import path
|
from libsaria import path
|
||||||
from libsaria.map import Map
|
from libsaria.map import Map
|
||||||
|
|
Loading…
Reference in New Issue