<s> import cStringIO <EOL> import struct <EOL> import dns . exception <EOL> import dns . inet <EOL> import dns . name <EOL> class IPSECKEY ( dns . rdata . Rdata ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:key>' ] <EOL> def __init__ ( self , rdclass , rdtype , precedence , gateway_type , algorithm , <EOL> gateway , key ) : <EOL> super ( IPSECKEY , self ) . __init__ ( rdclass , rdtype ) <EOL> if gateway_type == <NUM_LIT:0> : <EOL> if gateway != '<STR_LIT:.>' and not gateway is None : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> gateway = None <EOL> elif gateway_type == <NUM_LIT:1> : <EOL> junk = dns . inet . inet_pton ( dns . inet . AF_INET , gateway ) <EOL> elif gateway_type == <NUM_LIT:2> : <EOL> junk = dns . inet . inet_pton ( dns . inet . AF_INET6 , gateway ) <EOL> elif gateway_type == <NUM_LIT:3> : <EOL> pass <EOL> else : <EOL> raise SyntaxError ( '<STR_LIT>' % gateway_type ) <EOL> self . precedence = precedence <EOL> self . gateway_type = gateway_type <EOL> self . algorithm = algorithm <EOL> self . gateway = gateway <EOL> self . key = key <EOL> def to_text ( self , origin = None , relativize = True , ** kw ) : <EOL> if self . gateway_type == <NUM_LIT:0> : <EOL> gateway = '<STR_LIT:.>' <EOL> elif self . gateway_type == <NUM_LIT:1> : <EOL> gateway = self . gateway <EOL> elif self . gateway_type == <NUM_LIT:2> : <EOL> gateway = self . gateway <EOL> elif self . gateway_type == <NUM_LIT:3> : <EOL> gateway = str ( self . gateway . choose_relativity ( origin , relativize ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' % ( self . precedence , self . gateway_type , <EOL> self . algorithm , gateway , <EOL> dns . rdata . _base64ify ( self . key ) ) <EOL> def from_text ( cls , rdclass , rdtype , tok , origin = None , relativize = True ) : <EOL> precedence = tok . get_uint8 ( ) <EOL> gateway_type = tok . get_uint8 ( ) <EOL> algorithm = tok . get_uint8 ( ) <EOL> if gateway_type == <NUM_LIT:3> : <EOL> gateway = tok . get_name ( ) . choose_relativity ( origin , relativize ) <EOL> else : <EOL> gateway = tok . get_string ( ) <EOL> chunks = [ ] <EOL> while <NUM_LIT:1> : <EOL> t = tok . get ( ) . unescape ( ) <EOL> if t . is_eol_or_eof ( ) : <EOL> break <EOL> if not t . is_identifier ( ) : <EOL> raise dns . exception . SyntaxError <EOL> chunks . append ( t . value ) <EOL> b64 = '<STR_LIT>' . join ( chunks ) <EOL> key = b64 . decode ( '<STR_LIT>' ) <EOL> return cls ( rdclass , rdtype , precedence , gateway_type , algorithm , <EOL> gateway , key ) <EOL> from_text = classmethod ( from_text ) <EOL> def to_wire ( self , file , compress = None , origin = None ) : <EOL> header = struct . pack ( "<STR_LIT>" , self . precedence , self . gateway_type , <EOL> self . algorithm ) <EOL> file . write ( header ) <EOL> if self . gateway_type == <NUM_LIT:0> : <EOL> pass <EOL> elif self . gateway_type == <NUM_LIT:1> : <EOL> file . write ( dns . inet . inet_pton ( dns . inet . AF_INET , self . gateway ) ) <EOL> elif self . gateway_type == <NUM_LIT:2> : <EOL> file . write ( dns . inet . inet_pton ( dns . inet . AF_INET6 , self . gateway ) ) <EOL> elif self . gateway_type == <NUM_LIT:3> : <EOL> self . gateway . to_wire ( file , None , origin ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> file . write ( self . key ) <EOL> def from_wire ( cls , rdclass , rdtype , wire , current , rdlen , origin = None ) : <EOL> if rdlen < <NUM_LIT:3> : <EOL> raise dns . exception . FormError <EOL> header = struct . unpack ( '<STR_LIT>' , wire [ current : current + <NUM_LIT:3> ] ) <EOL> gateway_type = header [ <NUM_LIT:1> ] <EOL> current += <NUM_LIT:3> <EOL> rdlen -= <NUM_LIT:3> <EOL> if gateway_type == <NUM_LIT:0> : <EOL> gateway = None <EOL> elif gateway_type == <NUM_LIT:1> : <EOL> gateway = dns . inet . inet_ntop ( dns . inet . AF_INET , <EOL> wire [ current : current + <NUM_LIT:4> ] ) <EOL> current += <NUM_LIT:4> <EOL> rdlen -= <NUM_LIT:4> <EOL> elif gateway_type == <NUM_LIT:2> : <EOL> gateway = dns . inet . inet_ntop ( dns . inet . AF_INET6 , <EOL> wire [ current : current + <NUM_LIT:16> ] ) <EOL> current += <NUM_LIT:16> <EOL> rdlen -= <NUM_LIT:16> <EOL> elif gateway_type == <NUM_LIT:3> : <EOL> ( gateway , cused ) = dns . name . from_wire ( wire [ : current + rdlen ] , <EOL> current ) <EOL> current += cused <EOL> rdlen -= cused <EOL> else : <EOL> raise dns . exception . FormError ( '<STR_LIT>' ) <EOL> key = wire [ current : current + rdlen ] <EOL> return cls ( rdclass , rdtype , header [ <NUM_LIT:0> ] , gateway_type , header [ <NUM_LIT:2> ] , <EOL> gateway , key ) <EOL> from_wire = classmethod ( from_wire ) <EOL> def _cmp ( self , other ) : <EOL> f = cStringIO . StringIO ( ) <EOL> self . to_wire ( f ) <EOL> wire1 = f . getvalue ( ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> f . truncate ( ) <EOL> other . to_wire ( f ) <EOL> wire2 = f . getvalue ( ) <EOL> f . close ( ) <EOL> return cmp ( wire1 , wire2 ) </s>
