class Single(object):
def __init__(self, cfg):
cfg = dotdict(cfg)
self.bootstrap_servers = cfg.bootstrap_servers
self.client_id = cfg.client_id
self.topics = cfg.topics
def __new__(cls, *args):
bootstrap_servers = args[0]['bootstrap_servers']
client_id = args[0]['client_id']
topics = args[0]['topics']
if not hasattr(cls, '_users'):
cls._users = list()
orig = super(Single, cls)
ob = orig.__new__(cls)
cls._users.append(ob)
elif not reduce(lambda x, y: x or y, map(lambda x: x.bootstrap_servers == bootstrap_servers, cls._users), False):
orig = super(Single, cls)
ob = orig.__new__(cls)
cls._users.append(ob)
elif not reduce(lambda x1, y1: x1 or y1, map(lambda x1: x1.client_id == client_id, cls._users), False):
orig = super(Single, cls)
ob = orig.__new__(cls)
cls._users.append(ob)
elif not reduce(lambda x2, y2: x2 or y2,
map(lambda x2: len(list(set(topics).difference(set(x2.topics)))) == 0, cls._users), False):
orig = super(Single, cls)
ob = orig.__new__(cls)
cls._users.append(ob)
else:
for u in cls._users:
if u.bootstrap_servers == bootstrap_servers:
ob = u
return ob
|