<s> """<STR_LIT>""" <EOL> from . prog import Program <EOL> from . bytecode import BytecodeObject <EOL> from . visitors import MethodVisitor <EOL> from . utils . log import logger <EOL> class Instrumentation ( object ) : <EOL> """<STR_LIT>""" <EOL> KNOWN_OPTIONS = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , location = None ) : <EOL> self . options = { } <EOL> self . _location = location <EOL> self . program = None <EOL> self . apply_ran = False <EOL> self . wrapping_code = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } <EOL> def set_option ( self , key , value = True ) : <EOL> """<STR_LIT>""" <EOL> if key not in Instrumentation . KNOWN_OPTIONS : <EOL> raise Exception ( '<STR_LIT>' % key ) <EOL> self . options [ key ] = value <EOL> def get_option ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key not in Instrumentation . KNOWN_OPTIONS : <EOL> raise Exception ( '<STR_LIT>' % key ) <EOL> return self . options . get ( key , None ) <EOL> @ property <EOL> def location ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _location <EOL> @ location . setter <EOL> def location ( self , value ) : <EOL> self . _location = value <EOL> def prepare_program ( self ) : <EOL> """<STR_LIT>""" <EOL> self . program = Program ( self ) <EOL> return self . program is not None <EOL> def apply ( self , visitor , rewrite = False ) : <EOL> """<STR_LIT>""" <EOL> self . apply_ran = True <EOL> bytecode_files = self . program . bytecode_files <EOL> for bc_file in bytecode_files : <EOL> self . instrument ( visitor , bc_file , rewrite ) <EOL> def instrument ( self , visitor , bytecode_file , rewrite = False ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" , bytecode_file ) <EOL> code = BytecodeObject ( bytecode_file ) <EOL> code . accept ( visitor ) <EOL> if rewrite : <EOL> if self . wrapping_code [ '<STR_LIT>' ] : <EOL> code . add_enter_code ( * self . wrapping_code [ '<STR_LIT>' ] ) <EOL> if self . wrapping_code [ '<STR_LIT>' ] : <EOL> code . add_exit_code ( * self . wrapping_code [ '<STR_LIT>' ] ) <EOL> if code . has_changes : <EOL> code . write ( ) <EOL> def on_enter ( self , python_code , import_code = None ) : <EOL> """<STR_LIT>""" <EOL> if self . apply_ran : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . wrapping_code [ '<STR_LIT>' ] = ( python_code , import_code ) <EOL> def on_exit ( self , python_code , import_code = None ) : <EOL> """<STR_LIT>""" <EOL> if self . apply_ran : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . wrapping_code [ '<STR_LIT>' ] = ( python_code , import_code ) <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> class SimpleMethodVisitor ( MethodVisitor ) : <EOL> def __init__ ( self ) : <EOL> MethodVisitor . __init__ ( self ) <EOL> def visit ( self , methDecl ) : <EOL> logger . debug ( "<STR_LIT>" , methDecl ) <EOL> logger . debug ( "<STR_LIT>" , methDecl . code_object ) <EOL> logger . debug ( get_debug_code_object_info ( methDecl . code_object ) ) <EOL> bc = BytecodeObject . get_parsed_code ( methDecl . code_object ) <EOL> logger . debug ( "<STR_LIT>" , show_bytecode ( bc ) ) <EOL> logger . debug ( "<STR_LIT>" ) <EOL> for bytecode_file in self . program . bytecode_files : <EOL> code = BytecodeObject ( bytecode_file ) <EOL> code . parse ( ) <EOL> main_module = code . get_module ( ) <EOL> if not main_module : <EOL> logger . error ( "<STR_LIT>" , bytecode_file ) <EOL> continue <EOL> logger . debug ( "<STR_LIT>" , BytecodeObject . build_tree ( main_module ) ) <EOL> logger . debug ( "<STR_LIT>" , show_bytecode ( main_module . bytecode ) ) <EOL> simple_visitor = SimpleMethodVisitor ( ) <EOL> code . accept ( simple_visitor ) </s>
