<s> """<STR_LIT>""" <EOL> import importlib <EOL> import os <EOL> import re <EOL> from . import linter , persist , util <EOL> class PythonLinter ( linter . Linter ) : <EOL> """<STR_LIT>""" <EOL> SHEBANG_RE = re . compile ( r'<STR_LIT>' ) <EOL> comment_re = r'<STR_LIT>' <EOL> module = None <EOL> check_version = False <EOL> @ staticmethod <EOL> def match_shebang ( code ) : <EOL> """<STR_LIT>""" <EOL> match = PythonLinter . SHEBANG_RE . match ( code ) <EOL> if match : <EOL> return '<STR_LIT>' , match . group ( '<STR_LIT:version>' ) <EOL> else : <EOL> return None <EOL> shebang_match = match_shebang <EOL> @ classmethod <EOL> def initialize ( cls ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . initialize ( ) <EOL> persist . import_sys_path ( ) <EOL> cls . import_module ( ) <EOL> @ classmethod <EOL> def reinitialize ( cls ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> delattr ( cls , '<STR_LIT>' ) <EOL> cls . initialize ( ) <EOL> @ classmethod <EOL> def import_module ( cls ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> return <EOL> module = getattr ( cls , '<STR_LIT>' , None ) <EOL> cls . _cmd = None <EOL> cmd = cls . cmd <EOL> script = None <EOL> if isinstance ( cls . cmd , ( list , tuple ) ) : <EOL> cmd = cls . cmd [ <NUM_LIT:0> ] <EOL> if module is not None : <EOL> try : <EOL> module = importlib . import_module ( module ) <EOL> persist . debug ( '<STR_LIT>' . format ( cls . name , module ) ) <EOL> if cmd and not callable ( cmd ) : <EOL> match = util . PYTHON_CMD_RE . match ( cmd ) <EOL> if match and match . group ( '<STR_LIT:version>' ) : <EOL> version , script = match . group ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> version = util . find_python ( version = version , script = script , module = module ) <EOL> if version [ <NUM_LIT:0> ] is None or script and version [ <NUM_LIT:1> ] is None : <EOL> module = None <EOL> except ImportError : <EOL> message = '<STR_LIT>' <EOL> if cls . check_version : <EOL> warning = '<STR_LIT>' <EOL> message += '<STR_LIT>' <EOL> else : <EOL> warning = '<STR_LIT>' <EOL> message += '<STR_LIT>' <EOL> persist . printf ( message . format ( warning , module , cls . name ) ) <EOL> module = None <EOL> except Exception as ex : <EOL> persist . printf ( <EOL> '<STR_LIT>' <EOL> . format ( cls . name , str ( ex ) ) <EOL> ) <EOL> module = None <EOL> can_lint = True <EOL> if not module and cmd and not callable ( cmd ) : <EOL> match = util . PYTHON_CMD_RE . match ( cmd ) <EOL> if match and match . group ( '<STR_LIT:version>' ) : <EOL> can_lint = False <EOL> version , script = match . group ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> version = util . find_python ( version = version , script = script ) <EOL> if version [ <NUM_LIT:0> ] is not None and ( not script or version [ <NUM_LIT:1> ] is not None ) : <EOL> can_lint = True <EOL> if can_lint : <EOL> cls . _cmd = cls . cmd <EOL> if module : <EOL> cls . cmd = None <EOL> else : <EOL> persist . printf ( <EOL> '<STR_LIT>' <EOL> . format ( <EOL> cls . name , <EOL> '<STR_LIT>' . format ( script ) if script else '<STR_LIT>' , <EOL> cmd <EOL> ) ) <EOL> cls . disabled = True <EOL> cls . module = module <EOL> def context_sensitive_executable_path ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> if not self . check_version : <EOL> return False , None <EOL> match = util . PYTHON_CMD_RE . match ( cmd [ <NUM_LIT:0> ] ) <EOL> if match : <EOL> settings = self . get_view_settings ( ) <EOL> if '<STR_LIT>' in settings : <EOL> script = match . group ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> which = '<STR_LIT>' . format ( script , settings . get ( '<STR_LIT>' ) ) <EOL> path = self . which ( which ) <EOL> if path : <EOL> if path [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return True , '<STR_LIT>' <EOL> elif path [ <NUM_LIT:0> ] is None : <EOL> return True , None <EOL> return True , path <EOL> return False , None <EOL> @ classmethod <EOL> def get_module_version ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . module : <EOL> module = cls . module <EOL> while True : <EOL> if isinstance ( getattr ( module , '<STR_LIT>' , None ) , str ) : <EOL> return module . __version__ <EOL> if hasattr ( module , '<STR_LIT>' ) : <EOL> try : <EOL> module = importlib . import_module ( module . __package__ ) <EOL> except ImportError : <EOL> return None <EOL> else : <EOL> return None <EOL> def run ( self , cmd , code ) : <EOL> """<STR_LIT>""" <EOL> if self . module is not None : <EOL> use_module = False <EOL> if not self . check_version : <EOL> use_module = True <EOL> else : <EOL> settings = self . get_view_settings ( ) <EOL> version = settings . get ( '<STR_LIT>' ) <EOL> if version is None : <EOL> use_module = cmd is None or cmd [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> else : <EOL> version = util . find_python ( version = version , module = self . module ) <EOL> use_module = version [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> if use_module : <EOL> if persist . debug_mode ( ) : <EOL> persist . printf ( <EOL> '<STR_LIT>' . format ( <EOL> self . name , <EOL> os . path . basename ( self . filename or '<STR_LIT>' ) <EOL> ) <EOL> ) <EOL> try : <EOL> errors = self . check ( code , os . path . basename ( self . filename or '<STR_LIT>' ) ) <EOL> except Exception as err : <EOL> persist . printf ( <EOL> '<STR_LIT>' <EOL> . format ( self . name , str ( err ) ) <EOL> ) <EOL> errors = '<STR_LIT>' <EOL> if isinstance ( errors , ( tuple , list ) ) : <EOL> return '<STR_LIT:\n>' . join ( [ str ( e ) for e in errors ] ) <EOL> else : <EOL> return errors <EOL> else : <EOL> cmd = self . _cmd <EOL> else : <EOL> cmd = self . cmd or self . _cmd <EOL> cmd = self . build_cmd ( cmd = cmd ) <EOL> if cmd : <EOL> return super ( ) . run ( cmd , code ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def check ( self , code , filename ) : <EOL> """<STR_LIT>""" <EOL> persist . printf ( <EOL> '<STR_LIT>' <EOL> . format ( self . name ) <EOL> ) <EOL> return '<STR_LIT>' </s>
