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.
17 from typing import Iterable, List, Optional, Union, Iterator
18 from deprecated import deprecated
19 from warnings import warn
20 from logging import LogRecord
26 """Abstract class for defining the marker structure.
29 after deprecated child methods are removed, rename them here.
42 __metaclass__ = abc.ABCMeta
46 raise NotImplementedError()
49 def contains(self, item=None):
50 raise NotImplementedError()
53 def addChild(self, item):
54 raise NotImplementedError()
57 def removeChild(self, item):
58 raise NotImplementedError()
61 def __eq__(self, other):
62 raise NotImplementedError()
66 raise NotImplementedError()
70 raise NotImplementedError()
73 class BaseMarker(Marker):
74 """Basic marker class.
76 It is a marker with base functionalities that add sub-level markers and
77 check if another marker exists as the parent itself or as its child.
82 name : The name of the marker.
83 children (list) : The list of all children (sub-level) markers.
85 name (str) : The name of the marker.
87 getName : Returns the name of the marker.
88 addChild : Adds a sub-level marker.
89 addChilds : Adds a list of sub-level markers.
90 removeChild : Removes a specified sub-level marker.
91 contains : Checks if a sub-level marker exists.
97 """Name of the parent marker."""
102 # type: () -> List[Marker]
103 """Child markers of one parent marker."""
107 def name(self, value):
108 # type: (str) -> None
112 def children(self, value):
113 # type: (List[Marker]) -> None
114 self.__childs = value
116 def __init__(self, name): # type: (str)
119 TypeError : If the `name` parameter is not a string.
120 ValueError : If the `name` parameter is an empty string.
123 super(BaseMarker, self).__init__()
125 if not isinstance(name, str):
126 raise TypeError("not str type")
129 raise ValueError("empty value")
131 warn("Attribute `__childs` is replaced by the property `children`."
132 "Use children instead.", DeprecationWarning)
137 def add_child(self, marker):
138 # type: (Marker) -> None
139 """Append a marker to child markers.
141 Use this method to describe a different level of logs. For example,
142 error log would use the ERROR marker. However it's possible to
143 create a, for instance, TYPE_ERROR to mark type related events.
144 In this case TYPE_ERROR will be a child of parent ERROR.
147 marker : marker describing a different log level.
149 TypeError : if the marker object has different type.
152 if not isinstance(marker, Marker):
153 raise TypeError("Bad marker type. \
154 Can only add markers of type Marker. \
155 Type %s was passed." % type(marker))
160 if marker not in self.children:
161 self.children.append(marker)
163 def add_children(self, markers):
164 # type: (Iterable[List]) -> None
165 """ Append a list of markers to child markers.
168 markers : An iterable object, containing markers.
170 Exception : If `marker` parameter is not iterable.
175 except Exception as e:
178 for marker in markers:
179 self.children.append(marker)
181 def remove_child(self, marker):
182 # type: (Marker) -> None
183 """Use this method to remove a marker from the children list.
186 marker : A child marker object.
188 TypeError: if the marker object has different type.
191 if not isinstance(marker, Marker):
192 raise TypeError("Bad marker type. \
193 Can only add markers of type Marker. \
194 Type %s was passed." % type(marker))
196 if marker in self.children:
197 self.children.remove(marker)
199 def contains(self, item=None):
200 # type: (Optional[Union[Marker, str]]) -> bool
202 Use it to check if a marker exists as a parent itself or its chidren.
205 item : A child marker object. Defaults to None.
207 bool : True if the marker exists.
210 warn("`item` argument will be replaced with `marker`. "
211 "Default value None will be removed.",
215 if isinstance(marker, Marker):
218 return len(list(filter(
219 lambda x: x == marker, self.children))) > 0
221 elif isinstance(marker, str):
222 if marker == self.name:
225 return len(list(filter(
226 lambda x: x.name == marker, self.children))) > 0
231 # type: () -> Iterator[List[Marker]]
232 return iter(self.__childs)
236 return hash(self.__name)
238 def __eq__(self, other):
239 # type: (Marker) -> bool
240 if not isinstance(other, Marker):
242 if id(self) == id(other):
245 return self.__name == other.getName()
247 @deprecated(reason="Will be removed. Call the `name` property instead.")
249 """Class attribute getter."""
252 @deprecated(reason="Will be removed. Call add_children(markers) instead.")
253 def addChilds(self, childs):
254 """Add a list of sub-level markers. See add_children(markers)"""
255 self.add_children(childs)
257 @deprecated(reason="Will be removed. Call add_child(marker) instead.")
258 def addChild(self, item):
259 """Add a sub-level marker. See add_child(marker)"""
262 @deprecated(reason="Will be removed. Call remove_child(marker) instead.")
263 def removeChild(self, item):
264 """Remove a sub-level marker. See remove_child(marker)"""
265 self.remove_child(item)
268 @deprecated(reason="Will be removed. "
269 "Call match_marker(record, marker_to_match) instead.")
270 def matchMarkerHelp(record, markerToMatch):
271 """See match_marker(record, marker_to_match)."""
272 return match_markers(record, markerToMatch)
275 def match_markers(record, marker_to_match):
276 # type: (LogRecord, Union[Marker, List]) -> bool
278 Use this method to match a marker (or a list of markers) with a LogRecord
282 record : a record that may contain a marker.
283 markerToMatch : a marker or a list of markers.
285 Exception : if match check went wrong.
287 bool : whether the check can be done or the marker is found.
289 record_marker = getattr(record, MARKER_TAG, None)
291 if record_marker is None or \
292 marker_to_match is None:
295 if not isinstance(record_marker, Marker):
299 if isinstance(marker_to_match, list):
300 return len(list(filter(
301 lambda x: record_marker.contains(x), marker_to_match))) > 0
303 return record_marker.contains(marker_to_match)
304 except Exception as e: