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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
"""
ldap.modlist - create add/modify modlist's
(c) by Michael Stroeder <michael@stroeder.com>
$Id: modlist.py,v 1.4 2001/12/19 11:42:30 stroeder Exp $
Python compability note:
This module is known to work with Python 2.0+ but should work
with Python 1.5.2 as well.
"""
__version__ = '0.0.4'
import string,ldap
def addModlist(entry,ignore_attr_types=[]):
"""Build modify list for call of method LDAPObject.add()"""
ignore_attr_types = map(string.lower,ignore_attr_types)
modlist = []
for attrtype in entry.keys():
if string.lower(attrtype) in ignore_attr_types:
# This attribute type is ignored
continue
# Eliminate empty attr value strings in list
attrvaluelist = filter(None,entry[attrtype])
if attrvaluelist:
modlist.append((attrtype,entry[attrtype]))
return modlist
def modifyModlist(
old_entry,new_entry,ignore_attr_types=[],ignore_oldexistent=0
):
"""
Build differential modify list for calling LDAPObject.modify()/modify_s()
old_entry
Dictionary holding the old entry
new_entry
Dictionary holding what the new entry should be
ignore_attr_types
List of attribute type names to be ignored completely
ignore_oldexistent
If non-zero attribute type names which are in old_entry
but are not found in new_entry at all are not deleted.
This is handy for situations where your application
sets attribute value to '' for deleting an attribute.
In most cases leave zero.
"""
ignore_attr_types = map(string.lower,ignore_attr_types)
modlist = []
attrtype_lower_map = {}
for a in old_entry.keys():
attrtype_lower_map[string.lower(a)]=a
for attrtype in new_entry.keys():
attrtype_lower = string.lower(attrtype)
if attrtype_lower in ignore_attr_types:
# This attribute type is ignored
continue
new_value = filter(None,new_entry[attrtype])
if attrtype_lower_map.has_key(attrtype_lower):
old_value = old_entry.get(attrtype_lower_map[attrtype_lower],[])
del attrtype_lower_map[attrtype_lower]
else:
old_value = []
if not old_value and new_value:
# Add a new attribute to entry
modlist.append((ldap.MOD_ADD,attrtype,new_value))
elif old_value and new_value:
new_value.sort() ; old_value.sort()
if new_value!=old_value:
# Replace attribute value of existing attribute
# modify an existing attribute
modlist.append((ldap.MOD_REPLACE,attrtype,new_value))
elif old_value and not new_value:
# delete an existing attribute because attribute
# value list is empty
modlist.append((ldap.MOD_DELETE,attrtype,old_value))
if not ignore_oldexistent:
# Remove all attributes of old_entry which are not present
# in new_entry at all
for a in attrtype_lower_map.keys():
if a in ignore_attr_types:
# This attribute type is ignored
continue
attrtype = attrtype_lower_map[a]
modlist.append((ldap.MOD_DELETE,attrtype,old_entry[attrtype]))
return modlist
def test():
"""Test functions"""
print '\nTesting function addModlist():'
addModlist_tests = [
(
{
'objectClass':['person','pilotPerson'],
'cn':['Michael Str\303\266der','Michael Stroeder'],
'sn':['Str\303\266der'],
},
[
('objectClass',['person','pilotPerson']),
('cn',['Michael Str\303\266der','Michael Stroeder']),
('sn',['Str\303\266der']),
]
),
]
for entry,test_modlist in addModlist_tests:
test_modlist.sort()
result_modlist = addModlist(entry)
result_modlist.sort()
if test_modlist!=result_modlist:
print 'addModlist(%s) returns\n%s\ninstead of\n%s.' % (
repr(entry),repr(result_modlist),repr(test_modlist)
)
print '\nTesting function modifyModlist():'
modifyModlist_tests = [
(
{
'objectClass':['person','pilotPerson'],
'cn':['Michael Str\303\266der','Michael Stroeder'],
'sn':['Str\303\266der'],
'c':['DE'],
},
{
'objectClass':['person','inetOrgPerson'],
'cn':['Michael Str\303\266der','Michael Stroeder'],
'sn':[],
'mail':['michael@stroeder.com'],
},
[
(ldap.MOD_REPLACE,'objectClass',['inetOrgPerson','person']),
(ldap.MOD_DELETE,'c',['DE']),
(ldap.MOD_DELETE,'sn',['Str\303\266der']),
(ldap.MOD_ADD,'mail',['michael@stroeder.com']),
]
),
]
for old_entry,new_entry,test_modlist in modifyModlist_tests:
test_modlist.sort()
result_modlist = modifyModlist(old_entry,new_entry)
result_modlist.sort()
if test_modlist!=result_modlist:
print 'modifyModlist(%s,%s) returns\n%s\ninstead of\n%s.' % (
repr(old_entry),
repr(new_entry),
repr(result_modlist),
repr(test_modlist)
)
if __name__ == '__main__':
test()
|