-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathresponse.py
More file actions
151 lines (130 loc) · 6.02 KB
/
response.py
File metadata and controls
151 lines (130 loc) · 6.02 KB
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
# The MIT License (MIT)
#
# Copyright (C) 2020, 2022 - Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Response classes file."""
import json
from enum import Enum
from tsp.model_type import ModelType
from tsp.object_model import ObjectModel
from tsp.output_descriptor import OutputDescriptor
from tsp.virtual_table_header_model import VirtualTableHeaderModel
from tsp.virtual_table_model import VirtualTableModel
from tsp.time_graph_model import TimeGraphModel, TimeGraphArrow, TimeGraphModelEncoder, TimeGraphArrowEncoder
from tsp.xy_model import XYModel
from tsp.entry_model import EntryModel, EntryModelEncoder
from tsp.xy_model import XYModel, XYModelEncoder
MODEL_KEY = "model"
OUTPUT_DESCRIPTOR_KEY = "output"
RESPONSE_STATUS_KEY = "status"
STATUS_MESSAGE_KEY = "statusMessage"
class ResponseStatus(Enum):
'''
Model is partial, data provider is still computing. If this status is
returned, it's viewer responsability to request again the data provider after
waiting some time. Request data provider until COMPLETED status is received
'''
RUNNING = "RUNNING"
'''
Model is complete, no need to request data provider again
'''
COMPLETED = "COMPLETED"
'''
Error happened. Please see logs or detailed message of status.
'''
FAILED = "FAILED"
'''
Task has been cancelled. Please see logs or detailed message of status.
'''
CANCELLED = "CANCELLED"
# pylint: disable=too-few-public-methods
class GenericResponse:
'''
Output element style object for one style key. It supports style
inheritance. To avoid creating new styles the element style can have a parent
style and will have all the same style properties values as the parent and
can add or override style properties.
'''
def __init__(self, params, model_type):
'''
Constructor
'''
self.model_type = model_type
# Model returned in the response
self.model = params.get(MODEL_KEY)
if MODEL_KEY in params and params.get(MODEL_KEY) is not None:
if self.model_type == ModelType.TIME_GRAPH_TREE:
self.model = EntryModel(params.get(MODEL_KEY), self.model_type)
elif self.model_type == ModelType.TIME_GRAPH_STATE:
self.model = TimeGraphModel(params.get(MODEL_KEY))
elif self.model_type == ModelType.TIME_GRAPH_ARROW:
arrows = []
for arrow in params.get(MODEL_KEY):
arrows.append(TimeGraphArrow(arrow))
self.model = arrows
elif self.model_type == ModelType.XY_TREE:
self.model = EntryModel(params.get(MODEL_KEY))
elif self.model_type == ModelType.XY:
self.model = XYModel(params.get(MODEL_KEY))
elif self.model_type == ModelType.DATA:
self.model = ObjectModel(params.get(MODEL_KEY))
elif self.model_type == ModelType.DATA_TREE:
self.model = EntryModel(params.get(MODEL_KEY), self.model_type)
elif self.model_type == ModelType.VIRTUAL_TABLE_HEADER:
self.model = VirtualTableHeaderModel(params.get(MODEL_KEY))
elif self.model_type == ModelType.VIRTUAL_TABLE:
self.model = VirtualTableModel(params.get(MODEL_KEY))
# Output descriptor
if OUTPUT_DESCRIPTOR_KEY in params:
self.output = OutputDescriptor(params.get(OUTPUT_DESCRIPTOR_KEY))
else:
self.output = None
# Response status as described by ResponseStatus
if RESPONSE_STATUS_KEY in params:
self.status = ResponseStatus(params.get(RESPONSE_STATUS_KEY))
else: # pragma: no cover
self.status = ResponseStatus.FAILED
# Message associated with the response
if STATUS_MESSAGE_KEY in params:
self.status_text = params.get(STATUS_MESSAGE_KEY)
else: # pragma: no cover
self.status_text = ""
def __repr__(self) -> str:
return 'GenericResponse(model_type={}, model={}, output_descriptor={}, status={}, status_text={})'.format(
self.model_type, self.model, self.output, self.status, self.status_text
)
class GenericResponseEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, GenericResponse):
model = obj.model
if model is None:
return None
if obj.model_type == ModelType.TIME_GRAPH_TREE \
or obj.model_type == ModelType.XY_TREE \
or obj.model_type == ModelType.DATA_TREE:
model = EntryModelEncoder().default(obj.model)
elif obj.model_type == ModelType.TIME_GRAPH_STATE:
model = TimeGraphModelEncoder().default(obj.model)
elif obj.model_type == ModelType.TIME_GRAPH_ARROW:
model = [TimeGraphArrowEncoder().default(arrow) for arrow in obj.model]
elif obj.model_type == ModelType.XY:
model = XYModelEncoder().default(obj.model)
return model
return super().default(obj)