<s> import _ast <EOL> import ast <EOL> import contextlib <EOL> import logging <EOL> import os . path <EOL> import sys <EOL> try : <EOL> import configparser <EOL> except ImportError : <EOL> import ConfigParser as configparser <EOL> logger = logging . getLogger ( __name__ ) <EOL> """<STR_LIT>""" <EOL> @ contextlib . contextmanager <EOL> def output_file ( filename , filemode ) : <EOL> try : <EOL> out = sys . stdout <EOL> if filename is not None : <EOL> if os . path . isdir ( filename ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> out = open ( filename , filemode ) <EOL> yield out <EOL> except Exception : <EOL> raise <EOL> finally : <EOL> if out is not sys . stdout : <EOL> out . close ( ) <EOL> def _get_attr_qual_name ( node , aliases ) : <EOL> '''<STR_LIT>''' <EOL> if type ( node ) == _ast . Name : <EOL> if node . id in aliases : <EOL> return aliases [ node . id ] <EOL> return node . id <EOL> elif type ( node ) == _ast . Attribute : <EOL> name = '<STR_LIT>' % ( _get_attr_qual_name ( node . value , aliases ) , node . attr ) <EOL> if name in aliases : <EOL> return aliases [ name ] <EOL> return name <EOL> else : <EOL> return "<STR_LIT>" <EOL> def get_call_name ( node , aliases ) : <EOL> if type ( node . func ) == _ast . Name : <EOL> if deepgetattr ( node , '<STR_LIT>' ) in aliases : <EOL> return aliases [ deepgetattr ( node , '<STR_LIT>' ) ] <EOL> return ( deepgetattr ( node , '<STR_LIT>' ) ) <EOL> elif type ( node . func ) == _ast . Attribute : <EOL> return _get_attr_qual_name ( node . func , aliases ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def get_func_name ( node ) : <EOL> return node . name <EOL> def get_qual_attr ( node , aliases ) : <EOL> prefix = "<STR_LIT>" <EOL> if type ( node ) == _ast . Attribute : <EOL> try : <EOL> val = deepgetattr ( node , '<STR_LIT>' ) <EOL> if val in aliases : <EOL> prefix = aliases [ val ] <EOL> else : <EOL> prefix = deepgetattr ( node , '<STR_LIT>' ) <EOL> except Exception : <EOL> pass <EOL> return ( "<STR_LIT>" % ( prefix , node . attr ) ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def deepgetattr ( obj , attr ) : <EOL> """<STR_LIT>""" <EOL> for key in attr . split ( '<STR_LIT:.>' ) : <EOL> obj = getattr ( obj , key ) <EOL> return obj <EOL> class InvalidModulePath ( Exception ) : <EOL> pass <EOL> class ConfigError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , config_file ) : <EOL> self . config_file = config_file <EOL> self . message = "<STR_LIT>" . format ( config_file , message ) <EOL> super ( ConfigError , self ) . __init__ ( self . message ) <EOL> class ProfileNotFound ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config_file , profile ) : <EOL> self . config_file = config_file <EOL> self . profile = profile <EOL> message = '<STR_LIT>' % ( <EOL> self . profile , self . config_file ) <EOL> super ( ProfileNotFound , self ) . __init__ ( message ) <EOL> def warnings_formatter ( message , <EOL> category = UserWarning , <EOL> filename = '<STR_LIT>' , <EOL> lineno = - <NUM_LIT:1> , <EOL> line = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> return "<STR_LIT>" . format ( message ) <EOL> def get_module_qualname_from_path ( path ) : <EOL> '''<STR_LIT>''' <EOL> ( head , tail ) = os . path . split ( path ) <EOL> if head == '<STR_LIT>' or tail == '<STR_LIT>' : <EOL> raise InvalidModulePath ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( path ) ) <EOL> qname = [ os . path . splitext ( tail ) [ <NUM_LIT:0> ] ] <EOL> while head not in [ '<STR_LIT:/>' , '<STR_LIT:.>' ] : <EOL> if os . path . isfile ( os . path . join ( head , '<STR_LIT>' ) ) : <EOL> ( head , tail ) = os . path . split ( head ) <EOL> qname . insert ( <NUM_LIT:0> , tail ) <EOL> else : <EOL> break <EOL> qualname = '<STR_LIT:.>' . join ( qname ) <EOL> return qualname <EOL> def namespace_path_join ( base , name ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' % ( base , name ) <EOL> def namespace_path_split ( path ) : <EOL> '''<STR_LIT>''' <EOL> return tuple ( path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) ) <EOL> def escaped_bytes_representation ( b ) : <EOL> '''<STR_LIT>''' <EOL> return b . decode ( '<STR_LIT>' ) . encode ( '<STR_LIT>' ) <EOL> def linerange ( node ) : <EOL> """<STR_LIT>""" <EOL> strip = { "<STR_LIT:body>" : None , "<STR_LIT>" : None , <EOL> "<STR_LIT>" : None , "<STR_LIT>" : None } <EOL> for key in strip . keys ( ) : <EOL> if hasattr ( node , key ) : <EOL> strip [ key ] = getattr ( node , key ) <EOL> setattr ( node , key , [ ] ) <EOL> lines_min = <NUM_LIT> <EOL> lines_max = - <NUM_LIT:1> <EOL> for n in ast . walk ( node ) : <EOL> if hasattr ( n , '<STR_LIT>' ) : <EOL> lines_min = min ( lines_min , n . lineno ) <EOL> lines_max = max ( lines_max , n . lineno ) <EOL> for key in strip . keys ( ) : <EOL> if strip [ key ] is not None : <EOL> setattr ( node , key , strip [ key ] ) <EOL> if lines_max > - <NUM_LIT:1> : <EOL> return list ( range ( lines_min , lines_max + <NUM_LIT:1> ) ) <EOL> return [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> def linerange_fix ( node ) : <EOL> """<STR_LIT>""" <EOL> lines = linerange ( node ) <EOL> if hasattr ( node , '<STR_LIT>' ) and hasattr ( node . sibling , '<STR_LIT>' ) : <EOL> start = min ( lines ) <EOL> delta = node . sibling . lineno - start <EOL> if delta > <NUM_LIT:1> : <EOL> return list ( range ( start , node . sibling . lineno ) ) <EOL> return lines <EOL> def concat_string ( node , stop = None ) : <EOL> '''<STR_LIT>''' <EOL> def _get ( node , bits , stop = None ) : <EOL> if node != stop : <EOL> bits . append ( <EOL> _get ( node . left , bits , stop ) <EOL> if isinstance ( node . left , ast . BinOp ) <EOL> else node . left ) <EOL> bits . append ( <EOL> _get ( node . right , bits , stop ) <EOL> if isinstance ( node . right , ast . BinOp ) <EOL> else node . right ) <EOL> bits = [ node ] <EOL> while isinstance ( node . parent , ast . BinOp ) : <EOL> node = node . parent <EOL> if isinstance ( node , ast . BinOp ) : <EOL> _get ( node , bits , stop ) <EOL> return ( node , "<STR_LIT:U+0020>" . join ( [ x . s for x in bits if isinstance ( x , ast . Str ) ] ) ) <EOL> def get_called_name ( node ) : <EOL> '''<STR_LIT>''' <EOL> func = node . func <EOL> try : <EOL> return ( func . attr if isinstance ( func , ast . Attribute ) else func . id ) <EOL> except AttributeError : <EOL> return "<STR_LIT>" <EOL> def get_path_for_function ( f ) : <EOL> '''<STR_LIT>''' <EOL> if hasattr ( f , "<STR_LIT>" ) : <EOL> module_name = f . __module__ <EOL> elif hasattr ( f , "<STR_LIT>" ) : <EOL> module_name = f . im_func . __module__ <EOL> else : <EOL> logger . warning ( "<STR_LIT>" , f ) <EOL> return None <EOL> module = sys . modules [ module_name ] <EOL> if hasattr ( module , "<STR_LIT>" ) : <EOL> return module . __file__ <EOL> else : <EOL> logger . warning ( "<STR_LIT>" , module_name ) <EOL> return None <EOL> def parse_ini_file ( f_loc ) : <EOL> config = configparser . ConfigParser ( ) <EOL> try : <EOL> config . read ( f_loc ) <EOL> return { k : v for k , v in config . items ( '<STR_LIT>' ) } <EOL> except ( configparser . Error , KeyError , TypeError ) : <EOL> logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , f_loc ) <EOL> return None <EOL> def check_ast_node ( name ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> node = getattr ( ast , name ) <EOL> if issubclass ( node , ast . AST ) : <EOL> return name <EOL> except AttributeError : <EOL> pass <EOL> raise TypeError ( "<STR_LIT>" % name ) </s>
