#NEWLINE# def bufconvert( fmt , nums ) : #NEWLINE# #INDENT# buf = io . BytesIO ( ) #NEWLINE# bufstack = [ ] #NEWLINE# fmt = str ( fmt ) #NEWLINE# while ( len ( fmt ) > 0 ) : #NEWLINE# #INDENT# ( step , fmt ) = ( fmt [ 0 ] , fmt [ 1 : ] ) #NEWLINE# if ( step == '{' ) : #NEWLINE# #INDENT# bufstack . append ( buf ) #NEWLINE# buf = io . BytesIO ( ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( step == '}' ) : #NEWLINE# #INDENT# bufstack [ ( - 1 ) ] . write ( buf . getvalue ( ) ) #NEWLINE# buf = bufstack . pop ( ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( nums == ':' ) : #NEWLINE# #INDENT# ( step , fmt ) = ( fmt [ 0 ] , fmt [ 1 : ] ) #NEWLINE# buf = buftransform ( buf , step ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( step == '%' ) : #NEWLINE# #INDENT# ( step , fmt ) = ( fmt [ 0 : 2 ] , fmt [ 2 : ] ) #NEWLINE# buf . write ( chr ( int ( step , 16 ) ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( step in ( "'" , '"' ) ) : #NEWLINE# #INDENT# ( step , fmt ) = fmt . split ( step , 1 ) #NEWLINE# buf . write ( step . encode ( 'UTF-8' ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# step = out_ops [ step ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# raise KeyError ( 'Unsupported output format' , step ) #NEWLINE# #UNINDENT# step ( buf , nums . pop ( 0 ) ) #NEWLINE# #UNINDENT# assert ( len ( bufstack ) == 0 ) , 'Too many buffers on stack' #NEWLINE# return buf