<s> """<STR_LIT>""" <EOL> from twisted . names import dns <EOL> from twisted . persisted import styles <EOL> from twisted . python import failure <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . internet import defer <EOL> from twisted . internet . abstract import isIPAddress <EOL> from twisted . names import common <EOL> def searchFileForAll ( hostsFile , name ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> try : <EOL> lines = hostsFile . getContent ( ) . splitlines ( ) <EOL> except : <EOL> return results <EOL> name = name . lower ( ) <EOL> for line in lines : <EOL> idx = line . find ( '<STR_LIT:#>' ) <EOL> if idx != - <NUM_LIT:1> : <EOL> line = line [ : idx ] <EOL> if not line : <EOL> continue <EOL> parts = line . split ( ) <EOL> if name . lower ( ) in [ s . lower ( ) for s in parts [ <NUM_LIT:1> : ] ] : <EOL> results . append ( parts [ <NUM_LIT:0> ] ) <EOL> return results <EOL> def searchFileFor ( file , name ) : <EOL> """<STR_LIT>""" <EOL> addresses = searchFileForAll ( FilePath ( file ) , name ) <EOL> if addresses : <EOL> return addresses [ <NUM_LIT:0> ] <EOL> return None <EOL> class Resolver ( common . ResolverBase , styles . Versioned ) : <EOL> """<STR_LIT>""" <EOL> persistenceVersion = <NUM_LIT:1> <EOL> def upgradeToVersion1 ( self ) : <EOL> self . typeToMethod = { } <EOL> for ( k , v ) in common . typeToMethod . items ( ) : <EOL> self . typeToMethod [ k ] = getattr ( self , v ) <EOL> def __init__ ( self , file = '<STR_LIT>' , ttl = <NUM_LIT> * <NUM_LIT> ) : <EOL> common . ResolverBase . __init__ ( self ) <EOL> self . file = file <EOL> self . ttl = ttl <EOL> def _aRecords ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return tuple ( [ <EOL> dns . RRHeader ( name , dns . A , dns . IN , self . ttl , <EOL> dns . Record_A ( addr , self . ttl ) ) <EOL> for addr <EOL> in searchFileForAll ( FilePath ( self . file ) , name ) <EOL> if isIPAddress ( addr ) ] ) <EOL> def _aaaaRecords ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return tuple ( [ <EOL> dns . RRHeader ( name , dns . AAAA , dns . IN , self . ttl , <EOL> dns . Record_AAAA ( addr , self . ttl ) ) <EOL> for addr <EOL> in searchFileForAll ( FilePath ( self . file ) , name ) <EOL> if not isIPAddress ( addr ) ] ) <EOL> def _respond ( self , name , records ) : <EOL> """<STR_LIT>""" <EOL> if records : <EOL> return defer . succeed ( ( records , ( ) , ( ) ) ) <EOL> return defer . fail ( failure . Failure ( dns . DomainError ( name ) ) ) <EOL> def lookupAddress ( self , name , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _respond ( name , self . _aRecords ( name ) ) <EOL> def lookupIPV6Address ( self , name , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _respond ( name , self . _aaaaRecords ( name ) ) <EOL> lookupAllRecords = lookupAddress </s>
