Skip to content

Commit

Permalink
ldapurl.LDAPUrlExtensions: Rewrite in terms of MutableMapping
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Petr Viktorin committed Jun 5, 2020
1 parent dada91a commit 3e11046
Showing 1 changed file with 55 additions and 47 deletions.
102 changes: 55 additions & 47 deletions Lib/ldapurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
'LDAPUrlExtension','LDAPUrlExtensions','LDAPUrl'
]

from ldap.compat import UserDict, quote, unquote
from collections import MutableMapping

from ldap.compat import quote, unquote

LDAP_SCOPE_BASE = 0
LDAP_SCOPE_ONELEVEL = 1
Expand Down Expand Up @@ -130,58 +132,64 @@ def __ne__(self,other):
return not self.__eq__(other)


class LDAPUrlExtensions(UserDict):
"""
Models a collection of LDAP URL extensions as
dictionary type
"""

def __init__(self,default=None):
UserDict.__init__(self)
for k,v in (default or {}).items():
self[k]=v

def __setitem__(self,name,value):
class LDAPUrlExtensions(MutableMapping):
"""
value
Either LDAPUrlExtension instance, (critical,exvalue)
or string'ed exvalue
Models a collection of LDAP URL extensions as
a mapping type
"""
assert isinstance(value,LDAPUrlExtension)
assert name==value.extype
self.data[name] = value

def values(self):
return [
self[k]
for k in self.keys()
]

def __str__(self):
return ','.join(str(v) for v in self.values())

def __repr__(self):
return '<%s.%s instance at %s: %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self)),
self.data
)
def __init__(self, default=None):
self._data = {}
if default is not None:
self.update(default)

def __setitem__(self, name, value):
"""
value
Either LDAPUrlExtension instance, (critical,exvalue)
or string'ed exvalue
"""
assert isinstance(value, LDAPUrlExtension)
assert name == value.extype
self._data[name] = value

def __getitem__(self, name):
return self._data[name]

def __delitem__(self, name):
del self._data[name]

def __iter__(self):
return iter(self._data)

def __len__(self):
return len(self._data)

def __str__(self):
return ','.join(str(v) for v in self.values())

def __repr__(self):
return '<%s.%s instance at %s: %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self)),
self._data
)

def __eq__(self,other):
assert isinstance(other,self.__class__),TypeError(
"other has to be instance of %s" % (self.__class__)
)
return self.data==other.data
def __eq__(self,other):
assert isinstance(other, self.__class__), TypeError(
"other has to be instance of %s" % (self.__class__)
)
return self._data == other._data

def parse(self,extListStr):
for extension_str in extListStr.strip().split(','):
if extension_str:
e = LDAPUrlExtension(extension_str)
self[e.extype] = e
def parse(self,extListStr):
for extension_str in extListStr.strip().split(','):
if extension_str:
e = LDAPUrlExtension(extension_str)
self[e.extype] = e

def unparse(self):
return ','.join([ v.unparse() for v in self.values() ])
def unparse(self):
return ','.join(v.unparse() for v in self.values())


class LDAPUrl(object):
Expand Down

0 comments on commit 3e11046

Please sign in to comment.