<s> from __future__ import absolute_import <EOL> import struct <EOL> import logging <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> logger = logging . getLogger ( __name__ ) <EOL> header_struct = struct . Struct ( '<STR_LIT>' ) <EOL> begin_request_struct = struct . Struct ( '<STR_LIT>' ) <EOL> end_request_struct = struct . Struct ( '<STR_LIT>' ) <EOL> unknown_type_struct = struct . Struct ( '<STR_LIT>' ) <EOL> for name in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> packer = globals ( ) . get ( '<STR_LIT>' . format ( name ) ) <EOL> for prefix , attr in ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) : <EOL> full_name = prefix + name <EOL> globals ( ) [ full_name ] = getattr ( packer , attr ) <EOL> __all__ . append ( full_name ) <EOL> def pack_pairs ( pairs ) : <EOL> if isinstance ( pairs , dict ) : <EOL> pairs = pairs . iteritems ( ) <EOL> return '<STR_LIT>' . join ( pack_pair ( name , value ) for name , value in pairs ) <EOL> try : <EOL> from . speedups import pack_pair , unpack_pairs <EOL> logger . debug ( '<STR_LIT>' ) <EOL> except ImportError : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> length_struct = struct . Struct ( '<STR_LIT>' ) <EOL> def pack_len ( s ) : <EOL> l = len ( s ) <EOL> if l < <NUM_LIT> : <EOL> return chr ( l ) <EOL> elif l > <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> <NUM_LIT> ) ) <EOL> return length_struct . pack ( l | <NUM_LIT> ) <EOL> def pack_pair ( name , value ) : <EOL> return '<STR_LIT>' . join ( ( pack_len ( name ) , pack_len ( value ) , name , value ) ) <EOL> def unpack_len ( buf , pos ) : <EOL> _len = ord ( buf [ pos ] ) <EOL> if _len & <NUM_LIT> : <EOL> _len = length_struct . unpack_from ( buf , pos ) [ <NUM_LIT:0> ] & <NUM_LIT> <EOL> pos += <NUM_LIT:4> <EOL> else : <EOL> pos += <NUM_LIT:1> <EOL> return _len , pos <EOL> def unpack_pairs ( data ) : <EOL> end = len ( data ) <EOL> pos = <NUM_LIT:0> <EOL> while pos < end : <EOL> try : <EOL> name_len , pos = unpack_len ( data , pos ) <EOL> value_len , pos = unpack_len ( data , pos ) <EOL> except ( IndexError , struct . error ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if end - pos < name_len + value_len : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> name_len + value_len - ( end - pos ) ) ) <EOL> name = data [ pos : pos + name_len ] <EOL> pos += name_len <EOL> value = data [ pos : pos + value_len ] <EOL> pos += value_len <EOL> yield name , value </s>
