5414e2156351f671c5fc43cf7a5de5022e4760d8
[logging-analytics.git] / pylog / onaplogging / marker / marker.py
1 # Copyright 2018 ke liang <lokyse@163.com>.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import abc
16
17 MARKER_TAG = "marker"
18
19
20 class Marker(object):
21
22     __metaclass__ = abc.ABCMeta
23
24     @abc.abstractmethod
25     def getName(self):
26         raise NotImplementedError()
27
28     @abc.abstractmethod
29     def contains(self, item=None):
30         raise NotImplementedError()
31
32     @abc.abstractmethod
33     def addChild(self, item):
34         raise NotImplementedError()
35
36     @abc.abstractmethod
37     def removeChild(self, item):
38         raise NotImplementedError()
39
40     @abc.abstractmethod
41     def __eq__(self, other):
42         raise NotImplementedError()
43
44     @abc.abstractmethod
45     def __hash__(self):
46         raise NotImplementedError()
47
48     @abc.abstractmethod
49     def __iter__(self):
50         raise NotImplementedError()
51
52
53 class BaseMarker(Marker):
54
55     def __init__(self, name):
56         super(BaseMarker, self).__init__()
57         if not isinstance(name, str):
58             raise TypeError("not str type")
59         if name == "":
60             raise ValueError("empty value")
61         self.__name = name
62         self.__childs = []
63
64     def getName(self):
65         return self.__name
66
67     def __iter__(self):
68         return iter(self.__childs)
69
70     def __eq__(self, other):
71
72         if not isinstance(other, Marker):
73             return False
74         if id(self) == id(other):
75             return True
76
77         return self.__name == other.getName()
78
79     def __hash__(self):
80         return hash(self.__name)
81
82     def contains(self, item=None):
83
84         if isinstance(item, Marker):
85             if item == self:
86                 return True
87             return len(list(filter(
88                 lambda x: x == item, self.__childs))) > 0
89
90         elif isinstance(item, str):
91             if item == self.__name:
92                 return True
93
94             return len(list(filter(
95                 lambda x: x.__name == item, self.__childs))) > 0
96
97         return False
98
99     def addChild(self, item):
100         if not isinstance(item, Marker):
101             raise TypeError("can only add  (not %s) marker type"
102                             % type(item))
103         if self == item:
104             return
105         if item not in self.__childs:
106             self.__childs.append(item)
107
108     def addChilds(self, childs):
109         try:
110             iter(childs)
111         except Exception as e:
112             raise e
113
114         for item in childs:
115             self.addChild(item)
116
117     def removeChild(self, item):
118         if not isinstance(item, Marker):
119             raise TypeError("can only add  (not %s) marker type"
120                             % type(item))
121         if item in self.__childs:
122             self.__childs.remove(item)
123
124
125 def matchMarkerHelp(record, markerToMatch):
126
127     marker = getattr(record, MARKER_TAG, None)
128
129     if marker is None or markerToMatch is None:
130         return False
131
132     if not isinstance(marker, Marker):
133         return False
134
135     try:
136         if isinstance(markerToMatch, list):
137             return len(list(filter(
138                 lambda x: marker.contains(x), markerToMatch))) > 0
139
140         return marker.contains(markerToMatch)
141     except Exception as e:
142         raise e