3
3
4
4
See https://www.python-ldap.org/ for details.
5
5
"""
6
+ from __future__ import annotations
6
7
7
8
import ldap
8
9
9
- from ldap import __version__
10
+ from ldap .pkginfo import __version__
11
+ from ldap .controls import RequestControl
12
+ from typing import Any , Dict as DictType , Iterable , List as ListType , Sequence , TextIO , Tuple
13
+ from ldap_types import *
10
14
11
15
import ldif
12
16
24
28
25
29
class WrongResultType (Exception ):
26
30
27
- def __init__ (self ,receivedResultType ,expectedResultTypes ):
31
+ def __init__ (
32
+ self ,
33
+ receivedResultType : int ,
34
+ expectedResultTypes : Iterable [int ],
35
+ ) -> None :
28
36
self .receivedResultType = receivedResultType
29
37
self .expectedResultTypes = expectedResultTypes
30
38
Exception .__init__ (self )
31
39
32
- def __str__ (self ):
40
+ def __str__ (self ) -> str :
33
41
return 'Received wrong result type {} (expected one of {}).' .format (
34
42
self .receivedResultType ,
35
- ', ' .join (self .expectedResultTypes ),
43
+ ', ' .join ([ str ( x ) for x in self .expectedResultTypes ] ),
36
44
)
37
45
38
46
@@ -46,23 +54,23 @@ class AsyncSearchHandler:
46
54
LDAPObject instance
47
55
"""
48
56
49
- def __init__ (self ,l ) :
57
+ def __init__ (self , l : ldap . ldapobject . LDAPObject ) -> None :
50
58
self ._l = l
51
- self ._msgId = None
59
+ self ._msgId : int | None = None
52
60
self ._afterFirstResult = 1
53
61
54
62
def startSearch (
55
63
self ,
56
- searchRoot ,
57
- searchScope ,
58
- filterStr ,
59
- attrList = None ,
60
- attrsOnly = 0 ,
61
- timeout = - 1 ,
62
- sizelimit = 0 ,
63
- serverctrls = None ,
64
- clientctrls = None
65
- ):
64
+ searchRoot : str ,
65
+ searchScope : int ,
66
+ filterStr : str ,
67
+ attrList : ListType [ str ] | None = None ,
68
+ attrsOnly : int = 0 ,
69
+ timeout : int = - 1 ,
70
+ sizelimit : int = 0 ,
71
+ serverctrls : ListType [ RequestControl ] | None = None ,
72
+ clientctrls : ListType [ RequestControl ] | None = None ,
73
+ ) -> None :
66
74
"""
67
75
searchRoot
68
76
See parameter base of method LDAPObject.search()
@@ -89,26 +97,30 @@ def startSearch(
89
97
attrList ,attrsOnly ,serverctrls ,clientctrls ,timeout ,sizelimit
90
98
)
91
99
self ._afterFirstResult = 1
92
- return # startSearch()
93
100
94
- def preProcessing (self ):
101
+ def preProcessing (self ) -> Any :
95
102
"""
96
103
Do anything you want after starting search but
97
104
before receiving and processing results
98
105
"""
99
106
100
- def afterFirstResult (self ):
107
+ def afterFirstResult (self ) -> Any :
101
108
"""
102
109
Do anything you want right after successfully receiving but before
103
110
processing first result
104
111
"""
105
112
106
- def postProcessing (self ):
113
+ def postProcessing (self ) -> Any :
107
114
"""
108
115
Do anything you want after receiving and processing all results
109
116
"""
110
117
111
- def processResults (self ,ignoreResultsNumber = 0 ,processResultsCount = 0 ,timeout = - 1 ):
118
+ def processResults (
119
+ self ,
120
+ ignoreResultsNumber : int = 0 ,
121
+ processResultsCount : int = 0 ,
122
+ timeout : int = - 1 ,
123
+ ) -> int :
112
124
"""
113
125
ignoreResultsNumber
114
126
Don't process the first ignoreResultsNumber results.
@@ -118,6 +130,9 @@ def processResults(self,ignoreResultsNumber=0,processResultsCount=0,timeout=-1):
118
130
timeout
119
131
See parameter timeout of ldap.LDAPObject.result()
120
132
"""
133
+ if self ._msgId is None :
134
+ raise RuntimeError ('processResults() called without calling startSearch() first' )
135
+
121
136
self .preProcessing ()
122
137
result_counter = 0
123
138
end_result_counter = ignoreResultsNumber + processResultsCount
@@ -156,7 +171,11 @@ def processResults(self,ignoreResultsNumber=0,processResultsCount=0,timeout=-1):
156
171
self .postProcessing ()
157
172
return partial # processResults()
158
173
159
- def _processSingleResult (self ,resultType ,resultItem ):
174
+ def _processSingleResult (
175
+ self ,
176
+ resultType : int ,
177
+ resultItem : LDAPSearchResult ,
178
+ ) -> Any :
160
179
"""
161
180
Process single entry
162
181
@@ -177,11 +196,15 @@ class List(AsyncSearchHandler):
177
196
results.
178
197
"""
179
198
180
- def __init__ (self ,l ) :
199
+ def __init__ (self , l : ldap . ldapobject . LDAPObject ) -> None :
181
200
AsyncSearchHandler .__init__ (self ,l )
182
- self .allResults = []
201
+ self .allResults : ListType [ Tuple [ int , LDAPSearchResult ]] = []
183
202
184
- def _processSingleResult (self ,resultType ,resultItem ):
203
+ def _processSingleResult (
204
+ self ,
205
+ resultType : int ,
206
+ resultItem : LDAPSearchResult ,
207
+ ) -> None :
185
208
self .allResults .append ((resultType ,resultItem ))
186
209
187
210
@@ -190,11 +213,15 @@ class Dict(AsyncSearchHandler):
190
213
Class for collecting all search results into a dictionary {dn:entry}
191
214
"""
192
215
193
- def __init__ (self ,l ) :
216
+ def __init__ (self , l : ldap . ldapobject . LDAPObject ) -> None :
194
217
AsyncSearchHandler .__init__ (self ,l )
195
- self .allEntries = {}
218
+ self .allEntries : DictType [ str , LDAPEntryDict ] = {}
196
219
197
- def _processSingleResult (self ,resultType ,resultItem ):
220
+ def _processSingleResult (
221
+ self ,
222
+ resultType : int ,
223
+ resultItem : LDAPSearchResult ,
224
+ ) -> None :
198
225
if resultType in ENTRY_RESULT_TYPES :
199
226
# Search continuations are ignored
200
227
dn ,entry = resultItem
@@ -207,12 +234,20 @@ class IndexedDict(Dict):
207
234
and maintain case-sensitive equality indexes to entries
208
235
"""
209
236
210
- def __init__ (self ,l ,indexed_attrs = None ):
237
+ def __init__ (
238
+ self ,
239
+ l : ldap .ldapobject .LDAPObject ,
240
+ indexed_attrs : Sequence [str ] | None = None ,
241
+ ) -> None :
211
242
Dict .__init__ (self ,l )
212
243
self .indexed_attrs = indexed_attrs or ()
213
- self .index = {}.fromkeys (self .indexed_attrs ,{})
244
+ self .index : DictType [ str , DictType [ bytes , ListType [ str ]]] = {}.fromkeys (self .indexed_attrs ,{})
214
245
215
- def _processSingleResult (self ,resultType ,resultItem ):
246
+ def _processSingleResult (
247
+ self ,
248
+ resultType : int ,
249
+ resultItem : LDAPSearchResult ,
250
+ ) -> None :
216
251
if resultType in ENTRY_RESULT_TYPES :
217
252
# Search continuations are ignored
218
253
dn ,entry = resultItem
@@ -237,20 +272,26 @@ class FileWriter(AsyncSearchHandler):
237
272
File object instance where the LDIF data is written to
238
273
"""
239
274
240
- def __init__ (self ,l ,f ,headerStr = '' ,footerStr = '' ):
275
+ def __init__ (
276
+ self ,
277
+ l : ldap .ldapobject .LDAPObject ,
278
+ f : TextIO ,
279
+ headerStr : str = '' ,
280
+ footerStr : str = '' ,
281
+ ) -> None :
241
282
AsyncSearchHandler .__init__ (self ,l )
242
283
self ._f = f
243
284
self .headerStr = headerStr
244
285
self .footerStr = footerStr
245
286
246
- def preProcessing (self ):
287
+ def preProcessing (self ) -> None :
247
288
"""
248
289
The headerStr is written to output after starting search but
249
290
before receiving and processing results.
250
291
"""
251
292
self ._f .write (self .headerStr )
252
293
253
- def postProcessing (self ):
294
+ def postProcessing (self ) -> None :
254
295
"""
255
296
The footerStr is written to output after receiving and
256
297
processing results.
@@ -270,14 +311,24 @@ class LDIFWriter(FileWriter):
270
311
Either a file-like object or a ldif.LDIFWriter instance used for output
271
312
"""
272
313
273
- def __init__ (self ,l ,writer_obj ,headerStr = '' ,footerStr = '' ):
314
+ def __init__ (
315
+ self ,
316
+ l : ldap .ldapobject .LDAPObject ,
317
+ writer_obj : TextIO | ldif .LDIFWriter ,
318
+ headerStr : str = '' ,
319
+ footerStr : str = '' ,
320
+ ) -> None :
274
321
if isinstance (writer_obj ,ldif .LDIFWriter ):
275
322
self ._ldif_writer = writer_obj
276
323
else :
277
324
self ._ldif_writer = ldif .LDIFWriter (writer_obj )
278
325
FileWriter .__init__ (self ,l ,self ._ldif_writer ._output_file ,headerStr ,footerStr )
279
326
280
- def _processSingleResult (self ,resultType ,resultItem ):
327
+ def _processSingleResult (
328
+ self ,
329
+ resultType : int ,
330
+ resultItem : LDAPSearchResult ,
331
+ ) -> None :
281
332
if resultType in ENTRY_RESULT_TYPES :
282
333
# Search continuations are ignored
283
334
dn ,entry = resultItem
0 commit comments