<s> '''<STR_LIT>''' <EOL> import json <EOL> import collections <EOL> from radon . raw import analyze <EOL> from radon . metrics import mi_visit , mi_rank <EOL> from radon . complexity import ( cc_visit , sorted_results , cc_rank , <EOL> add_inner_blocks ) <EOL> from radon . cli . colors import RANKS_COLORS , MI_RANKS , RESET <EOL> from radon . cli . tools import ( iter_filenames , _open , cc_to_dict , dict_to_xml , <EOL> dict_to_codeclimate_issues , cc_to_terminal , <EOL> raw_to_dict ) <EOL> class Harvester ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , paths , config ) : <EOL> '''<STR_LIT>''' <EOL> self . paths = paths <EOL> self . config = config <EOL> self . _results = [ ] <EOL> def _iter_filenames ( self ) : <EOL> '''<STR_LIT>''' <EOL> return iter_filenames ( self . paths , self . config . exclude , <EOL> self . config . ignore ) <EOL> def gobble ( self , fobj ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> def run ( self ) : <EOL> '''<STR_LIT>''' <EOL> for name in self . _iter_filenames ( ) : <EOL> with _open ( name ) as fobj : <EOL> try : <EOL> yield ( name , self . gobble ( fobj ) ) <EOL> except Exception as e : <EOL> yield ( name , { '<STR_LIT:error>' : str ( e ) } ) <EOL> @ property <EOL> def results ( self ) : <EOL> '''<STR_LIT>''' <EOL> def caching_iterator ( it , r ) : <EOL> '''<STR_LIT>''' <EOL> for t in it : <EOL> yield t <EOL> r . append ( t ) <EOL> if self . _results : <EOL> return self . _results <EOL> return caching_iterator ( self . run ( ) , self . _results ) <EOL> def as_json ( self ) : <EOL> '''<STR_LIT>''' <EOL> return json . dumps ( dict ( self . results ) ) <EOL> def as_xml ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> def as_codeclimate_issues ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> def to_terminal ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError <EOL> class CCHarvester ( Harvester ) : <EOL> '''<STR_LIT>''' <EOL> def gobble ( self , fobj ) : <EOL> '''<STR_LIT>''' <EOL> r = cc_visit ( fobj . read ( ) , no_assert = self . config . no_assert ) <EOL> if self . config . show_closures : <EOL> r = add_inner_blocks ( r ) <EOL> return sorted_results ( r , order = self . config . order ) <EOL> def _to_dicts ( self ) : <EOL> '''<STR_LIT>''' <EOL> result = { } <EOL> for key , data in self . results : <EOL> if '<STR_LIT:error>' in data : <EOL> result [ key ] = data <EOL> continue <EOL> values = [ v for v in map ( cc_to_dict , data ) <EOL> if self . config . min <= v [ '<STR_LIT>' ] <= self . config . max ] <EOL> if values : <EOL> result [ key ] = values <EOL> return result <EOL> def as_json ( self ) : <EOL> '''<STR_LIT>''' <EOL> return json . dumps ( self . _to_dicts ( ) ) <EOL> def as_xml ( self ) : <EOL> '''<STR_LIT>''' <EOL> return dict_to_xml ( self . _to_dicts ( ) ) <EOL> def as_codeclimate_issues ( self ) : <EOL> '''<STR_LIT>''' <EOL> return dict_to_codeclimate_issues ( self . _to_dicts ( ) , self . config . min ) <EOL> def to_terminal ( self ) : <EOL> '''<STR_LIT>''' <EOL> average_cc = <NUM_LIT> <EOL> analyzed = <NUM_LIT:0> <EOL> for name , blocks in self . results : <EOL> if '<STR_LIT:error>' in blocks : <EOL> yield name , ( blocks [ '<STR_LIT:error>' ] , ) , { '<STR_LIT:error>' : True } <EOL> continue <EOL> res , cc , n = cc_to_terminal ( blocks , self . config . show_complexity , <EOL> self . config . min , self . config . max , <EOL> self . config . total_average ) <EOL> average_cc += cc <EOL> analyzed += n <EOL> if res : <EOL> yield name , ( ) , { } <EOL> yield res , ( ) , { '<STR_LIT>' : <NUM_LIT:1> } <EOL> if ( self . config . average or self . config . total_average ) and analyzed : <EOL> cc = average_cc / analyzed <EOL> ranked_cc = cc_rank ( cc ) <EOL> yield ( '<STR_LIT>' , <EOL> ( analyzed , ) , { } ) <EOL> yield ( '<STR_LIT>' , <EOL> ( RANKS_COLORS [ ranked_cc ] , ranked_cc , cc , RESET ) , { } ) <EOL> class RawHarvester ( Harvester ) : <EOL> '''<STR_LIT>''' <EOL> headers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def gobble ( self , fobj ) : <EOL> '''<STR_LIT>''' <EOL> return raw_to_dict ( analyze ( fobj . read ( ) ) ) <EOL> def as_xml ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def to_terminal ( self ) : <EOL> '''<STR_LIT>''' <EOL> sum_metrics = collections . defaultdict ( int ) <EOL> for path , mod in self . results : <EOL> if '<STR_LIT:error>' in mod : <EOL> yield path , ( mod [ '<STR_LIT:error>' ] , ) , { '<STR_LIT:error>' : True } <EOL> continue <EOL> yield path , ( ) , { } <EOL> for header in self . headers : <EOL> value = mod [ header . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ] <EOL> yield '<STR_LIT>' , ( header , value ) , { '<STR_LIT>' : <NUM_LIT:1> } <EOL> sum_metrics [ header ] += value <EOL> loc , comments = mod [ '<STR_LIT>' ] , mod [ '<STR_LIT>' ] <EOL> yield '<STR_LIT>' , ( ) , { '<STR_LIT>' : <NUM_LIT:1> } <EOL> yield ( '<STR_LIT>' , ( comments / ( float ( loc ) or <NUM_LIT:1> ) , ) , <EOL> { '<STR_LIT>' : <NUM_LIT:2> } ) <EOL> yield ( '<STR_LIT>' , ( comments / ( float ( mod [ '<STR_LIT>' ] ) or <NUM_LIT:1> ) , ) , <EOL> { '<STR_LIT>' : <NUM_LIT:2> } ) <EOL> yield ( '<STR_LIT>' , <EOL> ( ( comments + mod [ '<STR_LIT>' ] ) / ( float ( loc ) or <NUM_LIT:1> ) , ) , <EOL> { '<STR_LIT>' : <NUM_LIT:2> } ) <EOL> if self . config . summary : <EOL> yield '<STR_LIT>' , ( ) , { } <EOL> for header in self . headers : <EOL> yield '<STR_LIT>' , ( header , sum_metrics [ header ] ) , { '<STR_LIT>' : <NUM_LIT:1> } <EOL> class MIHarvester ( Harvester ) : <EOL> '''<STR_LIT>''' <EOL> def gobble ( self , fobj ) : <EOL> '''<STR_LIT>''' <EOL> mi = mi_visit ( fobj . read ( ) , self . config . multi ) <EOL> rank = mi_rank ( mi ) <EOL> return { '<STR_LIT>' : mi , '<STR_LIT>' : rank } <EOL> @ property <EOL> def filtered_results ( self ) : <EOL> '''<STR_LIT>''' <EOL> for key , value in self . results : <EOL> if ( '<STR_LIT:error>' in value or <EOL> self . config . min <= value [ '<STR_LIT>' ] <= self . config . max ) : <EOL> yield ( key , value ) <EOL> def as_json ( self ) : <EOL> '''<STR_LIT>''' <EOL> return json . dumps ( dict ( self . filtered_results ) ) <EOL> def as_xml ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def to_terminal ( self ) : <EOL> '''<STR_LIT>''' <EOL> for name , mi in self . filtered_results : <EOL> if '<STR_LIT:error>' in mi : <EOL> yield name , ( mi [ '<STR_LIT:error>' ] , ) , { '<STR_LIT:error>' : True } <EOL> continue <EOL> rank = mi [ '<STR_LIT>' ] <EOL> color = MI_RANKS [ rank ] <EOL> to_show = '<STR_LIT>' <EOL> if self . config . show : <EOL> to_show = '<STR_LIT>' . format ( mi [ '<STR_LIT>' ] ) <EOL> yield '<STR_LIT>' , ( name , color , rank , to_show , RESET ) , { } </s>
