<s> '''<STR_LIT>''' <EOL> from collections import deque <EOL> from haigha . writer import Writer <EOL> from haigha . reader import Reader <EOL> from haigha . frames . frame import Frame <EOL> class HeaderFrame ( Frame ) : <EOL> '''<STR_LIT>''' <EOL> PROPERTIES = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:15> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> Reader . read_table , Writer . write_table , <NUM_LIT:1> << <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_octet , <EOL> Writer . write_octet , <NUM_LIT:1> << <NUM_LIT:12> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_octet , Writer . write_octet , <NUM_LIT:1> << <NUM_LIT:11> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:10> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:9> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:8> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:7> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_timestamp , <EOL> Writer . write_timestamp , <NUM_LIT:1> << <NUM_LIT:6> ) , <EOL> ( '<STR_LIT:type>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:5> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:4> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , Reader . read_shortstr , <EOL> Writer . write_shortstr , <NUM_LIT:1> << <NUM_LIT:2> ) <EOL> ] <EOL> DEFAULT_PROPERTIES = True <EOL> @ classmethod <EOL> def type ( cls ) : <EOL> return <NUM_LIT:2> <EOL> @ property <EOL> def class_id ( self ) : <EOL> return self . _class_id <EOL> @ property <EOL> def weight ( self ) : <EOL> return self . _weight <EOL> @ property <EOL> def size ( self ) : <EOL> return self . _size <EOL> @ property <EOL> def properties ( self ) : <EOL> return self . _properties <EOL> @ classmethod <EOL> def parse ( self , channel_id , payload ) : <EOL> '''<STR_LIT>''' <EOL> class_id = payload . read_short ( ) <EOL> weight = payload . read_short ( ) <EOL> size = payload . read_longlong ( ) <EOL> properties = { } <EOL> if self . DEFAULT_PROPERTIES : <EOL> flag_bits = payload . read_short ( ) <EOL> for key , proptype , rfunc , wfunc , mask in self . PROPERTIES : <EOL> if flag_bits & mask : <EOL> properties [ key ] = rfunc ( payload ) <EOL> else : <EOL> flags = [ ] <EOL> while True : <EOL> flag_bits = payload . read_short ( ) <EOL> flags . append ( flag_bits ) <EOL> if flag_bits & <NUM_LIT:1> == <NUM_LIT:0> : <EOL> break <EOL> shift = <NUM_LIT:0> <EOL> for key , proptype , rfunc , wfunc , mask in self . PROPERTIES : <EOL> if shift == <NUM_LIT:0> : <EOL> if not flags : <EOL> break <EOL> flag_bits , flags = flags [ <NUM_LIT:0> ] , flags [ <NUM_LIT:1> : ] <EOL> shift = <NUM_LIT:15> <EOL> if flag_bits & ( <NUM_LIT:1> << shift ) : <EOL> properties [ key ] = rfunc ( payload ) <EOL> shift -= <NUM_LIT:1> <EOL> return HeaderFrame ( channel_id , class_id , weight , size , properties ) <EOL> def __init__ ( self , channel_id , class_id , weight , size , properties = { } ) : <EOL> Frame . __init__ ( self , channel_id ) <EOL> self . _class_id = class_id <EOL> self . _weight = weight <EOL> self . _size = size <EOL> self . _properties = properties <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , self . channel_id , self . _class_id , <EOL> self . _weight , self . _size , self . _properties ) <EOL> def write_frame ( self , buf ) : <EOL> '''<STR_LIT>''' <EOL> writer = Writer ( buf ) <EOL> writer . write_octet ( self . type ( ) ) <EOL> writer . write_short ( self . channel_id ) <EOL> stream_args_len_pos = len ( buf ) <EOL> writer . write_long ( <NUM_LIT:0> ) <EOL> stream_method_pos = len ( buf ) <EOL> writer . write_short ( self . _class_id ) <EOL> writer . write_short ( self . _weight ) <EOL> writer . write_longlong ( self . _size ) <EOL> if self . DEFAULT_PROPERTIES : <EOL> flags_pos = len ( buf ) <EOL> writer . write_short ( <NUM_LIT:0> ) <EOL> flag_bits = <NUM_LIT:0> <EOL> for key , proptype , rfunc , wfunc , mask in self . PROPERTIES : <EOL> val = self . _properties . get ( key , None ) <EOL> if val is not None : <EOL> flag_bits |= mask <EOL> wfunc ( writer , val ) <EOL> writer . write_short_at ( flag_bits , flags_pos ) <EOL> else : <EOL> shift = <NUM_LIT:15> <EOL> flag_bits = <NUM_LIT:0> <EOL> flags = [ ] <EOL> stack = deque ( ) <EOL> for key , proptype , rfunc , wfunc , mask in self . PROPERTIES : <EOL> val = self . _properties . get ( key , None ) <EOL> if val is not None : <EOL> if shift == <NUM_LIT:0> : <EOL> flags . append ( flag_bits ) <EOL> flag_bits = <NUM_LIT:0> <EOL> shift = <NUM_LIT:15> <EOL> flag_bits |= ( <NUM_LIT:1> << shift ) <EOL> stack . append ( ( wfunc , val ) ) <EOL> shift -= <NUM_LIT:1> <EOL> flags . append ( flag_bits ) <EOL> for flag_bits in flags : <EOL> writer . write_short ( flag_bits ) <EOL> for method , val in stack : <EOL> method ( writer , val ) <EOL> stream_len = len ( buf ) - stream_method_pos <EOL> writer . write_long_at ( stream_len , stream_args_len_pos ) <EOL> writer . write_octet ( <NUM_LIT> ) <EOL> HeaderFrame . register ( ) </s>
