<s> import os <EOL> import sys <EOL> import re <EOL> import ast <EOL> from optparse import OptionParser <EOL> class PythonTestFinder ( object ) : <EOL> def find_functions ( self , ast_body , matcher ) : <EOL> for obj in ast_body : <EOL> if not matcher ( obj ) : <EOL> continue <EOL> if isinstance ( obj , ast . FunctionDef ) : <EOL> yield obj . name <EOL> if isinstance ( obj , ast . ClassDef ) : <EOL> for func in self . find_functions ( obj . body , matcher ) : <EOL> yield '<STR_LIT>' % ( obj . name , func ) <EOL> def get_module_tests ( self , module ) : <EOL> with open ( module ) as f : <EOL> data = f . read ( ) <EOL> result = ast . parse ( data ) <EOL> def matcher ( obj ) : <EOL> if isinstance ( obj , ast . FunctionDef ) : <EOL> return re . search ( '<STR_LIT:test>' , obj . name , re . IGNORECASE ) <EOL> return isinstance ( obj , ast . ClassDef ) <EOL> tests = list ( <EOL> self . find_functions ( result . body , matcher ) <EOL> ) <EOL> return tests <EOL> class NoseTestFinder ( object ) : <EOL> def _generate_tests ( self , suite ) : <EOL> from nose . suite import ContextSuite <EOL> from nose . case import Test <EOL> for context in suite . _tests : <EOL> if isinstance ( context , Test ) : <EOL> yield context <EOL> continue <EOL> assert isinstance ( context , ContextSuite ) <EOL> for test in self . _generate_tests ( context ) : <EOL> yield test <EOL> def _get_test_name ( self , test_wrapper ) : <EOL> from nose . case import FunctionTestCase <EOL> test = test_wrapper . test <EOL> if isinstance ( test , FunctionTestCase ) : <EOL> return test . test . __name__ <EOL> return test . __class__ . __name__ + '<STR_LIT:.>' + test . _testMethodName <EOL> def _generate_test_names ( self , suite ) : <EOL> return map ( self . _get_test_name , self . _generate_tests ( suite ) ) <EOL> def get_module_tests ( self , module ) : <EOL> import nose <EOL> loader = nose . loader . defaultTestLoader ( ) <EOL> return self . _generate_test_names ( loader . loadTestsFromName ( module ) ) <EOL> def _get_prefixed ( strings , prefix ) : <EOL> for string in strings : <EOL> if string . startswith ( prefix ) : <EOL> yield string . replace ( prefix , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def _get_py_or_dirs ( directory , prefix ) : <EOL> for entry in os . listdir ( directory or '<STR_LIT:.>' ) : <EOL> path = os . path . join ( directory , entry ) <EOL> if entry . startswith ( prefix ) : <EOL> leftover = entry . replace ( prefix , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if os . path . isdir ( path ) : <EOL> yield leftover + '<STR_LIT:/>' <EOL> elif leftover . endswith ( '<STR_LIT>' ) : <EOL> yield leftover + '<STR_LIT::>' <EOL> def _complete ( test_finder , thing ) : <EOL> if '<STR_LIT::>' in thing : <EOL> module , test_part = thing . split ( '<STR_LIT::>' ) <EOL> tests = list ( test_finder . get_module_tests ( module ) ) <EOL> if '<STR_LIT:.>' in test_part : <EOL> return _get_prefixed ( strings = tests , prefix = test_part ) <EOL> funcs = [ test for test in tests if test . count ( '<STR_LIT:.>' ) == <NUM_LIT:0> ] <EOL> classes = [ test . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] for test in tests if '<STR_LIT:.>' in test ] <EOL> if test_part in classes : <EOL> return [ '<STR_LIT:.>' ] <EOL> return _get_prefixed ( strings = funcs + classes , prefix = test_part ) <EOL> if os . path . isdir ( thing ) : <EOL> if thing != '<STR_LIT:.>' and not thing . endswith ( '<STR_LIT:/>' ) : <EOL> return [ '<STR_LIT:/>' ] <EOL> return _get_py_or_dirs ( thing , '<STR_LIT>' ) <EOL> if os . path . exists ( thing ) : <EOL> return [ '<STR_LIT::>' ] <EOL> directory , file_part = os . path . split ( thing ) <EOL> return _get_py_or_dirs ( directory , file_part ) <EOL> def complete ( test_finder , thing ) : <EOL> for option in set ( _complete ( test_finder , thing ) ) : <EOL> sys . stdout . write ( thing + option + '<STR_LIT:U+0020>' ) <EOL> def main ( ) : <EOL> methods = { <EOL> '<STR_LIT>' : NoseTestFinder , <EOL> '<STR_LIT>' : PythonTestFinder , <EOL> } <EOL> parser = OptionParser ( usage = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> choices = list ( methods . keys ( ) ) , <EOL> default = '<STR_LIT>' , <EOL> ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> finder_class = methods [ options . search_method ] <EOL> finder_instance = finder_class ( ) <EOL> complete ( finder_instance , '<STR_LIT>' if len ( args ) == <NUM_LIT:0> else args [ <NUM_LIT:0> ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
