2019-10-16 16:53:37 +02:00
|
|
|
"""
|
|
|
|
Singleton decorator definition
|
|
|
|
"""
|
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
2017-01-10 21:15:35 +01:00
|
|
|
def Singleton(cls):
|
2019-10-16 16:53:37 +02:00
|
|
|
"""
|
|
|
|
Decorator implementing the singleton pattern:
|
|
|
|
it restricts the instantiation of a class to one "single" instance.
|
|
|
|
"""
|
2017-01-10 21:15:35 +01:00
|
|
|
instances = {}
|
2019-10-16 16:53:37 +02:00
|
|
|
|
|
|
|
# https://github.com/sphinx-doc/sphinx/issues/3783
|
|
|
|
@wraps(cls)
|
2017-01-10 21:15:35 +01:00
|
|
|
def getinstance():
|
2019-10-16 16:53:37 +02:00
|
|
|
"""Find an instance or save newly created one"""
|
2017-01-10 21:15:35 +01:00
|
|
|
if cls not in instances:
|
|
|
|
instances[cls] = cls()
|
|
|
|
return instances[cls]
|
|
|
|
return getinstance
|