#NEWLINE# def rewrite_from_imports( fromlist , indentation , lineiter ) : #NEWLINE# #INDENT# parsed_items = [ ] #NEWLINE# for item in complete_fromlist ( fromlist , lineiter ) : #NEWLINE# #INDENT# item = item . strip ( ) . split ( ) #NEWLINE# if ( not item ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( len ( item ) == 1 ) : #NEWLINE# #INDENT# parsed_items . append ( ( item [ 0 ] , None ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( len ( item ) == 3 ) and ( item [ 1 ] == 'as' ) ) : #NEWLINE# #INDENT# parsed_items . append ( ( item [ 0 ] , item [ 2 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'invalid syntax for import' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# new_imports = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( item , alias ) in parsed_items : #NEWLINE# #INDENT# new_imports . setdefault ( find_module ( item ) , [ ] ) . append ( ( item , alias ) ) #NEWLINE# #UNINDENT# for ( module_name , items ) in sorted ( new_imports . items ( ) ) : #NEWLINE# #INDENT# fromlist_items = sorted ( [ ( '%s%s' % ( item , ( ( ( alias is not None ) and ( ' as ' + alias ) ) or '' ) ) ) for ( item , alias ) in items ] , reverse = True ) #NEWLINE# prefix = ( '%sfrom %s import ' % ( indentation , module_name ) ) #NEWLINE# item_buffer = [ ] #NEWLINE# while fromlist_items : #NEWLINE# #INDENT# item_buffer . append ( fromlist_items . pop ( ) ) #NEWLINE# fromlist = ', ' . join ( item_buffer ) #NEWLINE# if ( ( len ( fromlist ) + len ( prefix ) ) > 79 ) : #NEWLINE# #INDENT# ( yield ( ( prefix + ', ' . join ( item_buffer [ : ( - 1 ) ] ) ) + ', \\' ) ) #NEWLINE# item_buffer = [ item_buffer [ ( - 1 ) ] ] #NEWLINE# indentation = indentation . replace ( '>' , '.' ) #NEWLINE# prefix = ( indentation + '     ' ) #NEWLINE# #UNINDENT# #UNINDENT# ( yield ( prefix + ', ' . join ( item_buffer ) ) )