#NEWLINE# def get_tagged_imports( fp ) : #NEWLINE# #INDENT# imports = [ ] #NEWLINE# inside_def = False #NEWLINE# def_lines = [ ] #NEWLINE# def_indent = 0 #NEWLINE# with open ( fp , 'r' ) as f : #NEWLINE# #INDENT# lastLine = f . readline ( ) #NEWLINE# for line in f : #NEWLINE# #INDENT# tagged_class_or_def = re . findall ( classdef_regex , lastLine ) #NEWLINE# tagged_line = re . findall ( tagged_line_regex , lastLine ) #NEWLINE# if ( tagged_class_or_def or inside_def ) : #NEWLINE# #INDENT# if ( tagged_class_or_def and def_lines ) : #NEWLINE# #INDENT# imports . append ( '' . join ( def_lines ) ) #NEWLINE# def_lines = [ ] #NEWLINE# inside_def = False #NEWLINE# #UNINDENT# if inside_def : #NEWLINE# #INDENT# indent = ( len ( lastLine ) - len ( lastLine . lstrip ( ' ' ) ) ) #NEWLINE# if ( ( indent == def_indent ) and ( lastLine != '\n' ) ) : #NEWLINE# #INDENT# imports . append ( '' . join ( def_lines ) ) #NEWLINE# def_lines = [ ] #NEWLINE# inside_def = False #NEWLINE# def_indent = 0 #NEWLINE# if tagged_line : #NEWLINE# #INDENT# imports . append ( lastLine ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( lastLine != '\n' ) : #NEWLINE# #INDENT# def_lines . append ( lastLine ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# inside_def = True #NEWLINE# def_indent = ( len ( lastLine ) - len ( lastLine . lstrip ( ' ' ) ) ) #NEWLINE# def_lines . append ( lastLine ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if tagged_line : #NEWLINE# #INDENT# imports . append ( lastLine ) #NEWLINE# #UNINDENT# #UNINDENT# lastLine = line #NEWLINE# #UNINDENT# #UNINDENT# tagged_line = re . findall ( tagged_line_regex , lastLine ) #NEWLINE# if inside_def : #NEWLINE# #INDENT# def_lines . append ( line ) #NEWLINE# imports . append ( '' . join ( def_lines ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if tagged_line : #NEWLINE# #INDENT# imports . append ( line ) #NEWLINE# #UNINDENT# #UNINDENT# src = ( '\n' . join ( imports ) + '\n' ) #NEWLINE# return src