|
9 | 9 | import ldap
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def addModlist(entry,ignore_attr_types=None): |
13 |
| - """Build modify list for call of method LDAPObject.add()""" |
14 |
| - ignore_attr_types = set(map(str.lower,ignore_attr_types or [])) |
15 |
| - modlist = [] |
16 |
| - for attrtype in entry.keys(): |
17 |
| - if attrtype.lower() in ignore_attr_types: |
18 |
| - # This attribute type is ignored |
19 |
| - continue |
20 |
| - # Eliminate empty attr value strings in list |
21 |
| - attrvaluelist = filter(lambda x:x!=None,entry[attrtype]) |
22 |
| - if attrvaluelist: |
23 |
| - modlist.append((attrtype,entry[attrtype])) |
24 |
| - return modlist # addModlist() |
| 12 | +def addModlist(entry, ignore_attr_types=None): |
| 13 | + """Build modify list for call of method LDAPObject.add()""" |
| 14 | + ignore_attr_types = set(map(str.lower, ignore_attr_types or [])) |
| 15 | + modlist = [] |
| 16 | + for attrtype in entry.keys(): |
| 17 | + if attrtype.lower() in ignore_attr_types: |
| 18 | + # This attribute type is ignored |
| 19 | + continue |
| 20 | + # Eliminate empty attr value strings in list |
| 21 | + attrvaluelist = [ |
| 22 | + val |
| 23 | + for val in entry[attrtype] |
| 24 | + if val is not None |
| 25 | + ] |
| 26 | + if attrvaluelist: |
| 27 | + modlist.append((attrtype, entry[attrtype])) |
| 28 | + return modlist # addModlist() |
25 | 29 |
|
26 | 30 |
|
27 | 31 | def modifyModlist(
|
28 |
| - old_entry,new_entry,ignore_attr_types=None,ignore_oldexistent=0,case_ignore_attr_types=None |
29 |
| -): |
30 |
| - """ |
31 |
| - Build differential modify list for calling LDAPObject.modify()/modify_s() |
| 32 | + old_entry, |
| 33 | + new_entry, |
| 34 | + ignore_attr_types=None, |
| 35 | + ignore_oldexistent=0, |
| 36 | + case_ignore_attr_types=None |
| 37 | + ): |
| 38 | + """ |
| 39 | + Build differential modify list for calling LDAPObject.modify()/modify_s() |
32 | 40 |
|
33 |
| - old_entry |
34 |
| - Dictionary holding the old entry |
35 |
| - new_entry |
36 |
| - Dictionary holding what the new entry should be |
37 |
| - ignore_attr_types |
38 |
| - List of attribute type names to be ignored completely |
39 |
| - ignore_oldexistent |
40 |
| - If non-zero attribute type names which are in old_entry |
41 |
| - but are not found in new_entry at all are not deleted. |
42 |
| - This is handy for situations where your application |
43 |
| - sets attribute value to '' for deleting an attribute. |
44 |
| - In most cases leave zero. |
45 |
| - case_ignore_attr_types |
46 |
| - List of attribute type names for which comparison will be made |
47 |
| - case-insensitive |
48 |
| - """ |
49 |
| - ignore_attr_types = set(map(str.lower,ignore_attr_types or [])) |
50 |
| - case_ignore_attr_types = set(map(str.lower,case_ignore_attr_types or [])) |
51 |
| - modlist = [] |
52 |
| - attrtype_lower_map = {} |
53 |
| - for a in old_entry.keys(): |
54 |
| - attrtype_lower_map[str.lower(a)]=a |
55 |
| - for attrtype in new_entry.keys(): |
56 |
| - attrtype_lower = str.lower(attrtype) |
57 |
| - if attrtype_lower in ignore_attr_types: |
58 |
| - # This attribute type is ignored |
59 |
| - continue |
60 |
| - # Filter away null-strings |
61 |
| - new_value = filter(lambda x:x!=None,new_entry[attrtype]) |
62 |
| - if attrtype_lower in attrtype_lower_map: |
63 |
| - old_value = old_entry.get(attrtype_lower_map[attrtype_lower],[]) |
64 |
| - old_value = filter(lambda x:x!=None,old_value) |
65 |
| - del attrtype_lower_map[attrtype_lower] |
66 |
| - else: |
67 |
| - old_value = [] |
68 |
| - if not old_value and new_value: |
69 |
| - # Add a new attribute to entry |
70 |
| - modlist.append((ldap.MOD_ADD,attrtype,new_value)) |
71 |
| - elif old_value and new_value: |
72 |
| - # Replace existing attribute |
73 |
| - replace_attr_value = len(old_value)!=len(new_value) |
74 |
| - if not replace_attr_value: |
75 |
| - if attrtype_lower in case_ignore_attr_types: |
76 |
| - norm_func = str.lower |
77 |
| - old_value_set = set(map(str.lower,old_value)) |
78 |
| - new_value_set = set(map(str.lower,new_value)) |
| 41 | + old_entry |
| 42 | + Dictionary holding the old entry |
| 43 | + new_entry |
| 44 | + Dictionary holding what the new entry should be |
| 45 | + ignore_attr_types |
| 46 | + List of attribute type names to be ignored completely |
| 47 | + ignore_oldexistent |
| 48 | + If non-zero attribute type names which are in old_entry |
| 49 | + but are not found in new_entry at all are not deleted. |
| 50 | + This is handy for situations where your application |
| 51 | + sets attribute value to '' for deleting an attribute. |
| 52 | + In most cases leave zero. |
| 53 | + case_ignore_attr_types |
| 54 | + List of attribute type names for which comparison will be made |
| 55 | + case-insensitive |
| 56 | + """ |
| 57 | + ignore_attr_types = set(map(str.lower, ignore_attr_types or [])) |
| 58 | + case_ignore_attr_types = set(map(str.lower, case_ignore_attr_types or [])) |
| 59 | + modlist = [] |
| 60 | + attrtype_lower_map = {} |
| 61 | + for a in old_entry.keys(): |
| 62 | + attrtype_lower_map[str.lower(a)] = a |
| 63 | + for attrtype in new_entry.keys(): |
| 64 | + attrtype_lower = str.lower(attrtype) |
| 65 | + if attrtype_lower in ignore_attr_types: |
| 66 | + # This attribute type is ignored |
| 67 | + continue |
| 68 | + # Filter away null-strings |
| 69 | + new_value = [ |
| 70 | + val |
| 71 | + for val in new_entry[attrtype] |
| 72 | + if val is not None |
| 73 | + ] |
| 74 | + if attrtype_lower in attrtype_lower_map: |
| 75 | + old_value = [ |
| 76 | + val |
| 77 | + for val in old_entry.get(attrtype_lower_map[attrtype_lower], []) |
| 78 | + if val is not None |
| 79 | + ] |
| 80 | + del attrtype_lower_map[attrtype_lower] |
79 | 81 | else:
|
80 |
| - old_value_set = set(old_value) |
81 |
| - new_value_set = set(new_value) |
82 |
| - replace_attr_value = new_value_set != old_value_set |
83 |
| - if replace_attr_value: |
84 |
| - modlist.append((ldap.MOD_DELETE,attrtype,None)) |
85 |
| - modlist.append((ldap.MOD_ADD,attrtype,new_value)) |
86 |
| - elif old_value and not new_value: |
87 |
| - # Completely delete an existing attribute |
88 |
| - modlist.append((ldap.MOD_DELETE,attrtype,None)) |
89 |
| - if not ignore_oldexistent: |
90 |
| - # Remove all attributes of old_entry which are not present |
91 |
| - # in new_entry at all |
92 |
| - for a in attrtype_lower_map.keys(): |
93 |
| - if a in ignore_attr_types: |
94 |
| - # This attribute type is ignored |
95 |
| - continue |
96 |
| - attrtype = attrtype_lower_map[a] |
97 |
| - modlist.append((ldap.MOD_DELETE,attrtype,None)) |
98 |
| - return modlist # modifyModlist() |
| 82 | + old_value = [] |
| 83 | + if not old_value and new_value: |
| 84 | + # Add a new attribute to entry |
| 85 | + modlist.append((ldap.MOD_ADD, attrtype, new_value)) |
| 86 | + elif old_value and new_value: |
| 87 | + # Replace existing attribute |
| 88 | + replace_attr_value = len(old_value) != len(new_value) |
| 89 | + if not replace_attr_value: |
| 90 | + if attrtype_lower in case_ignore_attr_types: |
| 91 | + old_value_set = set(map(str.lower, old_value)) |
| 92 | + new_value_set = set(map(str.lower, new_value)) |
| 93 | + else: |
| 94 | + old_value_set = set(old_value) |
| 95 | + new_value_set = set(new_value) |
| 96 | + replace_attr_value = new_value_set != old_value_set |
| 97 | + if replace_attr_value: |
| 98 | + modlist.append((ldap.MOD_DELETE, attrtype, None)) |
| 99 | + modlist.append((ldap.MOD_ADD, attrtype, new_value)) |
| 100 | + elif old_value and not new_value: |
| 101 | + # Completely delete an existing attribute |
| 102 | + modlist.append((ldap.MOD_DELETE, attrtype, None)) |
| 103 | + if not ignore_oldexistent: |
| 104 | + # Remove all attributes of old_entry which are not present |
| 105 | + # in new_entry at all |
| 106 | + for a in attrtype_lower_map.keys(): |
| 107 | + if a in ignore_attr_types: |
| 108 | + # This attribute type is ignored |
| 109 | + continue |
| 110 | + attrtype = attrtype_lower_map[a] |
| 111 | + modlist.append((ldap.MOD_DELETE, attrtype, None)) |
| 112 | + return modlist # modifyModlist() |
0 commit comments