1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
"""
This is a convenience wrapper for dictionaries
returned from LDAP servers containing attribute
names of variable case.
See https://www.python-ldap.org/ for details.
"""
import warnings
from collections.abc import MutableMapping
from ldap import __version__
class cidict(MutableMapping):
"""
Case-insensitive but case-respecting dictionary.
"""
__slots__ = ('_keys', '_data')
def __init__(self, default=None):
self._keys = {}
self._data = {}
if default:
self.update(default)
# MutableMapping abstract methods
def __getitem__(self, key):
return self._data[key.lower()]
def __setitem__(self, key, value):
lower_key = key.lower()
self._keys[lower_key] = key
self._data[lower_key] = value
def __delitem__(self, key):
lower_key = key.lower()
del self._keys[lower_key]
del self._data[lower_key]
def __iter__(self):
return iter(self._keys.values())
def __len__(self):
return len(self._keys)
# Specializations for performance
def __contains__(self, key):
return key.lower() in self._keys
def clear(self):
self._keys.clear()
self._data.clear()
def copy(self):
inst = self.__class__.__new__(self.__class__)
inst._data = self._data.copy()
inst._keys = self._keys.copy()
return inst
__copy__ = copy
# Backwards compatibility
def has_key(self, key):
"""Compatibility with python-ldap 2.x"""
return key in self
@property
def data(self):
"""Compatibility with older IterableUserDict-based implementation"""
warnings.warn(
'ldap.cidict.cidict.data is an internal attribute; it may be ' +
'removed at any time',
category=DeprecationWarning,
stacklevel=2,
)
return self._data
def strlist_minus(a,b):
"""
Return list of all items in a which are not in b (a - b).
a,b are supposed to be lists of case-insensitive strings.
"""
warnings.warn(
"strlist functions are deprecated and will be removed in 3.5",
category=DeprecationWarning,
stacklevel=2,
)
temp = cidict()
for elt in b:
temp[elt] = elt
result = [
elt
for elt in a
if elt not in temp
]
return result
def strlist_intersection(a,b):
"""
Return intersection of two lists of case-insensitive strings a,b.
"""
warnings.warn(
"strlist functions are deprecated and will be removed in 3.5",
category=DeprecationWarning,
stacklevel=2,
)
temp = cidict()
for elt in a:
temp[elt] = elt
result = [
temp[elt]
for elt in b
if elt in temp
]
return result
def strlist_union(a,b):
"""
Return union of two lists of case-insensitive strings a,b.
"""
warnings.warn(
"strlist functions are deprecated and will be removed in 3.5",
category=DeprecationWarning,
stacklevel=2,
)
temp = cidict()
for elt in a:
temp[elt] = elt
for elt in b:
temp[elt] = elt
return temp.values()
|