<s> """<STR_LIT>""" <EOL> import compiler <EOL> from compiler . visitor import ASTVisitor , walk <EOL> from cStringIO import StringIO <EOL> import os <EOL> import shlex <EOL> import sys <EOL> import grin <EOL> __version__ = '<STR_LIT>' <EOL> def normalize_From ( node ) : <EOL> """<STR_LIT>""" <EOL> statements = [ ] <EOL> children = node . getChildren ( ) <EOL> module = '<STR_LIT:.>' * node . level + node . modname <EOL> for name , asname in children [ <NUM_LIT:1> ] : <EOL> line = '<STR_LIT>' % ( module , name ) <EOL> if asname is not None : <EOL> line += '<STR_LIT>' % asname <EOL> line += '<STR_LIT:\n>' <EOL> statements . append ( line ) <EOL> return statements <EOL> def normalize_Import ( node ) : <EOL> """<STR_LIT>""" <EOL> statements = [ ] <EOL> children = node . getChildren ( ) <EOL> for name , asname in children [ <NUM_LIT:0> ] : <EOL> line = '<STR_LIT>' % ( name ) <EOL> if asname is not None : <EOL> line += '<STR_LIT>' % asname <EOL> line += '<STR_LIT:\n>' <EOL> statements . append ( line ) <EOL> return statements <EOL> class ImportPuller ( ASTVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> ASTVisitor . __init__ ( self ) <EOL> self . statements = [ ] <EOL> def visitFrom ( self , node ) : <EOL> self . statements . extend ( normalize_From ( node ) ) <EOL> def visitImport ( self , node ) : <EOL> self . statements . extend ( normalize_Import ( node ) ) <EOL> def as_string ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( self . statements ) <EOL> def normalize_file ( filename , * args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ast = compiler . parseFile ( filename ) <EOL> except Exception , e : <EOL> return StringIO ( '<STR_LIT>' ) <EOL> ip = ImportPuller ( ) <EOL> walk ( ast , ip ) <EOL> return StringIO ( ip . as_string ( ) ) <EOL> def get_grinimports_arg_parser ( parser = None ) : <EOL> """<STR_LIT>""" <EOL> parser = grin . get_grin_arg_parser ( parser ) <EOL> parser . set_defaults ( include = '<STR_LIT>' ) <EOL> parser . description = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . epilog = """<STR_LIT>""" <EOL> for action in parser . _actions : <EOL> if hasattr ( action , '<STR_LIT:version>' ) : <EOL> action . version = '<STR_LIT>' % __version__ <EOL> return parser <EOL> def grinimports_main ( argv = None ) : <EOL> if argv is None : <EOL> env_args = shlex . split ( os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> argv = [ sys . argv [ <NUM_LIT:0> ] ] + env_args + sys . argv [ <NUM_LIT:1> : ] <EOL> parser = get_grinimports_arg_parser ( ) <EOL> args = parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> if args . context is not None : <EOL> args . before_context = args . context <EOL> args . after_context = args . context <EOL> args . use_color = args . force_color or ( not args . no_color and <EOL> sys . stdout . isatty ( ) and <EOL> ( os . environ . get ( '<STR_LIT>' ) != '<STR_LIT>' ) ) <EOL> regex = grin . get_regex ( args ) <EOL> g = grin . GrepText ( regex , args ) <EOL> for filename , kind in grin . get_filenames ( args ) : <EOL> if kind == '<STR_LIT:text>' : <EOL> report = g . grep_a_file ( filename , opener = normalize_file ) <EOL> sys . stdout . write ( report ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> grinimports_main ( ) </s>
