#NEWLINE# def generate( specPath ) : #NEWLINE# #INDENT# spec = amqp_codegen . AmqpSpec ( specPath ) #NEWLINE# def genSingleDecode( prefix , cLvalue , unresolved_domain ) : #NEWLINE# #INDENT# type = spec . resolveDomain ( unresolved_domain ) #NEWLINE# if ( type == 'shortstr' ) : #NEWLINE# #INDENT# print ( ( prefix + ( '%s, offset = data.decode_short_string(encoded, offset)' % cLvalue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'longstr' ) : #NEWLINE# #INDENT# print ( ( prefix + "length = struct.unpack_from('>I', encoded, offset)[0]" ) ) #NEWLINE# print ( ( prefix + 'offset += 4' ) ) #NEWLINE# print ( ( prefix + ( '%s = encoded[offset:offset + length]' % cLvalue ) ) ) #NEWLINE# print ( ( prefix + 'try:' ) ) #NEWLINE# print ( ( prefix + ( '    %s = str(%s)' % ( cLvalue , cLvalue ) ) ) ) #NEWLINE# print ( ( prefix + 'except UnicodeEncodeError:' ) ) #NEWLINE# print ( ( prefix + '    pass' ) ) #NEWLINE# print ( ( prefix + 'offset += length' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'octet' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "%s = struct.unpack_from('B', encoded, offset)[0]" % cLvalue ) ) ) #NEWLINE# print ( ( prefix + 'offset += 1' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'short' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "%s = struct.unpack_from('>H', encoded, offset)[0]" % cLvalue ) ) ) #NEWLINE# print ( ( prefix + 'offset += 2' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'long' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "%s = struct.unpack_from('>I', encoded, offset)[0]" % cLvalue ) ) ) #NEWLINE# print ( ( prefix + 'offset += 4' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'longlong' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "%s = struct.unpack_from('>Q', encoded, offset)[0]" % cLvalue ) ) ) #NEWLINE# print ( ( prefix + 'offset += 8' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'timestamp' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "%s = struct.unpack_from('>Q', encoded, offset)[0]" % cLvalue ) ) ) #NEWLINE# print ( ( prefix + 'offset += 8' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'bit' ) : #NEWLINE# #INDENT# raise Exception ( "Can't decode bit in genSingleDecode" ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'table' ) : #NEWLINE# #INDENT# print ( Exception ( ( prefix + ( '(%s, offset) = data.decode_table(encoded, offset)' % cLvalue ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'Illegal domain in genSingleDecode' , type ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# def genSingleEncode( prefix , cValue , unresolved_domain ) : #NEWLINE# #INDENT# type = spec . resolveDomain ( unresolved_domain ) #NEWLINE# if ( type == 'shortstr' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "assert isinstance(%s, str_or_bytes),\\\n%s       'A non-string value was supplied for %s'" % ( cValue , prefix , cValue ) ) ) ) #NEWLINE# print ( ( prefix + ( 'data.encode_short_string(pieces, %s)' % cValue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'longstr' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "assert isinstance(%s, str_or_bytes),\\\n%s       'A non-string value was supplied for %s'" % ( cValue , prefix , cValue ) ) ) ) #NEWLINE# print ( ( prefix + ( "value = %s.encode('utf-8') if isinstance(%s, unicode_type) else %s" % ( cValue , cValue , cValue ) ) ) ) #NEWLINE# print ( ( prefix + "pieces.append(struct.pack('>I', len(value)))" ) ) #NEWLINE# print ( ( prefix + 'pieces.append(value)' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'octet' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "pieces.append(struct.pack('B', %s))" % cValue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'short' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "pieces.append(struct.pack('>H', %s))" % cValue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'long' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "pieces.append(struct.pack('>I', %s))" % cValue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'longlong' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "pieces.append(struct.pack('>Q', %s))" % cValue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'timestamp' ) : #NEWLINE# #INDENT# print ( ( prefix + ( "pieces.append(struct.pack('>Q', %s))" % cValue ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'bit' ) : #NEWLINE# #INDENT# raise Exception ( "Can't encode bit in genSingleEncode" ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'table' ) : #NEWLINE# #INDENT# print ( Exception ( ( prefix + ( 'data.encode_table(pieces, %s)' % cValue ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'Illegal domain in genSingleEncode' , type ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# def genDecodeMethodFields( m ) : #NEWLINE# #INDENT# print ( '        def decode(self, encoded, offset=0):' ) #NEWLINE# bitindex = None #NEWLINE# for f in m . arguments : #NEWLINE# #INDENT# if ( spec . resolveDomain ( f . domain ) == 'bit' ) : #NEWLINE# #INDENT# if ( bitindex is None ) : #NEWLINE# #INDENT# bitindex = 0 #NEWLINE# #UNINDENT# if ( bitindex >= 8 ) : #NEWLINE# #INDENT# bitindex = 0 #NEWLINE# #UNINDENT# if ( not bitindex ) : #NEWLINE# #INDENT# print ( "            bit_buffer = struct.unpack_from('B', encoded, offset)[0]" ) #NEWLINE# print ( '            offset += 1' ) #NEWLINE# #UNINDENT# print ( ( '            self.%s = (bit_buffer & (1 << %d)) != 0' % ( pyize ( f . name ) , bitindex ) ) ) #NEWLINE# bitindex += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# bitindex = None #NEWLINE# genSingleDecode ( '            ' , ( 'self.%s' % ( pyize ( f . name ) , ) ) , f . domain ) #NEWLINE# #UNINDENT# #UNINDENT# print ( '            return self' ) #NEWLINE# print ( '' ) #NEWLINE# #UNINDENT# def genDecodeProperties( c ) : #NEWLINE# #INDENT# print ( '    def decode(self, encoded, offset=0):' ) #NEWLINE# print ( '        flags = 0' ) #NEWLINE# print ( '        flagword_index = 0' ) #NEWLINE# print ( '        while True:' ) #NEWLINE# print ( "            partial_flags = struct.unpack_from('>H', encoded, offset)[0]" ) #NEWLINE# print ( '            offset += 2' ) #NEWLINE# print ( '            flags = flags | (partial_flags << (flagword_index * 16))' ) #NEWLINE# print ( '            if not (partial_flags & 1):' ) #NEWLINE# print ( '                break' ) #NEWLINE# print ( '            flagword_index += 1' ) #NEWLINE# for f in c . fields : #NEWLINE# #INDENT# if ( spec . resolveDomain ( f . domain ) == 'bit' ) : #NEWLINE# #INDENT# print ( ( '        self.%s = (flags & %s) != 0' % ( pyize ( f . name ) , flagName ( c , f ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( ( '        if flags & %s:' % ( flagName ( c , f ) , ) ) ) #NEWLINE# genSingleDecode ( '            ' , ( 'self.%s' % ( pyize ( f . name ) , ) ) , f . domain ) #NEWLINE# print ( '        else:' ) #NEWLINE# print ( ( '            self.%s = None' % ( pyize ( f . name ) , ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# print ( '        return self' ) #NEWLINE# print ( '' ) #NEWLINE# #UNINDENT# def genEncodeMethodFields( m ) : #NEWLINE# #INDENT# print ( '        def encode(self):' ) #NEWLINE# print ( '            pieces = list()' ) #NEWLINE# bitindex = None #NEWLINE# def finishBits( ) : #NEWLINE# #INDENT# if ( bitindex is not None ) : #NEWLINE# #INDENT# print ( "            pieces.append(struct.pack('B', bit_buffer))" ) #NEWLINE# #UNINDENT# #UNINDENT# for f in m . arguments : #NEWLINE# #INDENT# if ( spec . resolveDomain ( f . domain ) == 'bit' ) : #NEWLINE# #INDENT# if ( bitindex is None ) : #NEWLINE# #INDENT# bitindex = 0 #NEWLINE# print ( '            bit_buffer = 0' ) #NEWLINE# #UNINDENT# if ( bitindex >= 8 ) : #NEWLINE# #INDENT# finishBits ( ) #NEWLINE# print ( '            bit_buffer = 0' ) #NEWLINE# bitindex = 0 #NEWLINE# #UNINDENT# print ( ( '            if self.%s:' % pyize ( f . name ) ) ) #NEWLINE# print ( ( '                bit_buffer = bit_buffer | (1 << %d)' % bitindex ) ) #NEWLINE# bitindex += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# finishBits ( ) #NEWLINE# bitindex = None #NEWLINE# genSingleEncode ( '            ' , ( 'self.%s' % ( pyize ( f . name ) , ) ) , f . domain ) #NEWLINE# #UNINDENT# #UNINDENT# finishBits ( ) #NEWLINE# print ( '            return pieces' ) #NEWLINE# print ( '' ) #NEWLINE# #UNINDENT# def genEncodeProperties( c ) : #NEWLINE# #INDENT# print ( '    def encode(self):' ) #NEWLINE# print ( '        pieces = list()' ) #NEWLINE# print ( '        flags = 0' ) #NEWLINE# for f in c . fields : #NEWLINE# #INDENT# if ( spec . resolveDomain ( f . domain ) == 'bit' ) : #NEWLINE# #INDENT# print ( ( '        if self.%s: flags = flags | %s' % ( pyize ( f . name ) , flagName ( c , f ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( ( '        if self.%s is not None:' % ( pyize ( f . name ) , ) ) ) #NEWLINE# print ( ( '            flags = flags | %s' % ( flagName ( c , f ) , ) ) ) #NEWLINE# genSingleEncode ( '            ' , ( 'self.%s' % ( pyize ( f . name ) , ) ) , f . domain ) #NEWLINE# #UNINDENT# #UNINDENT# print ( '        flag_pieces = list()' ) #NEWLINE# print ( '        while True:' ) #NEWLINE# print ( '            remainder = flags >> 16' ) #NEWLINE# print ( '            partial_flags = flags & 0xFFFE' ) #NEWLINE# print ( '            if remainder != 0:' ) #NEWLINE# print ( '                partial_flags |= 1' ) #NEWLINE# print ( "            flag_pieces.append(struct.pack('>H', partial_flags))" ) #NEWLINE# print ( '            flags = remainder' ) #NEWLINE# print ( '            if not flags:' ) #NEWLINE# print ( '                break' ) #NEWLINE# print ( '        return flag_pieces + pieces' ) #NEWLINE# print ( '' ) #NEWLINE# #UNINDENT# def fieldDeclList( fields ) : #NEWLINE# #INDENT# return '' . join ( [ ( ', %s=%s' % ( pyize ( f . name ) , fieldvalue ( f . defaultvalue ) ) ) for f in fields ] ) #NEWLINE# #UNINDENT# def fieldInitList( prefix , fields ) : #NEWLINE# #INDENT# if fields : #NEWLINE# #INDENT# return '' . join ( [ ( '%sself.%s = %s\n' % ( prefix , pyize ( f . name ) , pyize ( f . name ) ) ) for f in fields ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( '%spass\n' % ( prefix , ) ) #NEWLINE# #UNINDENT# #UNINDENT# print ( '"""\nAMQP Specification\n==================\nThis module implements the constants and classes that comprise AMQP protocol\nlevel constructs. It should rarely be directly referenced outside of Pika\'s\nown internal use.\n\n.. note:: Auto-generated code by codegen.py, do not edit directly. Pull\nrequests to this file without accompanying ``utils/codegen.py`` changes will be\nrejected.\n\n"""\n\nimport struct\nfrom pika import amqp_object\nfrom pika import data\nfrom pika.compat import str_or_bytes, unicode_type\n\n# Python 3 support for str object\nstr = bytes\n' ) #NEWLINE# print ( ( 'PROTOCOL_VERSION = (%d, %d, %d)' % ( spec . major , spec . minor , spec . revision ) ) ) #NEWLINE# print ( ( 'PORT = %d' % spec . port ) ) #NEWLINE# print ( '' ) #NEWLINE# spec . constants . append ( ( 'FRAME_MAX_SIZE' , 131072 , '' ) ) #NEWLINE# spec . constants . append ( ( 'FRAME_HEADER_SIZE' , 7 , '' ) ) #NEWLINE# spec . constants . append ( ( 'FRAME_END_SIZE' , 1 , '' ) ) #NEWLINE# constants = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( c , v , cls ) in spec . constants : #NEWLINE# #INDENT# constants [ constantName ( c ) ] = v #NEWLINE# #UNINDENT# for key in sorted ( constants . keys ( ) ) : #NEWLINE# #INDENT# print ( ( '%s = %s' % ( key , constants [ key ] ) ) ) #NEWLINE# #UNINDENT# print ( '' ) #NEWLINE# for c in spec . allClasses ( ) : #NEWLINE# #INDENT# print ( '' ) #NEWLINE# print ( ( 'class %s(amqp_object.Class):' % ( camel ( c . name ) , ) ) ) #NEWLINE# print ( '' ) #NEWLINE# print ( ( '    INDEX = 0x%.04X  # %d' % ( c . index , c . index ) ) ) #NEWLINE# print ( ( '    NAME = %s' % ( fieldvalue ( camel ( c . name ) ) , ) ) ) #NEWLINE# print ( '' ) #NEWLINE# for m in c . allMethods ( ) : #NEWLINE# #INDENT# print ( ( '    class %s(amqp_object.Method):' % ( camel ( m . name ) , ) ) ) #NEWLINE# print ( '' ) #NEWLINE# methodid = ( ( m . klass . index << 16 ) | m . index ) #NEWLINE# print ( ( '        INDEX = 0x%.08X  # %d, %d; %d' % ( methodid , m . klass . index , m . index , methodid ) ) ) #NEWLINE# print ( ( '        NAME = %s' % fieldvalue ( m . structName ( ) ) ) ) #NEWLINE# print ( '' ) #NEWLINE# print ( ( '        def __init__(self%s):' % ( fieldDeclList ( m . arguments ) , ) ) ) #NEWLINE# print ( fieldInitList ( '            ' , m . arguments ) ) #NEWLINE# print ( '        @property' ) #NEWLINE# print ( '        def synchronous(self):' ) #NEWLINE# print ( ( '            return %s' % m . isSynchronous ) ) #NEWLINE# print ( '' ) #NEWLINE# genDecodeMethodFields ( m ) #NEWLINE# genEncodeMethodFields ( m ) #NEWLINE# #UNINDENT# #UNINDENT# for c in spec . allClasses ( ) : #NEWLINE# #INDENT# if c . fields : #NEWLINE# #INDENT# print ( '' ) #NEWLINE# print ( ( 'class %s(amqp_object.Properties):' % ( c . structName ( ) , ) ) ) #NEWLINE# print ( '' ) #NEWLINE# print ( ( '    CLASS = %s' % ( camel ( c . name ) , ) ) ) #NEWLINE# print ( ( '    INDEX = 0x%.04X  # %d' % ( c . index , c . index ) ) ) #NEWLINE# print ( ( '    NAME = %s' % fieldvalue ( c . structName ( ) ) ) ) #NEWLINE# print ( '' ) #NEWLINE# index = 0 #NEWLINE# if c . fields : #NEWLINE# #INDENT# for f in c . fields : #NEWLINE# #INDENT# if ( ( index % 16 ) == 15 ) : #NEWLINE# #INDENT# index += 1 #NEWLINE# #UNINDENT# shortnum = ( index / 16 ) #NEWLINE# partialindex = ( 15 - ( index % 16 ) ) #NEWLINE# bitindex = ( ( shortnum * 16 ) + partialindex ) #NEWLINE# print ( ( '    %s = (1 << %d)' % ( flagName ( None , f ) , bitindex ) ) ) #NEWLINE# index += 1 #NEWLINE# #UNINDENT# print ( '' ) #NEWLINE# #UNINDENT# print ( ( '    def __init__(self%s):' % ( fieldDeclList ( c . fields ) , ) ) ) #NEWLINE# print ( fieldInitList ( '        ' , c . fields ) ) #NEWLINE# genDecodeProperties ( c ) #NEWLINE# genEncodeProperties ( c ) #NEWLINE# #UNINDENT# #UNINDENT# print ( 'methods = {' ) #NEWLINE# print ( ',\n' . join ( [ ( '    0x%08X: %s' % ( ( ( m . klass . index << 16 ) | m . index ) , m . structName ( ) ) ) for m in spec . allMethods ( ) ] ) ) #NEWLINE# print ( '}' ) #NEWLINE# print ( '' ) #NEWLINE# print ( 'props = {' ) #NEWLINE# print ( ',\n' . join ( [ ( '    0x%04X: %s' % ( c . index , c . structName ( ) ) ) for c in spec . allClasses ( ) if c . fields ] ) ) #NEWLINE# print ( '}' ) #NEWLINE# print ( '' ) #NEWLINE# print ( '' ) #NEWLINE# print ( 'def has_content(methodNumber):' ) #NEWLINE# print ( '    return methodNumber in (' ) #NEWLINE# for m in spec . allMethods ( ) : #NEWLINE# #INDENT# if m . hasContent : #NEWLINE# #INDENT# print ( ( '        %s.INDEX,' % m . structName ( ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# print ( '    )' )