<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> import UserDict <EOL> from urllib import quote , unquote <EOL> LDAP_SCOPE_BASE = <NUM_LIT:0> <EOL> LDAP_SCOPE_ONELEVEL = <NUM_LIT:1> <EOL> LDAP_SCOPE_SUBTREE = <NUM_LIT:2> <EOL> SEARCH_SCOPE_STR = { None : '<STR_LIT>' , <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' } <EOL> SEARCH_SCOPE = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : LDAP_SCOPE_BASE , <EOL> '<STR_LIT>' : LDAP_SCOPE_ONELEVEL , <EOL> '<STR_LIT>' : LDAP_SCOPE_SUBTREE , <EOL> } <EOL> StringType = type ( '<STR_LIT>' ) <EOL> TupleType = type ( ( ) ) <EOL> def isLDAPUrl ( s ) : <EOL> """<STR_LIT>""" <EOL> s_lower = s . lower ( ) <EOL> return s_lower . startswith ( '<STR_LIT>' ) or s_lower . startswith ( '<STR_LIT>' ) or s_lower . startswith ( '<STR_LIT>' ) <EOL> def ldapUrlEscape ( s ) : <EOL> """<STR_LIT>""" <EOL> return quote ( s ) . replace ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) . replace ( '<STR_LIT:/>' , '<STR_LIT>' ) <EOL> class LDAPUrlExtension : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , extensionStr = None , critical = <NUM_LIT:0> , extype = None , exvalue = None ) : <EOL> self . critical = critical <EOL> self . extype = extype <EOL> self . exvalue = exvalue <EOL> if extensionStr : <EOL> self . _parse ( extensionStr ) <EOL> def _parse ( self , extension ) : <EOL> extension = extension . strip ( ) <EOL> if not extension : <EOL> self . extype , self . exvalue = None , None <EOL> return <EOL> self . critical = extension [ <NUM_LIT:0> ] == '<STR_LIT:!>' <EOL> if extension [ <NUM_LIT:0> ] == '<STR_LIT:!>' : <EOL> extension = extension [ <NUM_LIT:1> : ] . strip ( ) <EOL> try : <EOL> self . extype , self . exvalue = extension . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> self . extype , self . exvalue = extension , None <EOL> else : <EOL> self . exvalue = unquote ( self . exvalue . strip ( ) ) <EOL> self . extype = self . extype . strip ( ) <EOL> def unparse ( self ) : <EOL> if self . exvalue is None : <EOL> return '<STR_LIT>' % ( '<STR_LIT:!>' * ( self . critical > <NUM_LIT:0> ) , self . extype ) <EOL> else : <EOL> return '<STR_LIT>' % ( <EOL> '<STR_LIT:!>' * ( self . critical > <NUM_LIT:0> ) , <EOL> self . extype , quote ( self . exvalue or '<STR_LIT>' ) <EOL> ) <EOL> def __str__ ( self ) : <EOL> return self . unparse ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> self . __dict__ <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> return ( self . critical == other . critical ) and ( self . extype == other . extype ) and ( self . exvalue == other . exvalue ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class LDAPUrlExtensions ( UserDict . UserDict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = None ) : <EOL> UserDict . UserDict . __init__ ( self ) <EOL> for k , v in ( default or { } ) . items ( ) : <EOL> self [ k ] = v <EOL> def __setitem__ ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( value , LDAPUrlExtension ) <EOL> assert name == value . extype <EOL> self . data [ name ] = value <EOL> def values ( self ) : <EOL> return [ <EOL> self [ k ] <EOL> for k in self . keys ( ) <EOL> ] <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+002C>' . join ( map ( str , self . values ( ) ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> self . data <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> assert isinstance ( other , self . __class__ ) , TypeError ( <EOL> "<STR_LIT>" % ( self . __class__ ) <EOL> ) <EOL> return self . data == other . data <EOL> def parse ( self , extListStr ) : <EOL> for extension_str in extListStr . strip ( ) . split ( '<STR_LIT:U+002C>' ) : <EOL> if extension_str : <EOL> e = LDAPUrlExtension ( extension_str ) <EOL> self [ e . extype ] = e <EOL> def unparse ( self ) : <EOL> return '<STR_LIT:U+002C>' . join ( [ v . unparse ( ) for v in self . values ( ) ] ) <EOL> class LDAPUrl : <EOL> """<STR_LIT>""" <EOL> attr2extype = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def __init__ ( <EOL> self , <EOL> ldapUrl = None , <EOL> urlscheme = '<STR_LIT>' , <EOL> hostport = '<STR_LIT>' , dn = '<STR_LIT>' , attrs = None , scope = None , filterstr = None , <EOL> extensions = None , <EOL> who = None , cred = None <EOL> ) : <EOL> self . urlscheme = urlscheme <EOL> self . hostport = hostport <EOL> self . dn = dn <EOL> self . attrs = attrs <EOL> self . scope = scope <EOL> self . filterstr = filterstr <EOL> self . extensions = ( extensions or LDAPUrlExtensions ( { } ) ) <EOL> if ldapUrl != None : <EOL> self . _parse ( ldapUrl ) <EOL> if who != None : <EOL> self . who = who <EOL> if cred != None : <EOL> self . cred = cred <EOL> def __eq__ ( self , other ) : <EOL> return self . urlscheme == other . urlscheme and self . hostport == other . hostport and self . dn == other . dn and self . attrs == other . attrs and self . scope == other . scope and self . filterstr == other . filterstr and self . extensions == other . extensions <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def _parse ( self , ldap_url ) : <EOL> """<STR_LIT>""" <EOL> if not isLDAPUrl ( ldap_url ) : <EOL> raise ValueError , '<STR_LIT>' <EOL> scheme , rest = ldap_url . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . urlscheme = scheme . strip ( ) <EOL> if not self . urlscheme in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError , '<STR_LIT>' % ( self . urlscheme ) <EOL> slash_pos = rest . find ( '<STR_LIT:/>' ) <EOL> qemark_pos = rest . find ( '<STR_LIT:?>' ) <EOL> if ( slash_pos == - <NUM_LIT:1> ) and ( qemark_pos == - <NUM_LIT:1> ) : <EOL> self . hostport = unquote ( rest ) <EOL> self . dn = '<STR_LIT>' <EOL> return <EOL> else : <EOL> if slash_pos != - <NUM_LIT:1> and ( qemark_pos == - <NUM_LIT:1> or ( slash_pos < qemark_pos ) ) : <EOL> self . hostport = unquote ( rest [ : slash_pos ] ) <EOL> rest = rest [ slash_pos + <NUM_LIT:1> : ] <EOL> elif qemark_pos != <NUM_LIT:1> and ( slash_pos == - <NUM_LIT:1> or ( slash_pos > qemark_pos ) ) : <EOL> self . hostport = unquote ( rest [ : qemark_pos ] ) <EOL> rest = rest [ qemark_pos : ] <EOL> else : <EOL> raise ValueError , '<STR_LIT>' <EOL> paramlist = rest . split ( '<STR_LIT:?>' , <NUM_LIT:4> ) <EOL> paramlist_len = len ( paramlist ) <EOL> if paramlist_len >= <NUM_LIT:1> : <EOL> self . dn = unquote ( paramlist [ <NUM_LIT:0> ] ) . strip ( ) <EOL> if ( paramlist_len >= <NUM_LIT:2> ) and ( paramlist [ <NUM_LIT:1> ] ) : <EOL> self . attrs = unquote ( paramlist [ <NUM_LIT:1> ] . strip ( ) ) . split ( '<STR_LIT:U+002C>' ) <EOL> if paramlist_len >= <NUM_LIT:3> : <EOL> scope = paramlist [ <NUM_LIT:2> ] . strip ( ) <EOL> try : <EOL> self . scope = SEARCH_SCOPE [ scope ] <EOL> except KeyError : <EOL> raise ValueError , "<STR_LIT>" % ( repr ( scope ) ) <EOL> if paramlist_len >= <NUM_LIT:4> : <EOL> filterstr = paramlist [ <NUM_LIT:3> ] . strip ( ) <EOL> if not filterstr : <EOL> self . filterstr = None <EOL> else : <EOL> self . filterstr = unquote ( filterstr ) <EOL> if paramlist_len >= <NUM_LIT:5> : <EOL> if paramlist [ <NUM_LIT:4> ] : <EOL> self . extensions = LDAPUrlExtensions ( ) <EOL> self . extensions . parse ( paramlist [ <NUM_LIT:4> ] ) <EOL> else : <EOL> self . extensions = None <EOL> return <EOL> def applyDefaults ( self , defaults ) : <EOL> """<STR_LIT>""" <EOL> for k in defaults . keys ( ) : <EOL> if getattr ( self , k ) is None : <EOL> setattr ( self , k , defaults [ k ] ) <EOL> def initializeUrl ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . urlscheme == '<STR_LIT>' : <EOL> hostport = ldapUrlEscape ( self . hostport ) <EOL> else : <EOL> hostport = self . hostport <EOL> return '<STR_LIT>' % ( self . urlscheme , hostport ) <EOL> def unparse ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . attrs is None : <EOL> attrs_str = '<STR_LIT>' <EOL> else : <EOL> attrs_str = '<STR_LIT:U+002C>' . join ( self . attrs ) <EOL> scope_str = SEARCH_SCOPE_STR [ self . scope ] <EOL> if self . filterstr is None : <EOL> filterstr = '<STR_LIT>' <EOL> else : <EOL> filterstr = ldapUrlEscape ( self . filterstr ) <EOL> dn = ldapUrlEscape ( self . dn ) <EOL> if self . urlscheme == '<STR_LIT>' : <EOL> hostport = ldapUrlEscape ( self . hostport ) <EOL> else : <EOL> hostport = self . hostport <EOL> ldap_url = '<STR_LIT>' % ( <EOL> self . urlscheme , <EOL> hostport , dn , attrs_str , scope_str , filterstr <EOL> ) <EOL> if self . extensions : <EOL> ldap_url = ldap_url + '<STR_LIT:?>' + self . extensions . unparse ( ) <EOL> return ldap_url <EOL> def htmlHREF ( self , urlPrefix = '<STR_LIT>' , hrefText = None , hrefTarget = None ) : <EOL> """<STR_LIT>""" <EOL> assert type ( urlPrefix ) == StringType , "<STR_LIT>" <EOL> if hrefText is None : <EOL> hrefText = self . unparse ( ) <EOL> assert type ( hrefText ) == StringType , "<STR_LIT>" <EOL> if hrefTarget is None : <EOL> target = '<STR_LIT>' <EOL> else : <EOL> assert type ( hrefTarget ) == StringType , "<STR_LIT>" <EOL> target = '<STR_LIT>' % hrefTarget <EOL> return '<STR_LIT>' % ( <EOL> target , urlPrefix , self . unparse ( ) , hrefText <EOL> ) <EOL> def __str__ ( self ) : <EOL> return self . unparse ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) , <EOL> self . __dict__ <EOL> ) <EOL> def __getattr__ ( self , name ) : <EOL> if self . attr2extype . has_key ( name ) : <EOL> extype = self . attr2extype [ name ] <EOL> if self . extensions and self . extensions . has_key ( extype ) and not self . extensions [ extype ] . exvalue is None : <EOL> result = unquote ( self . extensions [ extype ] . exvalue ) <EOL> else : <EOL> return None <EOL> else : <EOL> raise AttributeError , "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , name <EOL> ) <EOL> return result <EOL> def __setattr__ ( self , name , value ) : <EOL> if self . attr2extype . has_key ( name ) : <EOL> extype = self . attr2extype [ name ] <EOL> if value is None : <EOL> delattr ( self , name ) <EOL> elif value != None : <EOL> self . extensions [ extype ] = LDAPUrlExtension ( <EOL> extype = extype , exvalue = unquote ( value ) <EOL> ) <EOL> else : <EOL> self . __dict__ [ name ] = value <EOL> def __delattr__ ( self , name ) : <EOL> if self . attr2extype . has_key ( name ) : <EOL> extype = self . attr2extype [ name ] <EOL> if self . extensions : <EOL> try : <EOL> del self . extensions [ extype ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> del self . __dict__ [ name ] </s>
