<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division , unicode_literals <EOL> from docopt import docopt <EOL> import json <EOL> import os <EOL> import random <EOL> import sys <EOL> import traceback <EOL> import jedi <EOL> class SourceFinder ( object ) : <EOL> _files = None <EOL> @ staticmethod <EOL> def fetch ( file_path ) : <EOL> if not os . path . isdir ( file_path ) : <EOL> yield file_path <EOL> return <EOL> for root , dirnames , filenames in os . walk ( file_path ) : <EOL> for name in filenames : <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> yield os . path . join ( root , name ) <EOL> @ classmethod <EOL> def files ( cls , file_path ) : <EOL> if cls . _files is None : <EOL> cls . _files = list ( cls . fetch ( file_path ) ) <EOL> return cls . _files <EOL> class TestCase ( object ) : <EOL> def __init__ ( self , operation , path , line , column , traceback = None ) : <EOL> if operation not in self . operations : <EOL> raise ValueError ( "<STR_LIT>" % operation ) <EOL> self . operation = operation <EOL> self . path = path <EOL> self . line = line <EOL> self . column = column <EOL> self . traceback = traceback <EOL> @ classmethod <EOL> def from_cache ( cls , record ) : <EOL> with open ( record ) as f : <EOL> args = json . load ( f ) <EOL> return cls ( * args ) <EOL> operations = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> @ classmethod <EOL> def generate ( cls , file_path ) : <EOL> operation = random . choice ( cls . operations ) <EOL> path = random . choice ( SourceFinder . files ( file_path ) ) <EOL> with open ( path ) as f : <EOL> source = f . read ( ) <EOL> lines = source . splitlines ( ) <EOL> if not lines : <EOL> lines = [ '<STR_LIT>' ] <EOL> line = random . randint ( <NUM_LIT:1> , len ( lines ) ) <EOL> column = random . randint ( <NUM_LIT:0> , len ( lines [ line - <NUM_LIT:1> ] ) ) <EOL> return cls ( operation , path , line , column ) <EOL> def run ( self , debugger , record = None , print_result = False ) : <EOL> try : <EOL> with open ( self . path ) as f : <EOL> self . script = jedi . Script ( f . read ( ) , self . line , self . column , self . path ) <EOL> self . objects = getattr ( self . script , self . operation ) ( ) <EOL> if print_result : <EOL> print ( "<STR_LIT>" . format ( ** self . __dict__ ) ) <EOL> self . show_location ( self . line , self . column ) <EOL> self . show_operation ( ) <EOL> except jedi . NotFoundError : <EOL> pass <EOL> except Exception : <EOL> self . traceback = traceback . format_exc ( ) <EOL> if record is not None : <EOL> call_args = ( self . operation , self . path , self . line , self . column , self . traceback ) <EOL> with open ( record , '<STR_LIT:w>' ) as f : <EOL> json . dump ( call_args , f ) <EOL> self . show_errors ( ) <EOL> if debugger : <EOL> einfo = sys . exc_info ( ) <EOL> pdb = __import__ ( debugger ) <EOL> if debugger == '<STR_LIT>' : <EOL> pdb . post_mortem ( einfo [ <NUM_LIT:2> ] , einfo [ <NUM_LIT:0> ] , einfo [ <NUM_LIT:1> ] ) <EOL> else : <EOL> pdb . post_mortem ( einfo [ <NUM_LIT:2> ] ) <EOL> exit ( <NUM_LIT:1> ) <EOL> def show_location ( self , lineno , column , show = <NUM_LIT:3> ) : <EOL> lower = lineno - show if lineno - show > <NUM_LIT:0> else <NUM_LIT:0> <EOL> prefix = '<STR_LIT>' <EOL> for i , line in enumerate ( self . script . source . split ( '<STR_LIT:\n>' ) [ lower : lineno ] ) : <EOL> print ( prefix , lower + i + <NUM_LIT:1> , line ) <EOL> print ( prefix , '<STR_LIT:U+0020>' , '<STR_LIT:U+0020>' * ( column + len ( str ( lineno ) ) ) , '<STR_LIT>' ) <EOL> def show_operation ( self ) : <EOL> print ( "<STR_LIT>" % self . operation . capitalize ( ) ) <EOL> if self . operation == '<STR_LIT>' : <EOL> self . show_completions ( ) <EOL> else : <EOL> self . show_definitions ( ) <EOL> def show_completions ( self ) : <EOL> for completion in self . objects : <EOL> print ( completion . name ) <EOL> def show_definitions ( self ) : <EOL> for completion in self . objects : <EOL> print ( completion . desc_with_module ) <EOL> if completion . module_path is None : <EOL> continue <EOL> if os . path . abspath ( completion . module_path ) == os . path . abspath ( self . path ) : <EOL> self . show_location ( completion . line , completion . column ) <EOL> def show_errors ( self ) : <EOL> print ( self . traceback ) <EOL> print ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( ** self . __dict__ ) ) <EOL> def main ( arguments ) : <EOL> debugger = '<STR_LIT>' if arguments [ '<STR_LIT>' ] else '<STR_LIT>' if arguments [ '<STR_LIT>' ] else '<STR_LIT>' if arguments [ '<STR_LIT>' ] else None <EOL> record = arguments [ '<STR_LIT>' ] <EOL> jedi . settings . use_filesystem_cache = arguments [ '<STR_LIT>' ] <EOL> if arguments [ '<STR_LIT>' ] : <EOL> jedi . set_debug_function ( ) <EOL> if arguments [ '<STR_LIT>' ] or arguments [ '<STR_LIT>' ] : <EOL> t = TestCase . from_cache ( record ) <EOL> if arguments [ '<STR_LIT>' ] : <EOL> t . show_errors ( ) <EOL> else : <EOL> t . run ( debugger ) <EOL> elif arguments [ '<STR_LIT>' ] : <EOL> TestCase ( <EOL> arguments [ '<STR_LIT>' ] , arguments [ '<STR_LIT>' ] , <EOL> int ( arguments [ '<STR_LIT>' ] ) , int ( arguments [ '<STR_LIT>' ] ) <EOL> ) . run ( debugger , print_result = True ) <EOL> else : <EOL> for _ in range ( int ( arguments [ '<STR_LIT>' ] ) ) : <EOL> t = TestCase . generate ( arguments [ '<STR_LIT>' ] or '<STR_LIT:.>' ) <EOL> if arguments [ '<STR_LIT>' ] : <EOL> print ( '<STR_LIT>' % ( t . operation , t . path , t . line , t . column ) ) <EOL> sys . stdout . flush ( ) <EOL> else : <EOL> print ( '<STR_LIT:.>' , end = '<STR_LIT>' ) <EOL> t . run ( debugger , record ) <EOL> sys . stdout . flush ( ) <EOL> print ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> arguments = docopt ( __doc__ ) <EOL> main ( arguments ) </s>
