<s> from __future__ import absolute_import <EOL> import logging <EOL> import sys <EOL> import types <EOL> import threading <EOL> import inspect <EOL> from functools import wraps <EOL> from itertools import chain <EOL> from . import config <EOL> tls = threading . local ( ) <EOL> tls . tracing = False <EOL> tls . indent = <NUM_LIT:0> <EOL> def find_function_info ( func , spec , args ) : <EOL> """<STR_LIT>""" <EOL> module = getattr ( func , '<STR_LIT>' , None ) <EOL> name = getattr ( func , '<STR_LIT>' , None ) <EOL> self = getattr ( func , '<STR_LIT>' , None ) <EOL> cname = None <EOL> if self : <EOL> cname = self . __name__ <EOL> elif len ( spec . args ) and spec . args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> cname = args [ <NUM_LIT:0> ] . __class__ . __name__ <EOL> elif len ( spec . args ) and spec . args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> cname = args [ <NUM_LIT:0> ] . __name__ <EOL> if name : <EOL> qname = [ ] <EOL> if module and module != '<STR_LIT:__main__>' : <EOL> qname . append ( module ) <EOL> qname . append ( '<STR_LIT:.>' ) <EOL> if cname : <EOL> qname . append ( cname ) <EOL> qname . append ( '<STR_LIT:.>' ) <EOL> qname . append ( name ) <EOL> name = '<STR_LIT>' . join ( qname ) <EOL> return name , None <EOL> def chop ( value ) : <EOL> MAX_SIZE = <NUM_LIT> <EOL> s = repr ( value ) <EOL> if len ( s ) > MAX_SIZE : <EOL> return s [ : MAX_SIZE ] + '<STR_LIT>' + s [ - <NUM_LIT:1> ] <EOL> else : <EOL> return s <EOL> def create_events ( fname , spec , args , kwds ) : <EOL> values = dict ( ) <EOL> if spec . defaults : <EOL> values = dict ( zip ( spec . args [ - len ( spec . defaults ) : ] , spec . defaults ) ) <EOL> values . update ( kwds ) <EOL> values . update ( list ( zip ( spec . args [ : len ( args ) ] , args ) ) ) <EOL> positional = [ '<STR_LIT>' % ( a , values . pop ( a ) ) for a in spec . args ] <EOL> anonymous = [ str ( a ) for a in args [ len ( positional ) : ] ] <EOL> keywords = [ '<STR_LIT>' % ( k , values [ k ] ) for k in sorted ( values . keys ( ) ) ] <EOL> params = '<STR_LIT:U+002CU+0020>' . join ( [ f for f in chain ( positional , anonymous , keywords ) if f ] ) <EOL> enter = [ '<STR_LIT>' , tls . indent * '<STR_LIT:U+0020>' , fname , '<STR_LIT:(>' , params , '<STR_LIT:)>' ] <EOL> leave = [ '<STR_LIT>' , tls . indent * '<STR_LIT:U+0020>' , fname ] <EOL> return enter , leave <EOL> def dotrace ( * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> recursive = kwds . get ( '<STR_LIT>' , False ) <EOL> def decorator ( func ) : <EOL> spec = None <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def wrapper ( * args , ** kwds ) : <EOL> if not logger . isEnabledFor ( logging . INFO ) or tls . tracing : <EOL> return func ( * args , ** kwds ) <EOL> fname , ftype = find_function_info ( func , spec , args ) <EOL> try : <EOL> tls . tracing = True <EOL> enter , leave = create_events ( fname , spec , args , kwds ) <EOL> try : <EOL> logger . info ( '<STR_LIT>' . join ( enter ) ) <EOL> tls . indent += <NUM_LIT:1> <EOL> try : <EOL> try : <EOL> tls . tracing = False <EOL> result = func ( * args , ** kwds ) <EOL> finally : <EOL> tls . tracing = True <EOL> except : <EOL> type , value , traceback = sys . exc_info ( ) <EOL> leave . append ( '<STR_LIT>' ) <EOL> mname = type . __module__ <EOL> if mname != '<STR_LIT:__main__>' : <EOL> leave . append ( mname ) <EOL> leave . append ( '<STR_LIT:.>' ) <EOL> leave . append ( type . __name__ ) <EOL> if value . args : <EOL> leave . append ( '<STR_LIT:(>' ) <EOL> leave . append ( '<STR_LIT:U+002CU+0020>' . join ( chop ( v ) for v in value . args ) ) <EOL> leave . append ( '<STR_LIT:)>' ) <EOL> else : <EOL> leave . append ( '<STR_LIT>' ) <EOL> raise <EOL> else : <EOL> if result is not None : <EOL> leave . append ( '<STR_LIT>' ) <EOL> leave . append ( chop ( result ) ) <EOL> finally : <EOL> tls . indent -= <NUM_LIT:1> <EOL> logger . info ( '<STR_LIT>' . join ( leave ) ) <EOL> finally : <EOL> tls . tracing = False <EOL> return result <EOL> result = None <EOL> rewrap = lambda x : x <EOL> if type ( func ) == classmethod : <EOL> rewrap = type ( func ) <EOL> func = func . __get__ ( True ) . __func__ <EOL> elif type ( func ) == staticmethod : <EOL> rewrap = type ( func ) <EOL> func = func . __get__ ( True ) <EOL> elif type ( func ) == property : <EOL> raise NotImplementedError <EOL> spec = inspect . getargspec ( func ) <EOL> return rewrap ( wraps ( func ) ( wrapper ) ) <EOL> arg0 = len ( args ) and args [ <NUM_LIT:0> ] or None <EOL> if recursive : <EOL> raise NotImplementedError <EOL> if inspect . ismodule ( arg0 ) : <EOL> for n , f in inspect . getmembers ( arg0 , inspect . isfunction ) : <EOL> setattr ( arg0 , n , decorator ( f ) ) <EOL> for n , c in inspect . getmembers ( arg0 , inspect . isclass ) : <EOL> dotrace ( c , * args , recursive = recursive ) <EOL> elif inspect . isclass ( arg0 ) : <EOL> for n , f in inspect . getmembers ( arg0 , lambda x : ( inspect . isfunction ( x ) or <EOL> inspect . ismethod ( x ) ) ) : <EOL> setattr ( arg0 , n , decorator ( f ) ) <EOL> if callable ( arg0 ) or type ( arg0 ) in ( classmethod , staticmethod ) : <EOL> return decorator ( arg0 ) <EOL> elif type ( arg0 ) == property : <EOL> pget , pset , pdel = None , None , None <EOL> if arg0 . fget : <EOL> pget = decorator ( arg0 . fget ) <EOL> if arg0 . fset : <EOL> pset = decorator ( arg0 . fset ) <EOL> if arg0 . fdel : <EOL> pdel = decorator ( arg0 . fdel ) <EOL> return property ( pget , pset , pdel ) <EOL> else : <EOL> return decorator <EOL> def notrace ( * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> return func <EOL> arg0 = len ( args ) and args [ <NUM_LIT:0> ] or None <EOL> if callable ( arg0 ) or type ( arg0 ) in ( classmethod , staticmethod ) : <EOL> return decorator ( arg0 ) <EOL> else : <EOL> return decorator <EOL> def doevent ( msg ) : <EOL> msg = [ '<STR_LIT>' , tls . indent * '<STR_LIT:U+0020>' , msg ] <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' . join ( msg ) ) <EOL> def noevent ( msg ) : <EOL> pass <EOL> if config . TRACE : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . setLevel ( logging . INFO ) <EOL> logger . handlers = [ logging . StreamHandler ( ) ] <EOL> trace = dotrace <EOL> event = doevent <EOL> else : <EOL> trace = notrace <EOL> event = noevent </s>
