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.
18 from deprecated import deprecated
19 from warnings import warn
20 from typing import Dict, Optional
22 from .marker import Marker
23 from .marker import BaseMarker
25 lock = threading.RLock()
28 class IMarkerFactory(object):
29 """Abstract marker factory for defining structure.
32 after deprecated child methods are removed, rename them here.
43 __metaclass__ = abc.ABCMeta
46 def getMarker(self, marker_name=None):
47 raise NotImplementedError()
50 def deleteMarker(self, marker_name=None):
51 raise NotImplementedError()
54 def exist(self, marker_name=None):
55 raise NotImplementedError()
58 class MarkerFactory(IMarkerFactory):
59 """A factory class maganing every marker.
61 It is designed to check the existance, create and remove single markers.
62 This class follows a singleton pattern - only one instance can be created.
67 marker_map : a map of existing markers.
69 _instance : a marker factory instance.
71 getMarker : creates a new marker or returns an available one.
72 deleteMarker : removes a specific marker.
73 exist : checks if a specific marker exists.
82 if not hasattr(self, '_marker_map'):
84 return self._marker_map
86 def get_marker(self, name=None):
87 # type: (Optional[str]) -> Marker
89 Use it to get any marker by its name. If it doesn't exist - it
90 will create a new marker that will be added to the factory.
91 Blocks the thread while executing.
94 name : A marker name. Defaults to None.
96 ValueError : If `name` is None.
98 Marker : A found or just newly created marker.
102 raise ValueError("Marker name is None. Must have a str value.")
106 marker = self.marker_map.get(name, None)
109 marker = BaseMarker(name)
110 self.marker_map[name] = marker
116 def delete_marker(self, name=None):
117 # type: (Optional[str]) -> bool
120 name: A marker name. Defaults to None.
122 bool: The status of deletion.
126 exists = self.exists(name)
128 del self.marker_map[name]
134 def exists(self, name=None):
135 # type: (Optional[str]) -> bool
137 Checks whether the search for a marker returns None and returns the
138 status of the operation.
141 name: marker name. Defaults to None.
143 bool: status of whether the marker was found.
145 marker = self.marker_map.get(name, None)
146 return marker is not None
148 def __new__(cls, *args, **kwargs):
149 if cls._instance is None:
150 cls._instance = super(MarkerFactory, cls).__new__(cls)
152 warn("_marker_map attribute will be replaced by marker_map property.",
156 @deprecated(reason="Will be removed. Call exists(name) instead.")
157 def exist(self, marker_name=None):
158 return self.exists(marker_name)
160 @deprecated(reason="Will be removed. Call get_marker(name) instead.")
161 def getMarker(self, marker_name=None):
162 return self.get_marker(marker_name)
164 @deprecated(reason="Will be removed. Call delete_marker(name) instead.")
165 def deleteMarker(self, marker_name=None):
166 return self.delete_marker(marker_name)