1
2
3
4
5
6 escapedChars = r',+"\<>;='
7 escapedChars_leading = r' #'
8 escapedChars_trailing = r' #'
9
11 r=''
12 r_trailer=''
13
14 if s and s[0] in escapedChars_leading:
15 r='\\'+s[0]
16 s=s[1:]
17
18 if s and s[-1] in escapedChars_trailing:
19 r_trailer='\\'+s[-1]
20 s=s[:-1]
21
22 for c in s:
23 if c in escapedChars:
24 r=r+'\\'+c
25 elif ord(c)<=31:
26 r=r+'\\%02X' % ord(c)
27 else:
28 r=r+c
29
30 return r+r_trailer
31
33 r=''
34 while s:
35 if s[0]=='\\':
36 if s[1] in '0123456789abcdef':
37 r=r+chr(int(s[1:3], 16))
38 s=s[3:]
39 else:
40 r=r+s[1]
41 s=s[2:]
42 else:
43 r=r+s[0]
44 s=s[1:]
45 return r
46
48 if not s:
49 return []
50
51 r=['']
52 while s:
53 if s[0]=='\\':
54 r[-1]=r[-1]+s[:2]
55 s=s[2:]
56 else:
57 if s[0] in separator:
58 r.append('')
59 s=s[1:]
60 while s[0]==' ':
61 s=s[1:]
62 else:
63 r[-1]=r[-1]+s[0]
64 s=s[1:]
65 return r
66
68 """Invalid relative distinguished name."""
69
71 Exception.__init__(self)
72 self.rdn = rdn
73
75 return "Invalid relative distinguished name %s." \
76 % repr(self.rdn)
77
79
80 attributeType = None
81 value = None
82
83 - def __init__(self, stringValue=None, attributeType=None, value=None):
95
98
100 return (self.__class__.__name__
101 + '(attributeType='
102 + repr(self.attributeType)
103 + ', value='
104 + repr(self.value)
105 + ')')
106
109
115
117 return not (self == other)
118
126
130
132 return not self > other
133
135 return not self < other
136
138 """LDAP Relative Distinguished Name."""
139
140 attributeTypesAndValues = None
141
142 - def __init__(self, magic=None, stringValue=None, attributeTypesAndValues=None):
162
165
168
174
177
182
184 return not (self == other)
185
187 if not isinstance(other, self.__class__):
188 return False
189 return self.split() < other.split()
190
194
196 return not self > other
197
199 return not self < other
200
203
204
206 """LDAP Distinguished Name."""
207 listOfRDNs = None
208 - def __init__(self, magic=None, stringValue=None, listOfRDNs=None):
231
234
237
240
242 return (self.__class__.__name__
243 + '(listOfRDNs='
244 + repr(self.listOfRDNs)
245 + ')')
246
248 return hash(str(self))
249
256
258 return not (self == other)
259
266
267 - def getDomainName(self):
268 domainParts = []
269 l=list(self.listOfRDNs)
270 l.reverse()
271 for rdn in l:
272 if rdn.count() != 1:
273 break
274 attributeTypeAndValue = rdn.split()[0]
275 if attributeTypeAndValue.attributeType.upper() != 'DC':
276 break
277 domainParts.insert(0, attributeTypeAndValue.value)
278 if domainParts:
279 return '.'.join(domainParts)
280 else:
281 return None
282
284 """Does the tree rooted at DN contain or equal the other DN."""
285 if self == other:
286 return 1
287 if not isinstance(other, DistinguishedName):
288 other=DistinguishedName(other)
289 its=list(other.split())
290 mine=list(self.split())
291
292 while mine and its:
293 m=mine.pop()
294 i=its.pop()
295 if m!=i:
296 return 0
297 if mine:
298 return 0
299 return 1
300