Move singleton logic in function

This commit is contained in:
sfigato 2020-08-29 20:10:56 +02:00
parent d9a6db63d7
commit e41e03f464
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -63,6 +63,15 @@ def inheritance(obj, attr, root_cls=object):
return res.strip(",")
def singletonize(cls, name):
"""
Helper function to be plugged in `__new__` method to make the class a singleton.
"""
if name not in cls._instances:
cls._instances[name] = object.__new__(cls)
return cls._instances[name]
async def iter_children(children):
return [child async for child in children]
@ -137,10 +146,7 @@ class Hackers(Entry):
_instances: T.Dict[str, Entry] = dict()
def __new__(cls, client, *args, **kwargs):
_name = f"{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
return singletonize(cls, f"{cls._name}-{id(client)}")
def __init__(self, client, *args, **kwargs):
super().__init__(client)
@ -175,10 +181,7 @@ class Robots(Entry):
_instances: T.Dict[str, Entry] = dict()
def __new__(cls, client, *args, **kwargs):
_name = f"{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
return singletonize(cls, f"{cls._name}-{id(client)}")
def __init__(self, client, *args, **kwargs):
super().__init__(client)
@ -202,10 +205,7 @@ class Congregations(Entry):
_instances: T.Dict[str, Entry] = dict()
def __new__(cls, client, *args, **kwargs):
_name = f"{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
return singletonize(cls, f"{cls._name}-{id(client)}")
def __init__(self, client, *args, **kwargs):
super().__init__(client)
@ -239,13 +239,10 @@ class User(Hackers):
_instances: T.Dict[str, Entry] = dict()
object_class = ["inetOrgPerson", "organizationalPerson", "person", "top"]
def __new__(cls, name, client, *args, **kwargs):
_name = f"{name}-{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
def __new__(cls, client, name, *args, **kwargs):
return singletonize(cls, f"{name}-{id(client)}")
def __init__(self, name, client, *args, **kwargs):
def __init__(self, client, name, *args, **kwargs):
super().__init__(client, *args, **kwargs)
self._name = name
self._entry = LDAPEntry(self.dn)
@ -342,11 +339,8 @@ class Service(Robots):
_instances: T.Dict[str, Entry] = dict()
object_class = ["account", "top", "simpleSecurityObject"]
def __new__(cls, name, client, *args, **kwargs):
_name = f"{name}-{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
def __new__(cls, client, name, *args, **kwargs):
return singletonize(cls, f"{name}-{id(client)}")
def __init__(self, name, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -404,11 +398,8 @@ class Group(Congregations):
_instances: T.Dict[str, Entry] = dict()
object_class = ["groupOfNames", "top"]
def __new__(cls, name, client, *args, **kwargs):
_name = f"{name}-{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
def __new__(cls, client, name, *args, **kwargs):
return singletonize(cls, f"{name}-{id(client)}")
def __init__(self, name, *args, **kwargs):
super().__init__(*args, **kwargs)