-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvirtual_table_model.py
More file actions
223 lines (188 loc) · 8.35 KB
/
virtual_table_model.py
File metadata and controls
223 lines (188 loc) · 8.35 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# The MIT License (MIT)
#
# Copyright (C) 2024 - 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.
"""VirtualTableModel class file."""
import json
from tsp.virtual_table_tag import VirtualTableTag
SIZE_KEY = "size"
LOW_INDEX_KEY = "lowIndex"
COLUMN_IDS_KEY = "columnIds"
LINES_KEY = "lines"
TAGS_KEY = "tags"
TABLE_LINE_INDEX_KEY = "index"
TABLE_LINE_CELLS_KEY = "cells"
TABLE_LINE_CELL_CONTENT_KEY = "content"
# pylint: disable=too-few-public-methods
class VirtualTableModel:
'''
Virtual table model that will be returned by the server
'''
def __init__(self, params):
# Size of the virtual table
self.size = 0
if SIZE_KEY in params:
if params.get(SIZE_KEY) is not None and type(params.get(SIZE_KEY)) is int:
self.size = int(params.get(SIZE_KEY))
del params[SIZE_KEY]
# Index of the first line in the virtual table
self.low_index = 0
if LOW_INDEX_KEY in params:
if params.get(LOW_INDEX_KEY) is not None and type(params.get(LOW_INDEX_KEY)) is int:
self.low_index = int(params.get(LOW_INDEX_KEY))
del params[LOW_INDEX_KEY]
# Array of column IDs in the virtual table
self.column_ids = []
if COLUMN_IDS_KEY in params:
if params.get(COLUMN_IDS_KEY) is not None:
for column_id in params.get(COLUMN_IDS_KEY):
self.column_ids.append(column_id)
del params[COLUMN_IDS_KEY]
# Array of lines in the virtual table
self.lines = []
if LINES_KEY in params:
for line in params.get(LINES_KEY):
self.lines.append(VirtualTableLine(line))
del params[LINES_KEY]
def to_json(self):
return json.dumps(self, cls=VirtualTableModelEncoder, indent=4)
def print(self):
print("VirtualTableModel:")
print(f" size: {self.size}")
print(f" low_index: {self.low_index}")
print(f" column_ids: {self.column_ids}")
print(" lines:")
for i, line in enumerate(self.lines):
line.print()
class VirtualTableModelEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, VirtualTableModel):
result = {}
result[SIZE_KEY] = obj.size
result[LOW_INDEX_KEY] = obj.low_index
result[COLUMN_IDS_KEY] = obj.column_ids
result[LINES_KEY] = [ VirtualTableLineEncoder().default(line) for line in obj.lines ]
return result
return super().default(obj)
class VirtualTableLine:
'''
Virtual table line that will be returned by the server
'''
def __init__(self, params):
# Index of the line in the virtual table
self.index = -1
if TABLE_LINE_INDEX_KEY in params:
if params.get(TABLE_LINE_INDEX_KEY) is not None and type(params.get(TABLE_LINE_INDEX_KEY)) is int:
self.index = int(params.get(TABLE_LINE_INDEX_KEY))
del params[TABLE_LINE_INDEX_KEY]
# Array of cells in the line
self.cells = []
if TABLE_LINE_CELLS_KEY in params:
if params.get(TABLE_LINE_CELLS_KEY) is not None:
for cell in params.get(TABLE_LINE_CELLS_KEY):
self.cells.append(VirtualTableLineCell(cell))
del params[TABLE_LINE_CELLS_KEY]
self.tags = VirtualTableTag.NO_TAGS
if TAGS_KEY in params:
if params.get(TAGS_KEY) is not None and isinstance(params.get(TAGS_KEY), int):
tags = int(params.get(TAGS_KEY))
self.tags = VirtualTableTag.NO_TAGS # Tag 0 is used for no tags
if tags & 0x1: # Tags 1 and 2 are reserved
self.tags |= VirtualTableTag.RESERVED_1
if tags & 0x2: # Tags 1 and 2 are reserved
self.tags |= VirtualTableTag.RESERVED_2
if tags & 0x4: # Tag 4 is used for border
self.tags |= VirtualTableTag.BORDER
if tags & 0x8: # Tag 8 is used for highlight
self.tags |= VirtualTableTag.HIGHLIGHT
del params[TAGS_KEY]
def has_tag(self, tag):
return bool(self.tags & tag)
def to_json(self):
return json.dumps(self, cls=VirtualTableLineEncoder, indent=4)
def print(self):
print(f" index: {self.index}")
if self.tags == VirtualTableTag.NO_TAGS:
print(" tags: NO_TAGS")
else:
active_tags = []
for tag in VirtualTableTag:
if tag != VirtualTableTag.NO_TAGS and self.has_tag(tag):
active_tags.append(tag.name)
print(f" tags: {' | '.join(active_tags)}")
print(" cells:")
for i, cell in enumerate(self.cells):
cell.print()
print(f" {'-' * 30}")
class VirtualTableLineEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, VirtualTableLine):
result = {}
result[TABLE_LINE_INDEX_KEY] = obj.index
result[TAGS_KEY] = obj.tags.value
result[TABLE_LINE_CELLS_KEY] = [ VirtualTableLineCellEncoder().default(cell) for cell in obj.cells ]
return result
return super().default(obj)
class VirtualTableLineCell:
'''
Virtual table line cell that will be returned by the server
'''
def __init__(self, params):
# Content of the cell
self.content = None
if TABLE_LINE_CELL_CONTENT_KEY in params:
if params.get(TABLE_LINE_CELL_CONTENT_KEY) is not None:
self.content = params.get(TABLE_LINE_CELL_CONTENT_KEY)
del params[TABLE_LINE_CELL_CONTENT_KEY]
self.tags = VirtualTableTag.NO_TAGS
if TAGS_KEY in params:
if params.get(TAGS_KEY) is not None and isinstance(params.get(TAGS_KEY), int):
tags = int(params.get(TAGS_KEY))
self.tags = VirtualTableTag.NO_TAGS # Tag 0 is used for no tags
if tags & 0x1: # Tags 1 and 2 are reserved
self.tags |= VirtualTableTag.RESERVED_1
if tags & 0x2: # Tags 1 and 2 are reserved
self.tags |= VirtualTableTag.RESERVED_2
if tags & 0x4: # Tag 4 is used for border
self.tags |= VirtualTableTag.BORDER
if tags & 0x8: # Tag 8 is used for highlight
self.tags |= VirtualTableTag.HIGHLIGHT
del params[TAGS_KEY]
def has_tag(self, tag):
return bool(self.tags & tag)
def print(self):
print(f" \"{TABLE_LINE_CELL_CONTENT_KEY}\": \"{self.content}\"")
if self.tags == VirtualTableTag.NO_TAGS:
tags_str = "NO_TAGS"
else:
active_tags = [tag.name for tag in VirtualTableTag if tag != VirtualTableTag.NO_TAGS and self.has_tag(tag)]
tags_str = " | ".join(active_tags)
print(f" \"tags\": \"{tags_str}\"")
print(f" {'-' * 10}")
def to_json(self):
return json.dumps(self, cls=VirtualTableLineCell, indent=4)
class VirtualTableLineCellEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, VirtualTableLineCell):
return {
TABLE_LINE_CELL_CONTENT_KEY: obj.content,
TAGS_KEY: obj.tags.value
}
return super().default(obj)