py.lib
2022-08-14
Parent:cab7fedf8432
py.lib/db/ldap.py
+ Возможность обработки параметров конфигурации перед добавлением в класс конфигурации . Переформатирование части кода по PEP
1.1 --- a/db/ldap.py Sat Aug 13 20:06:47 2022 +0300 1.2 +++ b/db/ldap.py Sun Aug 14 12:19:08 2022 +0300 1.3 @@ -2,83 +2,83 @@ 1.4 1.5 from ldap3 import Server, Connection, SIMPLE, SUBTREE 1.6 1.7 -LDAP_PAGE=1000 1.8 +LDAP_PAGE = 1000 1.9 + 1.10 1.11 class LdapError(Exception): pass 1.12 1.13 -class LdapRes(): 1.14 - def __init__(self, dn, attrib): 1.15 - self.dn = dn 1.16 - self.attr = attrib 1.17 - 1.18 - def __getitem__(self, item): 1.19 - return self.attr[item] 1.20 - 1.21 - def __iter__(self): 1.22 - return iter(self.attr) 1.23 - 1.24 - def __repr__(self): 1.25 - return '<LdapRes: dn: %s>' % self.dn 1.26 - 1.27 - @classmethod 1.28 - def fromLdapQuery(cls, q): 1.29 - if not isinstance(q, dict): 1.30 - raise LdapError('LdapRes: Parsing Error, not ldap response item') 1.31 - if not (('dn' in q) and ('attributes' in q)): 1.32 - raise LdapError('LdapRes: Parsing Error, format mismatch') 1.33 - 1.34 - return cls(q['dn'], q['attributes']) 1.35 + 1.36 +class LdapRes: 1.37 + def __init__(self, dn, attrib): 1.38 + self.dn = dn 1.39 + self.attr = attrib 1.40 + 1.41 + def __getitem__(self, item): 1.42 + return self.attr[item] 1.43 + 1.44 + def __iter__(self): 1.45 + return iter(self.attr) 1.46 + 1.47 + def __repr__(self): 1.48 + return '<LdapRes: dn: %s>' % self.dn 1.49 + 1.50 + @classmethod 1.51 + def fromLdapQuery(cls, q): 1.52 + if not isinstance(q, dict): 1.53 + raise LdapError('LdapRes: Parsing Error, not ldap response item') 1.54 + if not (('dn' in q) and ('attributes' in q)): 1.55 + raise LdapError('LdapRes: Parsing Error, format mismatch') 1.56 + 1.57 + return cls(q['dn'], q['attributes']) 1.58 + 1.59 1.60 -class Ldap(): 1.61 - def __init__(self, host, user, passwd, timeout=60, queryTimeout=300, **kwa): 1.62 - if 'baseDN' in kwa: 1.63 - self._baseDN = kwa['baseDN'] 1.64 - del kwa['baseDN'] 1.65 - else: 1.66 - self._baseDN = None 1.67 - ldapSrv = Server(host, connect_timeout=timeout, **kwa) 1.68 - self._conn = self._makeConnFabric(ldapSrv, authentication=SIMPLE, 1.69 - user=user, password=passwd, 1.70 - check_names=True, lazy=True, 1.71 - auto_referrals=False, raise_exceptions=True, auto_range=True 1.72 - ) 1.73 - self.queryTimeout = queryTimeout 1.74 - 1.75 - def __call__(self, filter, attrib, queryTimeout=None, baseDN = None): 1.76 - if baseDN is None: 1.77 - if self._baseDN is None: 1.78 - raise LdapError('No base dn on query execution') 1.79 - baseDN = self._baseDN 1.80 - if queryTimeout is None: 1.81 - queryTimeout = self.queryTimeout 1.82 - try: 1.83 - conn = self._conn() 1.84 - with conn: 1.85 - conn.open() 1.86 - conn.bind() 1.87 - 1.88 - res = conn.extend.standard.paged_search(baseDN, 1.89 - filter, attributes=attrib, paged_size=LDAP_PAGE, generator=False, 1.90 - search_scope=SUBTREE, time_limit=queryTimeout 1.91 - ) 1.92 +class Ldap: 1.93 + def __init__(self, host, user, passwd, timeout=60, queryTimeout=300, **kwa): 1.94 + if 'baseDN' in kwa: 1.95 + self._baseDN = kwa['baseDN'] 1.96 + del kwa['baseDN'] 1.97 + else: 1.98 + self._baseDN = None 1.99 + ldapSrv = Server(host, connect_timeout=timeout, **kwa) 1.100 + self._conn = self._makeConnFabric(ldapSrv, authentication=SIMPLE, 1.101 + user=user, password=passwd, 1.102 + check_names=True, lazy=True, 1.103 + auto_referrals=False, raise_exceptions=True, auto_range=True 1.104 + ) 1.105 + self.queryTimeout = queryTimeout 1.106 1.107 - for i in res: 1.108 - if i['type'] == 'searchResEntry': 1.109 - yield LdapRes.fromLdapQuery(i) 1.110 - 1.111 - except Exception as e: 1.112 - raise LdapError("Error on get data (%s): %s" % (type(e), str(e)), *e.args[1:]) 1.113 - 1.114 - def getList(self, *a, **kwa): 1.115 - return [ i for i in self(*a, **kwa) ] 1.116 - 1.117 - @staticmethod 1.118 - def _makeConnFabric(*a, **kwa): 1.119 - def _func(): 1.120 - return Connection(*a, **kwa) 1.121 - 1.122 - return _func 1.123 - 1.124 - 1.125 - 1.126 - 1.127 \ No newline at end of file 1.128 + def __call__(self, filter, attrib, queryTimeout=None, baseDN=None): 1.129 + if baseDN is None: 1.130 + if self._baseDN is None: 1.131 + raise LdapError('No base dn on query execution') 1.132 + baseDN = self._baseDN 1.133 + if queryTimeout is None: 1.134 + queryTimeout = self.queryTimeout 1.135 + try: 1.136 + conn = self._conn() 1.137 + with conn: 1.138 + conn.open() 1.139 + conn.bind() 1.140 + 1.141 + res = conn.extend.standard.paged_search(baseDN, 1.142 + filter, attributes=attrib, paged_size=LDAP_PAGE, 1.143 + generator=False, 1.144 + search_scope=SUBTREE, time_limit=queryTimeout 1.145 + ) 1.146 + 1.147 + for i in res: 1.148 + if i['type'] == 'searchResEntry': 1.149 + yield LdapRes.fromLdapQuery(i) 1.150 + 1.151 + except Exception as e: 1.152 + raise LdapError("Error on get data (%s): %s" % (type(e), str(e)), *e.args[1:]) 1.153 + 1.154 + def getList(self, *a, **kwa): 1.155 + return [i for i in self(*a, **kwa)] 1.156 + 1.157 + @staticmethod 1.158 + def _makeConnFabric(*a, **kwa): 1.159 + def _func(): 1.160 + return Connection(*a, **kwa) 1.161 + 1.162 + return _func