Skip to content

Commit

Permalink
ldap.modlist: removed use of .has_key() and use set for attribute val…
Browse files Browse the repository at this point in the history
…ue set comparison
  • Loading branch information
stroeder authored and Petr Viktorin committed Nov 22, 2017
1 parent 00c2b9b commit 1e54be2
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions Lib/ldap/modlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,12 @@
import ldap,ldap.cidict


def list_dict(l,case_insensitive=0):
"""
return a dictionary with all items of l being the keys of the dictionary
If argument case_insensitive is non-zero ldap.cidict.cidict will be
used for case-insensitive string keys
"""
if case_insensitive:
d = ldap.cidict.cidict()
else:
d = {}
for i in l:
d[i]=None
return d


def addModlist(entry,ignore_attr_types=None):
"""Build modify list for call of method LDAPObject.add()"""
ignore_attr_types = list_dict(map(str.lower,(ignore_attr_types or [])))
ignore_attr_types = set(map(str.lower,ignore_attr_types or []))
modlist = []
for attrtype in entry.keys():
if ignore_attr_types.has_key(str.lower(attrtype)):
if attrtype.lower() in ignore_attr_types:
# This attribute type is ignored
continue
# Eliminate empty attr value strings in list
Expand Down Expand Up @@ -66,20 +50,20 @@ def modifyModlist(
List of attribute type names for which comparison will be made
case-insensitive
"""
ignore_attr_types = list_dict(map(str.lower,(ignore_attr_types or [])))
case_ignore_attr_types = list_dict(map(str.lower,(case_ignore_attr_types or [])))
ignore_attr_types = set(map(str.lower,ignore_attr_types or []))
case_ignore_attr_types = set(map(str.lower,case_ignore_attr_types or []))
modlist = []
attrtype_lower_map = {}
for a in old_entry.keys():
attrtype_lower_map[str.lower(a)]=a
for attrtype in new_entry.keys():
attrtype_lower = str.lower(attrtype)
if ignore_attr_types.has_key(attrtype_lower):
if attrtype_lower in ignore_attr_types:
# This attribute type is ignored
continue
# Filter away null-strings
new_value = filter(lambda x:x!=None,new_entry[attrtype])
if attrtype_lower_map.has_key(attrtype_lower):
if attrtype_lower in attrtype_lower_map:
old_value = old_entry.get(attrtype_lower_map[attrtype_lower],[])
old_value = filter(lambda x:x!=None,old_value)
del attrtype_lower_map[attrtype_lower]
Expand All @@ -92,20 +76,13 @@ def modifyModlist(
# Replace existing attribute
replace_attr_value = len(old_value)!=len(new_value)
if not replace_attr_value:
case_insensitive = case_ignore_attr_types.has_key(attrtype_lower)
old_value_dict=list_dict(old_value,case_insensitive)
new_value_dict=list_dict(new_value,case_insensitive)
delete_values = []
for v in old_value:
if not new_value_dict.has_key(v):
replace_attr_value = 1
break
add_values = []
if not replace_attr_value:
for v in new_value:
if not old_value_dict.has_key(v):
replace_attr_value = 1
break
if attrtype_lower in case_ignore_attr_types:
norm_func = str.lower
else:
norm_func = None
old_value_set=set(map(norm_func,old_value))
new_value_set=set(map(norm_func,new_value))
replace_attr_value = new_value_set != old_value_set
if replace_attr_value:
modlist.append((ldap.MOD_DELETE,attrtype,None))
modlist.append((ldap.MOD_ADD,attrtype,new_value))
Expand All @@ -116,7 +93,7 @@ def modifyModlist(
# Remove all attributes of old_entry which are not present
# in new_entry at all
for a in attrtype_lower_map.keys():
if ignore_attr_types.has_key(a):
if a in ignore_attr_types:
# This attribute type is ignored
continue
attrtype = attrtype_lower_map[a]
Expand Down

0 comments on commit 1e54be2

Please sign in to comment.