-
Notifications
You must be signed in to change notification settings - Fork 601
/
messages.py
172 lines (106 loc) · 3.57 KB
/
messages.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 abc
from abc import ABCMeta
from collections import namedtuple
import json
class Msg:
"""
An abstract base class for all messages used for communicating between
the WebServices.
The minimal functionality is the ability to instantiate a Msg from JSON
and to write a Msg instance to JSON.
We use namedtuples because they are lightweight and immutable. The splat
operator (*) that we inherit from namedtuple is also convenient. We empty
__slots__ to avoid unnecessary overhead.
"""
__metaclass__ = ABCMeta
@abc.abstractmethod
def for_json(self):
d = self._asdict()
type_str = self.__class__.__name__
d.update({"type": type_str})
return d
@abc.abstractmethod
def to_json(self):
return json.dumps(self.for_json())
@staticmethod
def from_json(str):
d = json.loads(str)
type_str = d["type"]
del d["type"]
return eval(type_str)(**d)
class LoadSuccessful(
namedtuple(
"LoadSuccessful", ["uri", "path", "version", "is_update", "endpoint_type"]
),
Msg,
):
__slots__ = ()
class LoadFailed(namedtuple("LoadFailed", ["uri", "version", "error_msg"]), Msg):
__slots__ = ()
class LoadInProgress(
namedtuple(
"LoadInProgress", ["uri", "path", "version", "is_update", "endpoint_type"]
),
Msg,
):
__slots__ = ()
class Query(namedtuple("Query", ["uri", "params"]), Msg):
__slots__ = ()
class QuerySuccessful(
namedtuple("QuerySuccessful", ["uri", "version", "response"]), Msg
):
__slots__ = ()
class LoadObject(
namedtuple("LoadObject", ["uri", "url", "version", "is_update", "endpoint_type"]),
Msg,
):
__slots__ = ()
class DeleteObjects(namedtuple("DeleteObjects", ["uris"]), Msg):
__slots__ = ()
# Used for testing to flush out objects
class FlushObjects(namedtuple("FlushObjects", []), Msg):
__slots__ = ()
class ObjectsDeleted(namedtuple("ObjectsDeleted", ["uris"]), Msg):
__slots__ = ()
class ObjectsFlushed(namedtuple("ObjectsFlushed", ["n_before", "n_after"]), Msg):
__slots__ = ()
class CountObjects(namedtuple("CountObjects", []), Msg):
__slots__ = ()
class ObjectCount(namedtuple("ObjectCount", ["count"]), Msg):
__slots__ = ()
class ListObjects(namedtuple("ListObjects", []), Msg):
__slots__ = ()
class ObjectList(namedtuple("ObjectList", ["objects"]), Msg):
__slots__ = ()
class UnknownURI(namedtuple("UnknownURI", ["uri"]), Msg):
__slots__ = ()
class UnknownMessage(namedtuple("UnknownMessage", ["msg"]), Msg):
__slots__ = ()
class DownloadSkipped(
namedtuple("DownloadSkipped", ["uri", "version", "msg", "host"]), Msg
):
__slots__ = ()
class QueryFailed(namedtuple("QueryFailed", ["uri", "error"]), Msg):
__slots__ = ()
class QueryError(namedtuple("QueryError", ["uri", "error"]), Msg):
__slots__ = ()
class CheckHealth(namedtuple("CheckHealth", []), Msg):
__slots__ = ()
class Healthy(namedtuple("Healthy", []), Msg):
__slots__ = ()
class Unhealthy(namedtuple("Unhealthy", []), Msg):
__slots__ = ()
class Ping(namedtuple("Ping", ["id"]), Msg):
__slots__ = ()
class Pong(namedtuple("Pong", ["id"]), Msg):
__slots__ = ()
class Listening(namedtuple("Listening", []), Msg):
__slots__ = ()
class EngineFailure(namedtuple("EngineFailure", ["error"]), Msg):
__slots__ = ()
class FlushLogs(namedtuple("FlushLogs", []), Msg):
__slots__ = ()
class LogsFlushed(namedtuple("LogsFlushed", []), Msg):
__slots__ = ()
class ServiceError(namedtuple("ServiceError", ["error"]), Msg):
__slots__ = ()