1 # Copyright 2018 ke liang <lokyse@163.com>.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
22 __metaclass__ = abc.ABCMeta
26 raise NotImplementedError()
29 def contains(self, item=None):
30 raise NotImplementedError()
33 def addChild(self, item):
34 raise NotImplementedError()
37 def removeChild(self, item):
38 raise NotImplementedError()
41 def __eq__(self, other):
42 raise NotImplementedError()
46 raise NotImplementedError()
50 raise NotImplementedError()
53 class BaseMarker(Marker):
55 def __init__(self, name):
56 super(BaseMarker, self).__init__()
57 if not isinstance(name, str):
58 raise TypeError("not str type")
60 raise ValueError("empty value")
68 return iter(self.__childs)
70 def __eq__(self, other):
72 if not isinstance(other, Marker):
74 if id(self) == id(other):
77 return self.__name == other.getName()
80 return hash(self.__name)
82 def contains(self, item=None):
84 if isinstance(item, Marker):
87 return len(list(filter(
88 lambda x: x == item, self.__childs))) > 0
90 elif isinstance(item, str):
91 if item == self.__name:
94 return len(list(filter(
95 lambda x: x.__name == item, self.__childs))) > 0
99 def addChild(self, item):
100 if not isinstance(item, Marker):
101 raise TypeError("can only add (not %s) marker type"
105 if item not in self.__childs:
106 self.__childs.append(item)
108 def addChilds(self, childs):
111 except Exception as e:
117 def removeChild(self, item):
118 if not isinstance(item, Marker):
119 raise TypeError("can only add (not %s) marker type"
121 if item in self.__childs:
122 self.__childs.remove(item)
125 def matchMarkerHelp(record, markerToMatch):
127 marker = getattr(record, MARKER_TAG, None)
129 if marker is None or markerToMatch is None:
132 if not isinstance(marker, Marker):
136 if isinstance(markerToMatch, list):
137 return len(list(filter(
138 lambda x: marker.contains(x), markerToMatch))) > 0
140 return marker.contains(markerToMatch)
141 except Exception as e: