-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
172 lines (122 loc) · 3.77 KB
/
base.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging
import threading
import json
from datetime import datetime
from enum import Enum
from functools import total_ordering
class ServiceError(Exception):
pass
class ServicePayloadError(Exception):
pass
class MissingConfigurationError(Exception):
pass
class StopConsuming(Exception):
pass
class DeferDecision(Exception):
pass
def setup_logging(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
if logger.hasHandlers():
return logger
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(process)d - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
def deserialize_message(body):
s = body.decode('utf-8')
assert type(s) is str
d = json.loads(s)
assert type(d) is dict
return d
class StoppableThread(threading.Thread):
def __init__(self):
super(StoppableThread, self).__init__(daemon=True, target=self.consume)
self._stop_event = threading.Event()
def consume(self):
raise NotImplemented
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
class AccidentLocation:
@staticmethod
def from_dict(o):
return AccidentLocation(o['lat'], o['long'])
def __init__(self, lat, long):
self.lat = lat
self.long = long
def to_dict(self):
return {
'lat': self.lat,
'long': self.long
}
class Boundary:
@staticmethod
def from_dict(o):
return Boundary(o['left'], o['top'], o['right'], o['bottom'])
def __init__(self, left, top, right, bottom):
self.left = left
self.top = top
self.right = right
self.bottom = bottom
def to_dict(self):
return {
'left': self.left,
'top': self.top,
'right': self.right,
'bottom': self.bottom
}
@total_ordering
class AccidentPayload:
@staticmethod
def from_dict(o):
boundary = Boundary.from_dict(o['boundary'])
accident = AccidentLocation.from_dict(o['accident'])
p = AccidentPayload(boundary, accident)
p.utc_timestamp = o['utc_timestamp']
return p
def __init__(self, boundary, location, utc_timestamp=datetime.utcnow().timestamp()):
self.boundary = boundary
self.location = location
self.utc_timestamp = utc_timestamp
def to_dict(self):
return {
'utc_timestamp': self.utc_timestamp,
'boundary': self.boundary.to_dict(),
'accident': self.location.to_dict()
}
def __eq__(self, other):
return self.utc_timestamp == other.utc_timestamp
def __lt__(self, other):
return self.utc_timestamp < other.utc_timestamp
class PlayerInstruction(Enum):
GO = 'go'
STATUS = 'status'
class AccidentDeployment:
@staticmethod
def from_dict(o):
action = PlayerInstruction(o['action'])
payload = AccidentPayload.from_dict(o['payload'])
d = AccidentDeployment(action, payload)
d.utc_decision_time = o['utc_decision_time']
return d
def __init__(self, action, payload):
self.action = action
self.payload = payload
self.utc_decision_time = datetime.utcnow().timestamp()
def to_dict(self):
return {
'action': self.action.value,
'payload': self.payload.to_dict(),
'utc_decision_time': self.utc_decision_time,
}
class RpcCall:
body = None
error = False
def __init__(self, correlation_id):
self.correlation_id = correlation_id
def completed(self):
return self.error or self.body is not None