Skip to content

Commit 3e11046

Browse files
committed
ldapurl.LDAPUrlExtensions: Rewrite in terms of MutableMapping
The UserDict API is not specified when it comes to extending: it is not clear which methods can be safely overridden. The MutableMapping ABC fixes this problem by providing an explicit set of abstract methods, in terms of which the rest of the API is implemented. Switch to using MutableMapping for LDAPUrlExtensions.
1 parent dada91a commit 3e11046

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

Lib/ldapurl.py

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
'LDAPUrlExtension','LDAPUrlExtensions','LDAPUrl'
1717
]
1818

19-
from ldap.compat import UserDict, quote, unquote
19+
from collections import MutableMapping
20+
21+
from ldap.compat import quote, unquote
2022

2123
LDAP_SCOPE_BASE = 0
2224
LDAP_SCOPE_ONELEVEL = 1
@@ -130,58 +132,64 @@ def __ne__(self,other):
130132
return not self.__eq__(other)
131133

132134

133-
class LDAPUrlExtensions(UserDict):
134-
"""
135-
Models a collection of LDAP URL extensions as
136-
dictionary type
137-
"""
138-
139-
def __init__(self,default=None):
140-
UserDict.__init__(self)
141-
for k,v in (default or {}).items():
142-
self[k]=v
143-
144-
def __setitem__(self,name,value):
135+
class LDAPUrlExtensions(MutableMapping):
145136
"""
146-
value
147-
Either LDAPUrlExtension instance, (critical,exvalue)
148-
or string'ed exvalue
137+
Models a collection of LDAP URL extensions as
138+
a mapping type
149139
"""
150-
assert isinstance(value,LDAPUrlExtension)
151-
assert name==value.extype
152-
self.data[name] = value
153140

154-
def values(self):
155-
return [
156-
self[k]
157-
for k in self.keys()
158-
]
159-
160-
def __str__(self):
161-
return ','.join(str(v) for v in self.values())
162-
163-
def __repr__(self):
164-
return '<%s.%s instance at %s: %s>' % (
165-
self.__class__.__module__,
166-
self.__class__.__name__,
167-
hex(id(self)),
168-
self.data
169-
)
141+
def __init__(self, default=None):
142+
self._data = {}
143+
if default is not None:
144+
self.update(default)
145+
146+
def __setitem__(self, name, value):
147+
"""
148+
value
149+
Either LDAPUrlExtension instance, (critical,exvalue)
150+
or string'ed exvalue
151+
"""
152+
assert isinstance(value, LDAPUrlExtension)
153+
assert name == value.extype
154+
self._data[name] = value
155+
156+
def __getitem__(self, name):
157+
return self._data[name]
158+
159+
def __delitem__(self, name):
160+
del self._data[name]
161+
162+
def __iter__(self):
163+
return iter(self._data)
164+
165+
def __len__(self):
166+
return len(self._data)
167+
168+
def __str__(self):
169+
return ','.join(str(v) for v in self.values())
170+
171+
def __repr__(self):
172+
return '<%s.%s instance at %s: %s>' % (
173+
self.__class__.__module__,
174+
self.__class__.__name__,
175+
hex(id(self)),
176+
self._data
177+
)
170178

171-
def __eq__(self,other):
172-
assert isinstance(other,self.__class__),TypeError(
173-
"other has to be instance of %s" % (self.__class__)
174-
)
175-
return self.data==other.data
179+
def __eq__(self,other):
180+
assert isinstance(other, self.__class__), TypeError(
181+
"other has to be instance of %s" % (self.__class__)
182+
)
183+
return self._data == other._data
176184

177-
def parse(self,extListStr):
178-
for extension_str in extListStr.strip().split(','):
179-
if extension_str:
180-
e = LDAPUrlExtension(extension_str)
181-
self[e.extype] = e
185+
def parse(self,extListStr):
186+
for extension_str in extListStr.strip().split(','):
187+
if extension_str:
188+
e = LDAPUrlExtension(extension_str)
189+
self[e.extype] = e
182190

183-
def unparse(self):
184-
return ','.join([ v.unparse() for v in self.values() ])
191+
def unparse(self):
192+
return ','.join(v.unparse() for v in self.values())
185193

186194

187195
class LDAPUrl(object):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy