almost singleton

This commit is contained in:
root 2012-10-25 21:19:34 +02:00
parent 54c2dbb64b
commit 87af557bf1

View file

@ -188,20 +188,22 @@ class YunoHostError(Exception):
else: else:
self.desc = code self.desc = code
def singleton(cls):
class Singleton(object):
instances = {} instances = {}
def get_instance(): def __new__(cls, *args, **kwargs):
if cls not in instances: if cls not in cls.instances:
instances[cls] = cls() cls.instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)
return instances[cls] return cls.instances[cls]
return get_instance
@singleton
class YunoHostLDAP(object): class YunoHostLDAP(Singleton):
""" Specific LDAP functions for YunoHost """ """ Specific LDAP functions for YunoHost """
pwd = False
conn = ldap.initialize('ldap://localhost:389')
base = 'dc=yunohost,dc=org'
def __enter__(self, password=False): def __enter__(self):
self.__init__(password)
return self return self
def __init__(self, password=False): def __init__(self, password=False):
@ -211,10 +213,8 @@ class YunoHostLDAP(object):
Initialize to localhost, base yunohost.org, prompt for password Initialize to localhost, base yunohost.org, prompt for password
""" """
self.conn = ldap.initialize('ldap://localhost:389') if password: self.pwd = password
self.base = 'dc=yunohost,dc=org' elif self.pwd: pass
if password:
self.pwd = password
else: else:
try: try:
self.pwd = getpass.getpass(colorize(_('Admin Password: '), 'yellow')) self.pwd = getpass.getpass(colorize(_('Admin Password: '), 'yellow'))
@ -236,12 +236,12 @@ class YunoHostLDAP(object):
Boolean | YunoHostError Boolean | YunoHostError
""" """
try: #try:
self.conn.unbind_s() self.conn.unbind_s()
except: #except:
raise YunoHostError(169, _('An error occured during disconnection')) # raise YunoHostError(169, _('An error occured during disconnection'))
else: #else:
return True # return True
def search(self, base=None, filter='(objectClass=*)', attrs=['dn']): def search(self, base=None, filter='(objectClass=*)', attrs=['dn']):