forked from flags/Reactor-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
84 lines (60 loc) · 2.37 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from globals import LIFE
import alife
import logging
def create(name, process_callback, process_arguments, complete_callback=None, complete_arguments=None, fail_callback=None, fail_arguments=None, repeat_every=-1):
_event = {'name': name,
'process': {'callback': process_callback, 'arguments': process_arguments},
'complete_on': {'callback': complete_callback, 'arguments': complete_arguments},
'fail_on': {'callback': fail_callback, 'arguments': fail_arguments},
'repeat': repeat_every,
'completed': False,
'failed': False,
'accepted': [],
'flags': {}}
return _event
def process_event(event):
event['failed'] = False
if event['completed']:
return True
if event['complete_on']['callback'] and event['complete_on']['callback'](**event['complete_on']['arguments']):
event['completed'] = True
return True
if event['fail_on']['callback']:
_args = {}
for key in event['fail_on']['arguments']:
if isinstance(event['fail_on']['arguments'][key], dict) and '_action' in event['fail_on']['arguments'][key]:
_args[key] = alife.action.execute(event['fail_on']['arguments'][key])
else:
_args[key] = event['fail_on']['arguments'][key]
if not alife.action.execute(event['fail_on']['callback'])(**_args):
event['failed'] = True
return False
if event['process']:
_args = {}
for key in event['process']['arguments']:
if isinstance(event['process']['arguments'][key], dict) and '_action' in event['process']['arguments'][key]:
_args[key] = alife.action.execute(event['process']['arguments'][key])
else:
_args[key] = event['process']['arguments'][key]
alife.action.execute(event['process']['callback'])(**_args)
return True
def accept(event, life_id):
if life_id in event['accepted']:
return False
event['accepted'].append(life_id)
logging.debug('%s has accepted event: %s' % (' '.join(LIFE[life_id]['name']), event['name']))
def has_accepted(event, life_id):
if life_id in event['accepted']:
return True
return False
def clear_accepted(event):
event['accepted'] = []
logging.debug('Accepted list cleared for event: %s' % event['name'])
def flag(event, flag, value):
_event['flags'][flag] = value
def unflag(event, flag):
del _event['flags'][flag]
def get_flag(event, flag):
if flag in _event['flags']:
return _event['flags'][flag]
return False