Show More
@@ -185,7 +185,32 b' class SingletonConfigurable(Configurable):' | |||||
185 | """ |
|
185 | """ | |
186 |
|
186 | |||
187 | _instance = None |
|
187 | _instance = None | |
188 |
|
188 | |||
|
189 | @classmethod | |||
|
190 | def _walk_mro(cls): | |||
|
191 | """Walk the cls.mro() for parent classes that are also singletons | |||
|
192 | ||||
|
193 | For use in instance() | |||
|
194 | """ | |||
|
195 | ||||
|
196 | for subclass in cls.mro(): | |||
|
197 | if issubclass(cls, subclass) and \ | |||
|
198 | issubclass(subclass, SingletonConfigurable) and \ | |||
|
199 | subclass != SingletonConfigurable: | |||
|
200 | yield subclass | |||
|
201 | ||||
|
202 | @classmethod | |||
|
203 | def clear_instance(cls): | |||
|
204 | """unset _instance for this class and singleton parents. | |||
|
205 | """ | |||
|
206 | if not cls.initialized(): | |||
|
207 | return | |||
|
208 | for subclass in cls._walk_mro(): | |||
|
209 | if isinstance(subclass._instance, cls): | |||
|
210 | # only clear instances that are instances | |||
|
211 | # of the calling class | |||
|
212 | subclass._instance = None | |||
|
213 | ||||
189 | @classmethod |
|
214 | @classmethod | |
190 | def instance(cls, *args, **kwargs): |
|
215 | def instance(cls, *args, **kwargs): | |
191 | """Returns a global instance of this class. |
|
216 | """Returns a global instance of this class. | |
@@ -219,14 +244,10 b' class SingletonConfigurable(Configurable):' | |||||
219 | if cls._instance is None: |
|
244 | if cls._instance is None: | |
220 | inst = cls(*args, **kwargs) |
|
245 | inst = cls(*args, **kwargs) | |
221 | # Now make sure that the instance will also be returned by |
|
246 | # Now make sure that the instance will also be returned by | |
222 |
# |
|
247 | # parent classes' _instance attribute. | |
223 | for subclass in cls.mro(): |
|
248 | for subclass in cls._walk_mro(): | |
224 |
|
|
249 | subclass._instance = inst | |
225 | issubclass(subclass, SingletonConfigurable) and \ |
|
250 | ||
226 | subclass != SingletonConfigurable: |
|
|||
227 | subclass._instance = inst |
|
|||
228 | else: |
|
|||
229 | break |
|
|||
230 | if isinstance(cls._instance, cls): |
|
251 | if isinstance(cls._instance, cls): | |
231 | return cls._instance |
|
252 | return cls._instance | |
232 | else: |
|
253 | else: |
General Comments 0
You need to be logged in to leave comments.
Login now