<s> from twisted . internet import reactor <EOL> from twisted . web import server , resource <EOL> from ldaptor . protocols . ldap import ldapclient , ldapsyntax , ldapconnector , distinguishedname <EOL> from ldaptor import ldapfilter <EOL> class LDAPConfig ( object ) : <EOL> def __init__ ( self , <EOL> baseDN , <EOL> serviceLocationOverrides = None ) : <EOL> self . baseDN = distinguishedname . DistinguishedName ( baseDN ) <EOL> self . serviceLocationOverrides = { } <EOL> if serviceLocationOverrides is not None : <EOL> for k , v in serviceLocationOverrides . items ( ) : <EOL> dn = distinguishedname . DistinguishedName ( k ) <EOL> self . serviceLocationOverrides [ dn ] = v <EOL> def getBaseDN ( self ) : <EOL> return self . baseDN <EOL> def getServiceLocationOverrides ( self ) : <EOL> return self . serviceLocationOverrides <EOL> class AddressBookResource ( resource . Resource ) : <EOL> def __init__ ( self , config ) : <EOL> resource . Resource . __init__ ( self ) <EOL> self . config = config <EOL> self . putChild ( '<STR_LIT>' , self ) <EOL> def _search ( self ) : <EOL> c = ldapconnector . LDAPClientCreator ( reactor , ldapclient . LDAPClient ) <EOL> d = c . connectAnonymously ( self . config . getBaseDN ( ) , <EOL> self . config . getServiceLocationOverrides ( ) ) <EOL> def _doSearch ( proto ) : <EOL> searchFilter = ldapfilter . parseFilter ( '<STR_LIT>' ) <EOL> baseEntry = ldapsyntax . LDAPEntry ( client = proto , <EOL> dn = self . config . getBaseDN ( ) ) <EOL> d = baseEntry . search ( filterObject = searchFilter ) <EOL> return d <EOL> d . addCallback ( _doSearch ) <EOL> return d <EOL> def _show ( self , results , write ) : <EOL> for item in results : <EOL> write ( '<STR_LIT>' ) <EOL> write ( str ( item ) ) <EOL> write ( '<STR_LIT>' ) <EOL> def render ( self , request ) : <EOL> d = self . _search ( ) <EOL> d . addCallback ( self . _show , request . write ) <EOL> d . addErrback ( lambda e : request . write ( str ( e ) ) ) <EOL> d . addBoth ( lambda _ : request . finish ( ) ) <EOL> return server . NOT_DONE_YET <EOL> def getSite ( config ) : <EOL> return server . Site ( AddressBookResource ( config ) ) </s>
