-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.py
49 lines (40 loc) · 1.62 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import time, os
try: os.chdir(os.path.dirname(__file__))
except: pass
def elog(arg):
try: prelog = open('events.log').read()
except: prelog = ''
logg = open('events.log', 'w')
logg.write(prelog+time.asctime(time.localtime())+': '+arg+'\n')
class EventSystem(object):
def __init__(self):
self.events = {} # -- {'eventName':['func1', 'func2']}
def RegisterEvent(self, name):
elog('Registering event: %s'%name)
self.events[name] = []
def AddCallToEvent(self, event, name):
# -- name is the function name AS A STRING (func.__name__)
if event in self.events.keys():
if not name in self.events[event]:
elog('Adding call: %s, To event: %s'%(name,event))
self.events[event].append(name)
else: elog('That event already has that call!')
else: elog('That event does not exist, please register it with RegsisterEvent')
def RaiseEvent(self, event, data=''):
if event in self.events.keys():
if self.events[event]:
for a in self.events[event]:
elog('Raising event: %s, With data: %s'%(event,data))
a(data)
else: elog('Please add some calls to that event before raising it!')
else: elog('That event does not exist, please register it with RegsisterEvent')
def echo(arg):
print arg
def main():
try: os.remove('events.log')
except: pass
a = EventSystem()
a.RegisterEvent('OnExit')
a.AddCallToEvent('OnExit', echo)
a.RaiseEvent('OnExit', 'Event')
if __name__=='__main__': main()