Novell Home

Class DN - Python class to represent a distinguished name

From Developer Community

This Python class, called DN, represents a single distinguished name. It parses the different elements of the DN and can display them in the proper format for use when connecting.

Class

class DN:
    __ou = []
    __o = ""
    def __init__( self, dn ):
        items = dn.split( "," )
        self.__ou = []
        for item in items:
            if item[:2] == "ou":
                self.__ou.append( item[3:] )
            elif item[:2] == "o=":
                self.__o = item[2:]
        if self.__o == "":
            raise ValueError, "DN requires value for o"
    def toString(self):
        rstr = "";
        for value in self.__ou:
            rstr += "ou="+value+","
        rstr += "o="+self.__o
        return rstr

def selftest_passes():
    dn1 = DN("o=a");
    dn2 = DN("ou=b,o=a");
    return (dn1.toString() == "o=a" and
            dn2.toString() == "ou=b,o=a")

assert( selftest_passes() )

--Matt Ryan

© 2008 Novell, Inc. All Rights Reserved.