Passed
Push — master ( 227024...1d0698 )
by Oleksandr
02:44
created

tabpy.tabpy_server.common.messages   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 87
dl 0
loc 162
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Msg.to_json() 0 3 1
A Msg.from_json() 0 6 1
A Msg.for_json() 0 6 1
1
import abc
2
from abc import ABCMeta
3
from collections import namedtuple
4
import json
5
6
7
class Msg(object):
8
    """
9
    An abstract base class for all messages used for communicating between
10
    the WebServices.
11
12
    The minimal functionality is the ability to instantiate a Msg from JSON
13
    and to write a Msg instance to JSON.
14
15
    We use namedtuples because they are lightweight and immutable. The splat
16
    operator (*) that we inherit from namedtuple is also convenient. We empty
17
    __slots__ to avoid unnecessary overhead.
18
    """
19
    __metaclass__ = ABCMeta
20
21
    @abc.abstractmethod
22
    def for_json(self):
23
        d = self._asdict()
24
        type_str = self.__class__.__name__
25
        d.update({'type': type_str})
26
        return d
27
28
    @abc.abstractmethod
29
    def to_json(self):
30
        return json.dumps(self.for_json())
31
32
    @staticmethod
33
    def from_json(str):
34
        d = json.loads(str)
35
        type_str = d['type']
36
        del d['type']
37
        return eval(type_str)(**d)
38
39
40
class LoadSuccessful(namedtuple('LoadSuccessful', [
41
            'uri', 'path', 'version', 'is_update', 'endpoint_type']), Msg):
42
    __slots__ = ()
43
44
45
class LoadFailed(namedtuple('LoadFailed', [
46
        'uri', 'version', 'error_msg']), Msg):
47
    __slots__ = ()
48
49
50
class LoadInProgress(namedtuple('LoadInProgress', [
51
        'uri', 'path', 'version', 'is_update', 'endpoint_type']), Msg):
52
    __slots__ = ()
53
54
55
class Query(namedtuple('Query', ['uri', 'params']), Msg):
56
    __slots__ = ()
57
58
59
class QuerySuccessful(namedtuple(
60
        'QuerySuccessful', ['uri', 'version', 'response']), Msg):
61
    __slots__ = ()
62
63
64
class LoadObject(namedtuple('LoadObject', [
65
        'uri', 'url', 'version', 'is_update', 'endpoint_type']), Msg):
66
    __slots__ = ()
67
68
69
class DeleteObjects(namedtuple('DeleteObjects', ['uris']), Msg):
70
    __slots__ = ()
71
72
73
# Used for testing to flush out objects
74
class FlushObjects(namedtuple('FlushObjects', []), Msg):
75
    __slots__ = ()
76
77
78
class ObjectsDeleted(namedtuple('ObjectsDeleted', ['uris']), Msg):
79
    __slots__ = ()
80
81
82
class ObjectsFlushed(namedtuple(
83
        'ObjectsFlushed', ['n_before', 'n_after']), Msg):
84
    __slots__ = ()
85
86
87
class CountObjects(namedtuple('CountObjects', []), Msg):
88
    __slots__ = ()
89
90
91
class ObjectCount(namedtuple('ObjectCount', ['count']), Msg):
92
    __slots__ = ()
93
94
95
class ListObjects(namedtuple('ListObjects', []), Msg):
96
    __slots__ = ()
97
98
99
class ObjectList(namedtuple('ObjectList', ['objects']), Msg):
100
    __slots__ = ()
101
102
103
class UnknownURI(namedtuple('UnknownURI', ['uri']), Msg):
104
    __slots__ = ()
105
106
107
class UnknownMessage(namedtuple('UnknownMessage', ['msg']), Msg):
108
    __slots__ = ()
109
110
111
class DownloadSkipped(namedtuple('DownloadSkipped', [
112
        'uri', 'version', 'msg', 'host']), Msg):
113
    __slots__ = ()
114
115
116
class QueryFailed(namedtuple('QueryFailed', ['uri', 'error']), Msg):
117
    __slots__ = ()
118
119
120
class QueryError(namedtuple('QueryError', ['uri', 'error']), Msg):
121
    __slots__ = ()
122
123
124
class CheckHealth(namedtuple('CheckHealth', []), Msg):
125
    __slots__ = ()
126
127
128
class Healthy(namedtuple('Healthy', []), Msg):
129
    __slots__ = ()
130
131
132
class Unhealthy(namedtuple('Unhealthy', []), Msg):
133
    __slots__ = ()
134
135
136
class Ping(namedtuple('Ping', ['id']), Msg):
137
    __slots__ = ()
138
139
140
class Pong(namedtuple('Pong', ['id']), Msg):
141
    __slots__ = ()
142
143
144
class Listening(namedtuple('Listening', []), Msg):
145
    __slots__ = ()
146
147
148
class EngineFailure(namedtuple('EngineFailure', ['error']), Msg):
149
    __slots__ = ()
150
151
152
class FlushLogs(namedtuple('FlushLogs', []), Msg):
153
    __slots__ = ()
154
155
156
class LogsFlushed(namedtuple('LogsFlushed', []), Msg):
157
    __slots__ = ()
158
159
160
class ServiceError(namedtuple('ServiceError', ['error']), Msg):
161
    __slots__ = ()
162