From 87af557bf18994ba5245641e1187866d49aad7d7 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 25 Oct 2012 21:19:34 +0200 Subject: [PATCH] almost singleton --- yunohost.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/yunohost.py b/yunohost.py index e05bbb68..63e299a4 100644 --- a/yunohost.py +++ b/yunohost.py @@ -188,20 +188,22 @@ class YunoHostError(Exception): else: self.desc = code -def singleton(cls): + +class Singleton(object): instances = {} - def get_instance(): - if cls not in instances: - instances[cls] = cls() - return instances[cls] - return get_instance + def __new__(cls, *args, **kwargs): + if cls not in cls.instances: + cls.instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs) + return cls.instances[cls] -@singleton -class YunoHostLDAP(object): + +class YunoHostLDAP(Singleton): """ Specific LDAP functions for YunoHost """ + pwd = False + conn = ldap.initialize('ldap://localhost:389') + base = 'dc=yunohost,dc=org' - def __enter__(self, password=False): - self.__init__(password) + def __enter__(self): return self def __init__(self, password=False): @@ -211,10 +213,8 @@ class YunoHostLDAP(object): Initialize to localhost, base yunohost.org, prompt for password """ - self.conn = ldap.initialize('ldap://localhost:389') - self.base = 'dc=yunohost,dc=org' - if password: - self.pwd = password + if password: self.pwd = password + elif self.pwd: pass else: try: self.pwd = getpass.getpass(colorize(_('Admin Password: '), 'yellow')) @@ -236,12 +236,12 @@ class YunoHostLDAP(object): Boolean | YunoHostError """ - try: - self.conn.unbind_s() - except: - raise YunoHostError(169, _('An error occured during disconnection')) - else: - return True + #try: + self.conn.unbind_s() + #except: + # raise YunoHostError(169, _('An error occured during disconnection')) + #else: + # return True def search(self, base=None, filter='(objectClass=*)', attrs=['dn']):