port of Net::LDAP::Entry

Robert Sander ml-it-python-ldap-dev at epigenomics.com
Thu Jul 25 10:31:33 CEST 2002


Hi!

We have been using the Perl Net::LDAP modules to access our directory
server. Now we want to switch to Python. This is a first port of
Net::LDAP::Entry 

class Entry:
    import ldap
    def __init__(self, dn, values = {}):
        self.distname = dn
        self.values = values
        self.chgtype = "add"
        self.changes = []
    def __str__(self):
        return "%s[%s]: %s" % ( self.distname, self.chgtype, self.values )
    def get_value(self, attr):
        try:
            return self.values[attr]
        except KeyError, er:
            return None
    def changetype(self, typ = None):
        if typ:
            self.chgtype = typ
            self.changes = []
        else:
            return self.chgtype
    def dn(self, dn = None):
        if dn:
            self.distname = dn
        else:
            return self.distname
    def exists(self, attr):
        return self.values.has_key(attr)
    def add(self, attrs):
        if type(attrs) != type([]):
            attrs = [attrs]
        for typ, val in attrs:
            if type(val) != type([]):
                val = [val]
            if self.values.has_key(typ):
                self.values[typ].extend(val)
                if self.chgtype == "modify":
                    self.changes.append((ldap.MOD_REPLACE, typ, self.values[typ]))
            else:
                self.values[typ] = val
                if self.chgtype == "modify":
                    self.changes.append((ldap.MOD_ADD, typ, val))
    def replace(self, attrs):
        if type(attrs) != type([]):
            attrs = [attrs]
        for typ, val in attrs:
            if type(val) != type([]):
                val = [val]
            self.values[typ] = val
            if self.chgtype == "modify":
                self.changes.append((ldap.MOD_REPLACE, typ, val))
    def delete(self, attrs = None):
        if attrs:
            if type(attrs) != type([]):
                attrs = [attrs]
            for attr in attrs:
                if self.values.has_key(attr):
                    del(self.values[attr])
                    if self.chgtype == "modify":
                        self.changes.append((ldap.MOD_DELETE, attr, None))
    def update(self, l):
        if self.chgtype == "add":
            import ldap.modlist
            return l.add_s(self.distname, ldap.modlist.addModlist(self.values))
        elif self.chgtype == "delete":
            return l.delete_s(self.distname)
        elif self.chgtype == "modify":
            return l.modify_s(self.distname, self.changes)


You may find it useful.

We have a directory service set up with OpenLDAP extensively using
referrals. It seems that python-ldap currently is not able to handle
these. Is this correct?

Greetings
-- 
Robert Sander
Manager
Information Systems        www.epigenomics.com        Kastanienallee 24
+493024345330                                              10435 Berlin





More information about the python-ldap mailing list