forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtags.py
More file actions
180 lines (127 loc) · 4.87 KB
/
tags.py
File metadata and controls
180 lines (127 loc) · 4.87 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
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from robot.utils import is_string, normalize, NormalizedDict, Matcher, unic
class Tags:
__slots__ = ['_tags']
def __init__(self, tags=None):
self._tags = self._init_tags(tags)
def _init_tags(self, tags):
if not tags:
return ()
if is_string(tags):
tags = (tags,)
return self._normalize(tags)
def _normalize(self, tags):
normalized = NormalizedDict([(unic(t), None) for t in tags], ignore='_')
for remove in '', 'NONE':
if remove in normalized:
normalized.pop(remove)
return tuple(normalized)
def add(self, tags):
self._tags = self._normalize(tuple(self) + tuple(Tags(tags)))
def remove(self, tags):
tags = TagPatterns(tags)
self._tags = tuple([t for t in self if not tags.match(t)])
def match(self, tags):
return TagPatterns(tags).match(self)
def __contains__(self, tags):
return self.match(tags)
def __len__(self):
return len(self._tags)
def __iter__(self):
return iter(self._tags)
def __str__(self):
return '[%s]' % ', '.join(self)
def __repr__(self):
return repr(list(self))
def __eq__(self, other):
if not isinstance(other, Tags):
return False
self_normalized = [normalize(tag, ignore='_') for tag in self]
other_normalized = [normalize(tag, ignore='_') for tag in other]
return sorted(self_normalized) == sorted(other_normalized)
def __getitem__(self, index):
item = self._tags[index]
return item if not isinstance(index, slice) else Tags(item)
def __add__(self, other):
return Tags(tuple(self) + tuple(Tags(other)))
class TagPatterns:
def __init__(self, patterns):
self._patterns = tuple(TagPattern(p) for p in Tags(patterns))
def match(self, tags):
tags = tags if isinstance(tags, Tags) else Tags(tags)
return any(p.match(tags) for p in self._patterns)
def __contains__(self, tag):
return self.match(tag)
def __len__(self):
return len(self._patterns)
def __iter__(self):
return iter(self._patterns)
def __getitem__(self, index):
return self._patterns[index]
def __str__(self):
return '[%s]' % ', '.join(str(pattern) for pattern in self)
def TagPattern(pattern):
pattern = pattern.replace(' ', '')
if 'NOT' in pattern:
return NotTagPattern(*pattern.split('NOT'))
if 'OR' in pattern:
return OrTagPattern(pattern.split('OR'))
if 'AND' in pattern or '&' in pattern:
return AndTagPattern(pattern.replace('&', 'AND').split('AND'))
return SingleTagPattern(pattern)
class SingleTagPattern:
def __init__(self, pattern):
self._matcher = Matcher(pattern, ignore='_')
def match(self, tags):
return self._matcher.match_any(tags)
def __iter__(self):
yield self
def __str__(self):
return self._matcher.pattern
def __bool__(self):
return bool(self._matcher)
class AndTagPattern:
def __init__(self, patterns):
self._patterns = tuple(TagPattern(p) for p in patterns)
def match(self, tags):
return all(p.match(tags) for p in self._patterns)
def __iter__(self):
return iter(self._patterns)
def __str__(self):
return ' AND '.join(str(pattern) for pattern in self)
class OrTagPattern:
def __init__(self, patterns):
self._patterns = tuple(TagPattern(p) for p in patterns)
def match(self, tags):
return any(p.match(tags) for p in self._patterns)
def __iter__(self):
return iter(self._patterns)
def __str__(self):
return ' OR '.join(str(pattern) for pattern in self)
class NotTagPattern:
def __init__(self, must_match, *must_not_match):
self._first = TagPattern(must_match)
self._rest = OrTagPattern(must_not_match)
def match(self, tags):
if not self._first:
return not self._rest.match(tags)
return self._first.match(tags) and not self._rest.match(tags)
def __iter__(self):
yield self._first
for pattern in self._rest:
yield pattern
def __str__(self):
return ' NOT '.join(str(pattern) for pattern in self).lstrip()