Skip to content

Commit 3eb6129

Browse files
kavehshahedibhufmann
authored andcommitted
Handle tags as bitmask instead of enum
Signed-off-by: Kaveh Shahedi <[email protected]>
1 parent 413a65c commit 3eb6129

File tree

3 files changed

+53
-35
lines changed

3 files changed

+53
-35
lines changed

test_tsp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,12 @@ def test_fetch_virtual_table_lines(self, ust):
530530
assert line.index is not None
531531
if i == 0:
532532
assert line.index == LOW_INDEX
533-
assert line.tags is VirtualTableTag.NO_TAGS
533+
assert line.tags == VirtualTableTag.NO_TAGS
534534

535535
assert len(line.cells) > 0
536536
for cell in line.cells:
537537
assert cell.content is not None
538-
assert cell.tags is VirtualTableTag.NO_TAGS
538+
assert cell.tags == VirtualTableTag.NO_TAGS
539539

540540
self._delete_experiments()
541541
self._delete_traces()

tsp/virtual_table_model.py

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,35 @@ def __init__(self, params):
104104

105105
self.tags = VirtualTableTag.NO_TAGS
106106
if TAGS_KEY in params:
107-
if params.get(TAGS_KEY) is not None and type(params.get(TAGS_KEY)) is int:
107+
if params.get(TAGS_KEY) is not None and isinstance(params.get(TAGS_KEY), int):
108108
tags = int(params.get(TAGS_KEY))
109-
110-
match tags:
111-
case 0: # Tag 0 is used for no tags
112-
self.tags = VirtualTableTag.NO_TAGS
113-
case 1 | 2: # Tags 1 and 2 are reserved
114-
self.tags = VirtualTableTag.RESERVED
115-
case 4: # Tag 4 is used for border
116-
self.tags = VirtualTableTag.BORDER
117-
case 8: # Tag 8 is used for highlight
118-
self.tags = VirtualTableTag.HIGHLIGHT
119-
case _: # Other tags are not supported
120-
self.tags = VirtualTableTag.NO_TAGS
109+
110+
self.tags = VirtualTableTag.NO_TAGS # Tag 0 is used for no tags
111+
if tags & 0x1: # Tags 1 and 2 are reserved
112+
self.tags |= VirtualTableTag.RESERVED_1
113+
if tags & 0x2: # Tags 1 and 2 are reserved
114+
self.tags |= VirtualTableTag.RESERVED_2
115+
if tags & 0x4: # Tag 4 is used for border
116+
self.tags |= VirtualTableTag.BORDER
117+
if tags & 0x8: # Tag 8 is used for highlight
118+
self.tags |= VirtualTableTag.HIGHLIGHT
119+
121120
del params[TAGS_KEY]
122121

122+
def has_tag(self, tag):
123+
return bool(self.tags & tag)
124+
123125
def print(self):
124126

125127
print(f" index: {self.index}")
126-
print(f" tags: {self.tags.name}")
128+
if self.tags == VirtualTableTag.NO_TAGS:
129+
print(" tags: NO_TAGS")
130+
else:
131+
active_tags = []
132+
for tag in VirtualTableTag:
133+
if tag != VirtualTableTag.NO_TAGS and self.has_tag(tag):
134+
active_tags.append(tag.name)
135+
print(f" tags: {' | '.join(active_tags)}")
127136
print(" cells:")
128137
for i, cell in enumerate(self.cells):
129138
cell.print()
@@ -144,23 +153,31 @@ def __init__(self, params):
144153

145154
self.tags = VirtualTableTag.NO_TAGS
146155
if TAGS_KEY in params:
147-
if params.get(TAGS_KEY) is not None and type(params.get(TAGS_KEY)) is int:
156+
if params.get(TAGS_KEY) is not None and isinstance(params.get(TAGS_KEY), int):
148157
tags = int(params.get(TAGS_KEY))
158+
159+
self.tags = VirtualTableTag.NO_TAGS # Tag 0 is used for no tags
160+
if tags & 0x1: # Tags 1 and 2 are reserved
161+
self.tags |= VirtualTableTag.RESERVED_1
162+
if tags & 0x2: # Tags 1 and 2 are reserved
163+
self.tags |= VirtualTableTag.RESERVED_2
164+
if tags & 0x4: # Tag 4 is used for border
165+
self.tags |= VirtualTableTag.BORDER
166+
if tags & 0x8: # Tag 8 is used for highlight
167+
self.tags |= VirtualTableTag.HIGHLIGHT
149168

150-
match tags:
151-
case 0: # Tag 0 is used for no tags
152-
self.tags = VirtualTableTag.NO_TAGS
153-
case 1 | 2: # Tags 1 and 2 are reserved
154-
self.tags = VirtualTableTag.RESERVED
155-
case 4: # Tag 4 is used for border
156-
self.tags = VirtualTableTag.BORDER
157-
case 8: # Tag 8 is used for highlight
158-
self.tags = VirtualTableTag.HIGHLIGHT
159-
case _: # Other tags are not supported
160-
self.tags = VirtualTableTag.NO_TAGS
161169
del params[TAGS_KEY]
162170

171+
def has_tag(self, tag):
172+
return bool(self.tags & tag)
173+
163174
def print(self):
164175
print(f" \"{TABLE_LINE_CELL_CONTENT_KEY}\": \"{self.content}\"")
165-
print(f" \"tags\": {self.tags.name}")
176+
if self.tags == VirtualTableTag.NO_TAGS:
177+
tags_str = "NO_TAGS"
178+
else:
179+
active_tags = [tag.name for tag in VirtualTableTag if tag != VirtualTableTag.NO_TAGS and self.has_tag(tag)]
180+
tags_str = " | ".join(active_tags)
181+
182+
print(f" \"tags\": \"{tags_str}\"")
166183
print(f" {'-' * 10}")

tsp/virtual_table_tag.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from enum import Enum
1+
from enum import Flag, auto
22

3-
class VirtualTableTag(Enum):
3+
class VirtualTableTag(Flag):
44
'''
55
Tag is a bit mask to apply for tagging elements (e.g. table lines, states).
66
This can be used by the server to indicate if a filter matches and what action to apply.
@@ -9,19 +9,20 @@ class VirtualTableTag(Enum):
99
'''
1010
Simply no tags
1111
'''
12-
NO_TAGS = 'NO_TAGS'
12+
NO_TAGS = 0
1313

1414
'''
1515
Some tags are reserved for the server
1616
'''
17-
RESERVED = 'RESERVED'
17+
RESERVED_1 = auto()
18+
RESERVED_2 = auto()
1819

1920
'''
2021
Border tag
2122
'''
22-
BORDER = 'BORDER'
23+
BORDER = auto()
2324

2425
'''
2526
Highlight tag
2627
'''
27-
HIGHLIGHT = 'HIGHLIGHT'
28+
HIGHLIGHT = auto()

0 commit comments

Comments
 (0)