<s> from functools import partial <EOL> import inspect <EOL> import logging <EOL> import sys <EOL> import re <EOL> from silk . utils import six <EOL> from silk . profiling . profiler import silk_profile <EOL> Logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def _get_module ( module_name ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:.>' in module_name : <EOL> splt = module_name . split ( '<STR_LIT:.>' ) <EOL> imp = '<STR_LIT:.>' . join ( splt [ : - <NUM_LIT:1> ] ) <EOL> frm = splt [ - <NUM_LIT:1> ] <EOL> module = __import__ ( imp , globals ( ) , locals ( ) , [ frm ] , <NUM_LIT:0> ) <EOL> module = getattr ( module , frm ) <EOL> else : <EOL> module = __import__ ( module_name , globals ( ) , locals ( ) , [ ] , <NUM_LIT:0> ) <EOL> return module <EOL> def _get_func ( module , func_name ) : <EOL> """<STR_LIT>""" <EOL> cls_name = None <EOL> cls = None <EOL> if '<STR_LIT:.>' in func_name : <EOL> cls_name , func_name = func_name . split ( '<STR_LIT:.>' ) <EOL> if cls_name : <EOL> cls = getattr ( module , cls_name ) <EOL> func = getattr ( cls , func_name ) <EOL> else : <EOL> func = getattr ( module , func_name ) <EOL> return cls , func <EOL> def profile_function_or_method ( module , func , name = None ) : <EOL> """<STR_LIT>""" <EOL> if type ( module ) is str or type ( module ) is unicode : <EOL> module = _get_module ( module ) <EOL> decorator = silk_profile ( name , _dynamic = True ) <EOL> func_name = func <EOL> cls , func = _get_func ( module , func_name ) <EOL> wrapped_target = decorator ( func ) <EOL> if cls : <EOL> setattr ( cls , func_name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] , wrapped_target ) <EOL> else : <EOL> setattr ( module , func_name , wrapped_target ) <EOL> def _get_parent_module ( module ) : <EOL> parent = sys . modules <EOL> splt = module . __name__ . split ( '<STR_LIT:.>' ) <EOL> if len ( splt ) > <NUM_LIT:1> : <EOL> for module_name in splt [ : - <NUM_LIT:1> ] : <EOL> try : <EOL> parent = getattr ( parent , module_name ) <EOL> except AttributeError : <EOL> parent = parent [ module_name ] <EOL> return parent <EOL> def _get_context_manager_source ( end_line , file_path , name , start_line ) : <EOL> inject_code = "<STR_LIT>" % name <EOL> code = '<STR_LIT>' <EOL> with open ( file_path , '<STR_LIT:r>' ) as f : <EOL> ws = '<STR_LIT>' <EOL> for i , line in enumerate ( f ) : <EOL> if i == start_line : <EOL> x = re . search ( r"<STR_LIT>" , line ) <EOL> try : <EOL> ws = x . groups ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> ws = '<STR_LIT>' <EOL> code += ws + inject_code <EOL> code += ws + '<STR_LIT:U+0020>' + line <EOL> elif start_line < i <= end_line : <EOL> code += ws + '<STR_LIT:U+0020>' + line <EOL> else : <EOL> code += line <EOL> return code <EOL> def _get_ws ( txt ) : <EOL> """<STR_LIT>""" <EOL> m = re . search ( r"<STR_LIT>" , txt ) <EOL> try : <EOL> fws = m . groups ( ) [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> fws = '<STR_LIT>' <EOL> return fws <EOL> def _get_source_lines ( func ) : <EOL> source = inspect . getsourcelines ( func ) [ <NUM_LIT:0> ] <EOL> fws = _get_ws ( source [ <NUM_LIT:0> ] ) <EOL> for i in range ( <NUM_LIT:0> , len ( source ) ) : <EOL> source [ i ] = source [ i ] . replace ( fws , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> return source <EOL> def _new_func_from_source ( source , func ) : <EOL> """<STR_LIT>""" <EOL> src_str = '<STR_LIT>' . join ( source ) <EOL> frames = inspect . getouterframes ( inspect . currentframe ( ) ) <EOL> calling_frame = frames [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> context = { } <EOL> globals = six . get_function_globals ( func ) <EOL> locals = calling_frame . f_locals <EOL> combined = globals . copy ( ) <EOL> combined . update ( locals ) <EOL> Logger . debug ( '<STR_LIT>' % src_str ) <EOL> six . exec_ ( src_str , combined , context ) <EOL> new_func = context [ func . __name__ ] <EOL> return new_func <EOL> def _inject_context_manager_func ( func , start_line , end_line , name ) : <EOL> """<STR_LIT>""" <EOL> source = _get_source_lines ( func ) <EOL> start_line += <NUM_LIT:1> <EOL> end_line += <NUM_LIT:1> <EOL> ws = _get_ws ( source [ start_line ] ) <EOL> for i in range ( start_line , end_line ) : <EOL> try : <EOL> source [ i ] = '<STR_LIT:U+0020>' + source [ i ] <EOL> except IndexError : <EOL> raise IndexError ( '<STR_LIT>' % ( func . __name__ , i ) ) <EOL> source . insert ( start_line , ws + "<STR_LIT>" ) <EOL> source . insert ( start_line + <NUM_LIT:1> , ws + "<STR_LIT>" % name ) <EOL> return _new_func_from_source ( source , func ) <EOL> def is_str_typ ( o ) : <EOL> return any ( map ( partial ( isinstance , o ) , six . string_types ) ) or isinstance ( o , six . text_type ) <EOL> def inject_context_manager_func ( module , func , start_line , end_line , name ) : <EOL> if is_str_typ ( module ) : <EOL> module = _get_module ( module ) <EOL> cls = None <EOL> if is_str_typ ( func ) : <EOL> func_name = func <EOL> cls , func = _get_func ( module , func_name ) <EOL> else : <EOL> func_name = func . __name__ <EOL> new_func = _inject_context_manager_func ( func , start_line , end_line , name ) <EOL> if cls : <EOL> setattr ( cls , func_name , new_func ) <EOL> else : <EOL> setattr ( module , func_name , new_func ) </s>
