#NEWLINE# def _remove_indents_from_function( code ) : #NEWLINE# #INDENT# "Remove leading indents from the function's source code.\n\n    Otherwise an exec later when running the function would complain about\n    the indents.\n    " #NEWLINE# def swap_tabs_to_spaces( line ) : #NEWLINE# #INDENT# new_line = '' #NEWLINE# for i in xrange ( 0 , len ( line ) ) : #NEWLINE# #INDENT# if ( line [ i ] == ' ' ) : #NEWLINE# #INDENT# new_line += line [ i ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( line [ i ] == '\t' ) : #NEWLINE# #INDENT# new_line += ( ' ' * 8 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_line += line [ i : len ( line ) ] #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return new_line #NEWLINE# #UNINDENT# lines = code . split ( '\n' ) #NEWLINE# indent = ( - 1 ) #NEWLINE# for line in lines : #NEWLINE# #INDENT# m = re . match ( '^([ \t]*)def\\s.*$' , line ) #NEWLINE# if m : #NEWLINE# #INDENT# indent = len ( swap_tabs_to_spaces ( m . group ( 1 ) ) ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# if ( indent < 0 ) : #NEWLINE# #INDENT# raise Exception ( 'No def found for function source' ) #NEWLINE# #UNINDENT# result = '' #NEWLINE# for line in lines : #NEWLINE# #INDENT# line = swap_tabs_to_spaces ( line ) #NEWLINE# i = 0 #NEWLINE# while ( i < len ( line ) ) : #NEWLINE# #INDENT# if ( ( i < indent ) and ( line [ i ] == ' ' ) ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# result += ( line [ i : len ( line ) ] + '\n' ) #NEWLINE# #UNINDENT# return result