|
4 | 4 | See https://www.python-ldap.org/ for details.
|
5 | 5 | """
|
6 | 6 |
|
7 |
| -from ldap import __version__ |
8 |
| - |
9 | 7 | __all__ = [
|
10 |
| - 'open','initialize','init', |
11 |
| - 'explode_dn','explode_rdn', |
12 |
| - 'get_option','set_option', |
13 |
| - 'escape_str', |
14 |
| - 'strf_secs','strp_secs', |
| 8 | + 'escape_str', |
| 9 | + 'explode_dn', |
| 10 | + 'explode_rdn', |
| 11 | + 'get_option', |
| 12 | + 'init', |
| 13 | + 'initialize', |
| 14 | + 'open', |
| 15 | + 'set_option', |
| 16 | + 'strf_secs', |
| 17 | + 'strp_secs', |
15 | 18 | ]
|
16 | 19 |
|
17 |
| -import sys,pprint,time,_ldap,ldap |
| 20 | +import sys |
| 21 | +import pprint |
| 22 | +import time |
18 | 23 | from calendar import timegm
|
19 | 24 |
|
20 |
| -from ldap import LDAPError |
| 25 | +from ldap.pkginfo import __version__, __author__, __license__ |
21 | 26 |
|
22 |
| -from ldap.dn import explode_dn,explode_rdn |
| 27 | +import _ldap |
| 28 | +assert _ldap.__version__ == __version__, ImportError( |
| 29 | + 'ldap %s and _ldap %s version mismatch!' % (__version__, _ldap.__version__) |
| 30 | +) |
23 | 31 |
|
| 32 | +import ldap |
| 33 | +from ldap import LDAPError |
| 34 | +from ldap.dn import explode_dn, explode_rdn |
24 | 35 | from ldap.ldapobject import LDAPObject
|
25 | 36 |
|
26 | 37 | if __debug__:
|
27 |
| - # Tracing is only supported in debugging mode |
28 |
| - import traceback |
29 |
| - |
30 |
| - |
31 |
| -def _ldap_function_call(lock,func,*args,**kwargs): |
32 |
| - """ |
33 |
| - Wrapper function which locks and logs calls to function |
34 |
| -
|
35 |
| - lock |
36 |
| - Instance of threading.Lock or compatible |
37 |
| - func |
38 |
| - Function to call with arguments passed in via *args and **kwargs |
39 |
| - """ |
40 |
| - if lock: |
41 |
| - lock.acquire() |
42 |
| - if __debug__: |
43 |
| - if ldap._trace_level>=1: |
44 |
| - ldap._trace_file.write('*** %s.%s %s\n' % ( |
45 |
| - '_ldap',func.__name__, |
46 |
| - pprint.pformat((args,kwargs)) |
47 |
| - )) |
48 |
| - if ldap._trace_level>=9: |
49 |
| - traceback.print_stack(limit=ldap._trace_stack_limit,file=ldap._trace_file) |
50 |
| - try: |
51 |
| - try: |
52 |
| - result = func(*args,**kwargs) |
| 38 | + # Tracing is only supported in debugging mode |
| 39 | + import traceback |
| 40 | + |
| 41 | + |
| 42 | +def _ldap_function_call(lock, func, *args, **kwargs): |
| 43 | + """ |
| 44 | + Wrapper function which locks and logs calls to function |
| 45 | +
|
| 46 | + lock |
| 47 | + Instance of threading.Lock or compatible |
| 48 | + func |
| 49 | + Function to call with arguments passed in via *args and **kwargs |
| 50 | + """ |
| 51 | + if __debug__ and ldap._trace_level >= 1: |
| 52 | + ldap._trace_file.write('*** %s.%s %s\n' % ( |
| 53 | + '_ldap', |
| 54 | + func.__name__, |
| 55 | + pprint.pformat((args, kwargs)) |
| 56 | + )) |
| 57 | + if ldap._trace_level >= 9: |
| 58 | + traceback.print_stack( |
| 59 | + limit=ldap._trace_stack_limit, |
| 60 | + file=ldap._trace_file, |
| 61 | + ) |
| 62 | + if lock: |
| 63 | + lock.acquire() |
| 64 | + try: # finally |
| 65 | + try: # error / result logging |
| 66 | + result = func(*args, **kwargs) |
| 67 | + except LDAPError, err: |
| 68 | + if __debug__ and ldap._trace_level >= 2: |
| 69 | + ldap._trace_file.write('=> LDAPError: %s\n' % (err)) |
| 70 | + raise |
| 71 | + else: |
| 72 | + if __debug__ and ldap._trace_level >= 2: |
| 73 | + ldap._trace_file.write('=> result:\n%s\n' % (pprint.pformat(result))) |
53 | 74 | finally:
|
54 |
| - if lock: |
55 |
| - lock.release() |
56 |
| - except LDAPError,e: |
57 |
| - if __debug__ and ldap._trace_level>=2: |
58 |
| - ldap._trace_file.write('=> LDAPError: %s\n' % (str(e))) |
59 |
| - raise |
60 |
| - if __debug__ and ldap._trace_level>=2: |
61 |
| - ldap._trace_file.write('=> result:\n%s\n' % (pprint.pformat(result))) |
62 |
| - return result |
63 |
| - |
64 |
| - |
65 |
| -def initialize(uri,trace_level=0,trace_file=sys.stdout,trace_stack_limit=None): |
66 |
| - """ |
67 |
| - Return LDAPObject instance by opening LDAP connection to |
68 |
| - LDAP host specified by LDAP URL |
69 |
| -
|
70 |
| - Parameters: |
71 |
| - uri |
72 |
| - LDAP URL containing at least connection scheme and hostport, |
73 |
| - e.g. ldap://localhost:389 |
74 |
| - trace_level |
75 |
| - If non-zero a trace output of LDAP calls is generated. |
76 |
| - trace_file |
77 |
| - File object where to write the trace output to. |
78 |
| - Default is to use stdout. |
79 |
| - """ |
80 |
| - return LDAPObject(uri,trace_level,trace_file,trace_stack_limit) |
81 |
| - |
82 |
| - |
83 |
| -def open(host,port=389,trace_level=0,trace_file=sys.stdout,trace_stack_limit=None): |
84 |
| - """ |
85 |
| - Return LDAPObject instance by opening LDAP connection to |
86 |
| - specified LDAP host |
87 |
| -
|
88 |
| - Parameters: |
89 |
| - host |
90 |
| - LDAP host and port, e.g. localhost |
91 |
| - port |
92 |
| - integer specifying the port number to use, e.g. 389 |
93 |
| - trace_level |
94 |
| - If non-zero a trace output of LDAP calls is generated. |
95 |
| - trace_file |
96 |
| - File object where to write the trace output to. |
97 |
| - Default is to use stdout. |
98 |
| - """ |
99 |
| - import warnings |
100 |
| - warnings.warn('ldap.open() is deprecated! Use ldap.initialize() instead.', DeprecationWarning,2) |
101 |
| - return initialize('ldap://%s:%d' % (host,port),trace_level,trace_file,trace_stack_limit) |
| 75 | + if lock: |
| 76 | + lock.release() |
| 77 | + return result # end of _ldap_function_call() |
| 78 | + |
| 79 | + |
| 80 | +def initialize( |
| 81 | + uri, |
| 82 | + trace_level=0, |
| 83 | + trace_file=sys.stdout, |
| 84 | + trace_stack_limit=None, |
| 85 | + ): |
| 86 | + """ |
| 87 | + Return LDAPObject instance by opening LDAP connection to |
| 88 | + LDAP host specified by LDAP URL |
| 89 | +
|
| 90 | + Parameters: |
| 91 | + uri |
| 92 | + LDAP URL containing at least connection scheme and hostport, |
| 93 | + e.g. ldap://localhost:389 |
| 94 | + trace_level |
| 95 | + If non-zero a trace output of LDAP calls is generated. |
| 96 | + trace_file |
| 97 | + File object where to write the trace output to. |
| 98 | + Default is to use stdout. |
| 99 | + """ |
| 100 | + return LDAPObject(uri, trace_level, trace_file, trace_stack_limit) |
| 101 | + |
| 102 | + |
| 103 | +def open( |
| 104 | + host, |
| 105 | + port=389, |
| 106 | + trace_level=0, |
| 107 | + trace_file=sys.stdout, |
| 108 | + trace_stack_limit=None, |
| 109 | + ): |
| 110 | + """ |
| 111 | + Return LDAPObject instance by opening LDAP connection to |
| 112 | + specified LDAP host |
| 113 | +
|
| 114 | + Parameters: |
| 115 | + host |
| 116 | + LDAP host and port, e.g. localhost |
| 117 | + port |
| 118 | + integer specifying the port number to use, e.g. 389 |
| 119 | + trace_level |
| 120 | + If non-zero a trace output of LDAP calls is generated. |
| 121 | + trace_file |
| 122 | + File object where to write the trace output to. |
| 123 | + Default is to use stdout. |
| 124 | + """ |
| 125 | + import warnings |
| 126 | + warnings.warn( |
| 127 | + 'ldap.open() is deprecated! Use ldap.initialize() instead.', DeprecationWarning, 2 |
| 128 | + ) |
| 129 | + return initialize('ldap://%s:%d' % (host, port), trace_level, trace_file, trace_stack_limit) |
102 | 130 |
|
103 | 131 | init = open
|
104 | 132 |
|
105 | 133 |
|
106 | 134 | def get_option(option):
|
107 |
| - """ |
108 |
| - get_option(name) -> value |
| 135 | + """ |
| 136 | + get_option(name) -> value |
109 | 137 |
|
110 |
| - Get the value of an LDAP global option. |
111 |
| - """ |
112 |
| - return _ldap_function_call(None,_ldap.get_option,option) |
| 138 | + Get the value of an LDAP global option. |
| 139 | + """ |
| 140 | + return _ldap_function_call(None, _ldap.get_option, option) |
113 | 141 |
|
114 | 142 |
|
115 |
| -def set_option(option,invalue): |
116 |
| - """ |
117 |
| - set_option(name, value) |
| 143 | +def set_option(option, invalue): |
| 144 | + """ |
| 145 | + set_option(name, value) |
118 | 146 |
|
119 |
| - Set the value of an LDAP global option. |
120 |
| - """ |
121 |
| - return _ldap_function_call(None,_ldap.set_option,option,invalue) |
| 147 | + Set the value of an LDAP global option. |
| 148 | + """ |
| 149 | + return _ldap_function_call(None, _ldap.set_option, option, invalue) |
122 | 150 |
|
123 | 151 |
|
124 |
| -def escape_str(escape_func,s,*args): |
125 |
| - """ |
126 |
| - Applies escape_func() to all items of `args' and returns a string based |
127 |
| - on format string `s'. |
128 |
| - """ |
129 |
| - escape_args = map(escape_func,args) |
130 |
| - return s % tuple(escape_args) |
| 152 | +def escape_str(escape_func, val, *args): |
| 153 | + """ |
| 154 | + Applies escape_func() to all items of `args' and returns a string based |
| 155 | + on format string `val'. |
| 156 | + """ |
| 157 | + escape_args = map(escape_func, args) |
| 158 | + return val % tuple(escape_args) |
131 | 159 |
|
132 | 160 |
|
133 | 161 | def strf_secs(secs):
|
|
0 commit comments