This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy path_dashboard_scope.py
More file actions
103 lines (83 loc) · 2.65 KB
/
_dashboard_scope.py
File metadata and controls
103 lines (83 loc) · 2.65 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
import tatsu
def convert_scope_string_to_expression(scope=None):
if scope is None or not scope:
return [True, []]
_SCOPE_GRAMMAR = r"""
@@grammar::CALC
start = expression $ ;
expression
=
| operand simple_operator word
| operand multiple_operator multiple_value
;
simple_operator
=
| 'is not'
| 'is'
| 'contains'
| 'does not contain'
| 'starts with'
| '='
;
multiple_operator
=
| 'not in'
| 'in'
;
operand = /[a-zA-Z0-9_\-\.]+/ ;
multiple_value
=
| '[' word_array ']'
| word
;
word_array
=
| word ',' word_array
| word
;
word =
| /[a-zA-Z0-9-_\-\.]+/
| '"' /[a-zA-Z0-9-_\-\.]+/ '"'
| "'" /[a-zA-Z0-9-_\-\.]+/ "'"
;
"""
def flatten(S):
if S == [] or S == ():
return list(S)
if isinstance(S[0], list) or isinstance(S[0], tuple):
return flatten(S[0]) + flatten(S[1:])
return list(S[:1]) + flatten(S[1:])
try:
grammar = tatsu.compile(_SCOPE_GRAMMAR)
scope_list = []
scope_expressions = scope.strip(' \t\n\r').split(' and ')
for scope in scope_expressions:
operand, parsed_operator, value = grammar.parse(scope)
operator_match = {
"is": "equals",
"=": "equals",
"is not": "notEquals",
"in": "in",
"not in": "notIn",
"contains": "contains",
"does not contain": "notContains",
"starts with": "startsWith",
}
if isinstance(value, tuple) or isinstance(value, list):
value = flatten(value)
if len(value) > 1:
value = list(value[1:-1]) # Remove '[' and ']'
value = [elem for elem in value if elem != ','] # Remove ','
else:
value = [value]
operator = "" if parsed_operator not in operator_match else operator_match[parsed_operator]
scope_list.append({
'displayName': "",
"isVariable": False,
'operand': operand,
'operator': operator,
'value': value
})
return [True, scope_list]
except Exception as ex:
return [False, f"invalid scope: {scope}, {ex.message}"]