def generateModList(self, old_entry, new_entry): """A mod list generator that computes more precise modification lists than the python-ldap version. For single-value attributes always use a REPLACE operation, otherwise use ADD/DEL. """ # Some attributes, like those in cn=config, need to be replaced # not deleted/added. FORCE_REPLACE_ON_UPDATE_ATTRS = ('nsslapd-ssl-check-hostname', 'nsslapd-lookthroughlimit', 'nsslapd-idlistscanlimit', 'nsslapd-anonlimitsdn', 'nsslapd-minssf-exclude-rootdse') modlist = [] old_entry = ipautil.CIDict(old_entry) new_entry = ipautil.CIDict(new_entry) keys = set(map(string.lower, old_entry.keys())) keys.update(map(string.lower, new_entry.keys())) for key in keys: new_values = new_entry.get(key, []) if not(isinstance(new_values,list) or isinstance(new_values,tuple)): new_values = [new_values] new_values = filter(lambda value:value!=None, new_values) old_values = old_entry.get(key, []) if not(isinstance(old_values,list) or isinstance(old_values,tuple)): old_values = [old_values] old_values = filter(lambda value:value!=None, old_values) # We used to convert to sets and use difference to calculate # the changes but this did not preserve order which is important # particularly for schema adds = [x for x in new_values if x not in old_values] removes = [x for x in old_values if x not in new_values] if len(adds) == 0 and len(removes) == 0: continue is_single_value = self.get_single_value(key) force_replace = False if key in FORCE_REPLACE_ON_UPDATE_ATTRS or is_single_value: force_replace = True # You can't remove schema online. An add will automatically # replace any existing schema. if old_entry.get('dn', DN()) == DN(('cn', 'schema')): if len(adds) > 0: modlist.append((ldap.MOD_ADD, key, adds)) else: if adds: if force_replace: modlist.append((ldap.MOD_REPLACE, key, adds)) else: modlist.append((ldap.MOD_ADD, key, adds)) if removes: if not force_replace: modlist.append((ldap.MOD_DELETE, key, removes)) return modlist
def test_init(self): cidict = ipautil.CIDict() assert dict(cidict.items()) == {} cidict = ipautil.CIDict([('a', 2), ('b', 3), ('C', 4)]) assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4} cidict = ipautil.CIDict([('a', 2), ('b', None)], b=3, C=4) assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4} cidict = ipautil.CIDict({'a': 2, 'b': None}, b=3, C=4) assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4} cidict = ipautil.CIDict(a=2, b=3, C=4) assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
def toDict(self): """Convert the attrs and values to a dict. The dict is keyed on the attribute name. The value is either single value or a list of values.""" assert isinstance(self.dn, DN) result = ipautil.CIDict(self.data) result['dn'] = self.dn return result
def __init__(self,entrydata): """data is the raw data returned from the python-ldap result method, which is a search result entry or a reference or None. If creating a new empty entry, data is the string DN.""" if entrydata: if isinstance(entrydata,tuple): self.dn = entrydata[0] self.data = ipautil.CIDict(entrydata[1]) elif isinstance(entrydata,DN): self.dn = entrydata self.data = ipautil.CIDict() elif isinstance(entrydata, basestring): self.dn = DN(entrydata) self.data = ipautil.CIDict() else: raise TypeError("entrydata must be 2-tuple, DN, or basestring, got %s" % type(entrydata)) else: self.dn = DN() self.data = ipautil.CIDict() assert isinstance(self.dn, DN)
def __init__(self, entrydata=None): """data is the raw data returned from the python-ldap result method, which is a search result entry or a reference or None. If creating a new empty entry, data is the string DN.""" if entrydata: if isinstance(entrydata, tuple): self.dn = entrydata[0] self.data = ipautil.CIDict(entrydata[1]) elif isinstance(entrydata, DN): self.dn = entrydata self.data = ipautil.CIDict() elif isinstance(entrydata, basestring): self.dn = DN(entrydata) self.data = ipautil.CIDict() elif isinstance(entrydata, dict): self.dn = entrydata['dn'] del entrydata['dn'] self.data = ipautil.CIDict(entrydata) else: self.dn = DN() self.data = ipautil.CIDict() assert isinstance(self.dn, DN) self.orig_data = ipautil.CIDict(copy_CIDict(self.data))
def cidict_setup(self): self.cidict = ipautil.CIDict() self.cidict["Key1"] = "val1" self.cidict["key2"] = "val2" self.cidict["KEY3"] = "VAL3"
def origDataDict(self): """Returns a dict of the original values of the user. Used for updates.""" assert isinstance(self.dn, DN) result = ipautil.CIDict(self.orig_data) result['dn'] = self.dn return result