|
6 | 6 |
|
7 | 7 | # This is also the overall release version number
|
8 | 8 |
|
9 |
| -from pkginfo import __version__, __author__, __license__ |
10 |
| - |
11 | 9 | import sys
|
12 | 10 |
|
13 |
| -if __debug__: |
14 |
| - # Tracing is only supported in debugging mode |
15 |
| - import traceback |
16 |
| - _trace_level = 0 |
17 |
| - _trace_file = sys.stderr |
18 |
| - _trace_stack_limit = None |
| 11 | +from ldap.pkginfo import __version__, __author__, __license__ |
19 | 12 |
|
20 |
| -from ldap.pkginfo import __version__ |
| 13 | +if __debug__: |
| 14 | + # Tracing is only supported in debugging mode |
| 15 | + import traceback |
| 16 | + _trace_level = 0 |
| 17 | + _trace_file = sys.stderr |
| 18 | + _trace_stack_limit = None |
21 | 19 |
|
22 | 20 | import _ldap
|
23 |
| -assert _ldap.__version__==__version__, \ |
24 |
| - ImportError('ldap %s and _ldap %s version mismatch!' % (__version__,_ldap.__version__)) |
| 21 | +assert _ldap.__version__ == __version__, ImportError( |
| 22 | + 'ldap %s and _ldap %s version mismatch!' % (__version__, _ldap.__version__) |
| 23 | +) |
25 | 24 | from _ldap import *
|
26 | 25 | # call into libldap to initialize it right now
|
| 26 | +from functions import open, initialize, init, get_option, set_option |
| 27 | +from functions import escape_str, strf_secs, strp_secs |
| 28 | +from ldapobject import NO_UNIQUE_ENTRY |
| 29 | +from ldap.dn import explode_dn, explode_rdn |
| 30 | + |
| 31 | + |
27 | 32 | LIBLDAP_API_INFO = _ldap.get_option(_ldap.OPT_API_INFO)
|
28 | 33 |
|
29 | 34 | OPT_NAMES_DICT = {}
|
30 |
| -for k,v in vars(_ldap).items(): |
31 |
| - if k.startswith('OPT_'): |
32 |
| - OPT_NAMES_DICT[v]=k |
| 35 | +for key, val in vars(_ldap).items(): |
| 36 | + if key.startswith('OPT_'): |
| 37 | + OPT_NAMES_DICT[val] = key |
| 38 | + |
33 | 39 |
|
34 | 40 | class DummyLock:
|
35 |
| - """Define dummy class with methods compatible to threading.Lock""" |
36 |
| - def __init__(self): |
37 |
| - pass |
38 |
| - def acquire(self): |
39 |
| - pass |
40 |
| - def release(self): |
41 |
| - pass |
| 41 | + """ |
| 42 | + Define dummy class with methods compatible to threading.Lock |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self): |
| 46 | + pass |
| 47 | + |
| 48 | + def acquire(self): |
| 49 | + """ |
| 50 | + dummy |
| 51 | + """ |
| 52 | + pass |
| 53 | + |
| 54 | + def release(self): |
| 55 | + """ |
| 56 | + dummy |
| 57 | + """ |
| 58 | + pass |
| 59 | + |
42 | 60 |
|
43 | 61 | try:
|
44 |
| - # Check if Python installation was build with thread support |
45 |
| - import thread |
| 62 | + # Check if Python installation was build with thread support |
| 63 | + import thread |
46 | 64 | except ImportError:
|
47 |
| - LDAPLockBaseClass = DummyLock |
| 65 | + LDAPLockBaseClass = DummyLock |
48 | 66 | else:
|
49 |
| - import threading |
50 |
| - LDAPLockBaseClass = threading.Lock |
| 67 | + import threading |
| 68 | + LDAPLockBaseClass = threading.Lock |
51 | 69 |
|
52 | 70 |
|
53 |
| -class LDAPLock: |
54 |
| - """ |
55 |
| - Mainly a wrapper class to log all locking events. |
56 |
| - Note that this cumbersome approach with _lock attribute was taken |
57 |
| - since threading.Lock is not suitable for sub-classing. |
58 |
| - """ |
59 |
| - _min_trace_level = 3 |
60 |
| - |
61 |
| - def __init__(self,lock_class=None,desc=''): |
| 71 | +class LDAPLock(object): |
62 | 72 | """
|
63 |
| - lock_class |
64 |
| - Class compatible to threading.Lock |
65 |
| - desc |
66 |
| - Description shown in debug log messages |
| 73 | + Mainly a wrapper class to log all locking events. |
| 74 | + Note that this cumbersome approach with _lock attribute was taken |
| 75 | + since threading.Lock is not suitable for sub-classing. |
67 | 76 | """
|
68 |
| - self._desc = desc |
69 |
| - self._lock = (lock_class or LDAPLockBaseClass)() |
70 |
| - |
71 |
| - def acquire(self): |
72 |
| - if __debug__: |
73 |
| - global _trace_level |
74 |
| - if _trace_level>=self._min_trace_level: |
75 |
| - _trace_file.write('***%s.acquire() %s %s\n' % (self.__class__.__name__,repr(self),self._desc)) |
76 |
| - return self._lock.acquire() |
77 |
| - |
78 |
| - def release(self): |
79 |
| - if __debug__: |
80 |
| - global _trace_level |
81 |
| - if _trace_level>=self._min_trace_level: |
82 |
| - _trace_file.write('***%s.release() %s %s\n' % (self.__class__.__name__,repr(self),self._desc)) |
83 |
| - return self._lock.release() |
| 77 | + _min_trace_level = 3 |
| 78 | + |
| 79 | + def __init__(self, lock_class=None, desc='', trace_level=None): |
| 80 | + """ |
| 81 | + lock_class |
| 82 | + Class compatible to threading.Lock |
| 83 | + desc |
| 84 | + Description shown in debug log messages |
| 85 | + """ |
| 86 | + self._desc = desc |
| 87 | + self._lock = (lock_class or LDAPLockBaseClass)() |
| 88 | + if trace_level is not None: |
| 89 | + self._min_trace_level = trace_level |
| 90 | + |
| 91 | + def acquire(self): |
| 92 | + """ |
| 93 | + acquire lock and log |
| 94 | + """ |
| 95 | + if __debug__: |
| 96 | + global _trace_level |
| 97 | + if _trace_level >= self._min_trace_level: |
| 98 | + _trace_file.write('***%s.acquire() %r %s\n' % ( |
| 99 | + self.__class__.__name__, self, self._desc |
| 100 | + )) |
| 101 | + return self._lock.acquire() |
| 102 | + |
| 103 | + def release(self): |
| 104 | + """ |
| 105 | + release lock and log |
| 106 | + """ |
| 107 | + if __debug__: |
| 108 | + global _trace_level |
| 109 | + if _trace_level >= self._min_trace_level: |
| 110 | + _trace_file.write('***%s.release() %r %s\n' % ( |
| 111 | + self.__class__.__name__, self, self._desc |
| 112 | + )) |
| 113 | + return self._lock.release() |
84 | 114 |
|
85 | 115 |
|
86 | 116 | # Create module-wide lock for serializing all calls into underlying LDAP lib
|
87 | 117 | _ldap_module_lock = LDAPLock(desc='Module wide')
|
88 | 118 |
|
89 |
| -from functions import open,initialize,init,get_option,set_option,escape_str,strf_secs,strp_secs |
90 |
| - |
91 |
| -from ldapobject import NO_UNIQUE_ENTRY |
92 |
| - |
93 |
| -from ldap.dn import explode_dn,explode_rdn,str2dn,dn2str |
94 |
| -del str2dn |
95 |
| -del dn2str |
96 |
| - |
97 | 119 | # More constants
|
98 | 120 |
|
99 | 121 | # For compability of 2.3 and 2.4 OpenLDAP API
|
100 |
| -OPT_DIAGNOSTIC_MESSAGE = OPT_ERROR_STRING |
| 122 | +OPT_DIAGNOSTIC_MESSAGE = _ldap.OPT_ERROR_STRING |
0 commit comments