<s> """<STR_LIT>""" <EOL> import os , shlex <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class NetrcParseError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg , filename = None , lineno = None ) : <EOL> self . filename = filename <EOL> self . lineno = lineno <EOL> self . msg = msg <EOL> Exception . __init__ ( self , msg ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . msg , self . filename , self . lineno ) <EOL> class netrc : <EOL> def __init__ ( self , file = None ) : <EOL> if file is None : <EOL> try : <EOL> file = os . path . join ( os . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> except KeyError : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> self . hosts = { } <EOL> self . macros = { } <EOL> fp = open ( file ) <EOL> try : <EOL> self . _parse ( fp ) <EOL> finally : <EOL> fp . close ( ) <EOL> def _parse ( self , fp ) : <EOL> lexer = shlex . shlex ( fp ) <EOL> lexer . wordchars += r"""<STR_LIT>""" <EOL> while <NUM_LIT:1> : <EOL> toplevel = tt = lexer . get_token ( ) <EOL> if not tt : <EOL> break <EOL> elif tt == '<STR_LIT>' : <EOL> entryname = lexer . get_token ( ) <EOL> elif tt == '<STR_LIT:default>' : <EOL> entryname = '<STR_LIT:default>' <EOL> elif tt == '<STR_LIT>' : <EOL> entryname = lexer . get_token ( ) <EOL> self . macros [ entryname ] = [ ] <EOL> lexer . whitespace = '<STR_LIT>' <EOL> while <NUM_LIT:1> : <EOL> line = lexer . instream . readline ( ) <EOL> if not line or line == '<STR_LIT>' : <EOL> lexer . whitespace = '<STR_LIT>' <EOL> break <EOL> self . macros [ entryname ] . append ( line ) <EOL> continue <EOL> else : <EOL> raise NetrcParseError ( <EOL> "<STR_LIT>" % tt , file , lexer . lineno ) <EOL> login = '<STR_LIT>' <EOL> account = password = None <EOL> self . hosts [ entryname ] = { } <EOL> while <NUM_LIT:1> : <EOL> tt = lexer . get_token ( ) <EOL> if ( tt == '<STR_LIT>' or tt == '<STR_LIT>' or <EOL> tt == '<STR_LIT:default>' or tt == '<STR_LIT>' ) : <EOL> if password : <EOL> self . hosts [ entryname ] = ( login , account , password ) <EOL> lexer . push_token ( tt ) <EOL> break <EOL> else : <EOL> raise NetrcParseError ( <EOL> "<STR_LIT>" <EOL> % ( toplevel , entryname , repr ( tt ) ) , <EOL> file , lexer . lineno ) <EOL> elif tt == '<STR_LIT>' or tt == '<STR_LIT:user>' : <EOL> login = lexer . get_token ( ) <EOL> elif tt == '<STR_LIT>' : <EOL> account = lexer . get_token ( ) <EOL> elif tt == '<STR_LIT:password>' : <EOL> password = lexer . get_token ( ) <EOL> else : <EOL> raise NetrcParseError ( "<STR_LIT>" % tt , <EOL> file , lexer . lineno ) <EOL> fp . close ( ) <EOL> def authenticators ( self , host ) : <EOL> """<STR_LIT>""" <EOL> if host in self . hosts : <EOL> return self . hosts [ host ] <EOL> elif '<STR_LIT:default>' in self . hosts : <EOL> return self . hosts [ '<STR_LIT:default>' ] <EOL> else : <EOL> return None <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> rep = "<STR_LIT>" <EOL> for host in self . hosts . keys ( ) : <EOL> attrs = self . hosts [ host ] <EOL> rep = rep + "<STR_LIT>" + host + "<STR_LIT>" + repr ( attrs [ <NUM_LIT:0> ] ) + "<STR_LIT:\n>" <EOL> if attrs [ <NUM_LIT:1> ] : <EOL> rep = rep + "<STR_LIT>" + repr ( attrs [ <NUM_LIT:1> ] ) <EOL> rep = rep + "<STR_LIT>" + repr ( attrs [ <NUM_LIT:2> ] ) + "<STR_LIT:\n>" <EOL> for macro in self . macros . keys ( ) : <EOL> rep = rep + "<STR_LIT>" + macro + "<STR_LIT:\n>" <EOL> for line in self . macros [ macro ] : <EOL> rep = rep + line <EOL> rep = rep + "<STR_LIT:\n>" <EOL> return rep <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print netrc ( ) </s>
