Island, Common: Move singleton to code_utils.py in common

Singleton is a common pattern, potentially usable in the Agent so it belongs in common
This commit is contained in:
vakarisz 2022-04-27 18:10:23 +03:00
parent fdf8198e7f
commit 65eb9b171b
2 changed files with 11 additions and 9 deletions

View File

@ -10,3 +10,12 @@ class abstractstatic(staticmethod):
function.__isabstractmethod__ = True
__isabstractmethod__ = True
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]

View File

@ -1,17 +1,10 @@
import logging
from common.utils.code_utils import Singleton
logger = logging.getLogger(__name__)
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class ReportExporterManager(object, metaclass=Singleton):
def __init__(self):
self._exporters_set = set()