def check_astng_module(self, astng, checkers): """check a module from its astng representation, real work""" # call raw checkers if possible if not astng.pure_python: self.add_message('I0001', args=astng.name) else: #assert astng.file.endswith('.py') stream = norm_open(astng.file) # invoke IRawChecker interface on self to fetch module/block # level options self.process_module(stream) if self._ignore_file: return False # walk ast to collect line numbers orig_state = self._module_msgs_state.copy() self._module_msgs_state = {} self.collect_block_lines(astng, orig_state) for checker in checkers: if implements(checker, IRawChecker) and checker is not self: stream.seek(0) checker.process_module(stream) # generate events to astng checkers self.astng_events(astng, [checker for checker in checkers if implements(checker, IASTNGChecker)]) return True
def lint_string(self, to_lint): """A lot of this is copy+pasted from PyLinter.""" ast_node = AstroidBuilder(MANAGER).string_build(to_lint) walker = utils.PyLintASTWalker(self) _checkers = self.prepare_checkers() tokencheckers = [c for c in _checkers if interface.implements(c, interfaces.ITokenChecker) and c is not self] rawcheckers = [c for c in _checkers if interface.implements(c, interfaces.IRawChecker)] # notify global begin for checker in _checkers: checker.open() if interface.implements(checker, interfaces.IAstroidChecker): walker.add_checker(checker) self.set_current_module('<module>') self.file_state = utils.FileState('<stmt>') self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = ast_node.file # pylint: disable=maybe-no-member self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
def check_astng_module(self, astng, checkers): """check a module from its astng representation, real work""" # call raw checkers if possible if not astng.pure_python: self.add_message('I0001', args=astng.name) else: #assert astng.file.endswith('.py') stream = norm_open(astng.file) # invoke IRawChecker interface on self to fetch module/block # level options self.process_module(stream) if self._ignore_file: return False # walk ast to collect line numbers orig_state = self._module_msgs_state.copy() self._module_msgs_state = {} self.collect_block_lines(astng, orig_state) for checker in checkers: if implements(checker, IRawChecker) and checker is not self: stream.seek(0) checker.process_module(stream) # generate events to astng checkers self.astng_events(astng, [ checker for checker in checkers if implements(checker, IASTNGChecker) ]) return True
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ # initialize msgs_state now that all messages have been registered into # the store for msg in self.msgs_store.messages: if not msg.may_be_emitted(): self._msgs_state[msg.msgid] = False if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules, ) walker = PyLintASTWalker(self) checkers = self.prepare_checkers() tokencheckers = [ c for c in checkers if implements(c, ITokenChecker) and c is not self ] rawcheckers = [c for c in checkers if implements(c, IRawChecker)] # notify global begin for checker in checkers: checker.open() if implements(checker, IAstroidChecker): walker.add_checker(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr['name'], descr['path'] if not descr['isarg'] and not self.should_analyze_file( modname, filepath): continue if self.config.files_output: reportfile = 'pylint_%s.%s' % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, 'w')) self.set_current_module(modname, filepath) # get the module representation astroid = self.get_ast(filepath, modname) if astroid is None: continue # XXX to be correct we need to keep module_msgs_state for every # analyzed module (the problem stands with localized messages which # are only detected in the .close step) self.file_state = FileState(descr['basename']) self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astroid.file # pylint: disable=maybe-no-member self.check_astroid_module(astroid, walker, rawcheckers, tokencheckers) # warn about spurious inline messages handling for msgid, line, args in self.file_state.iter_spurious_suppression_messages( self.msgs_store): self.add_message(msgid, line, None, args) # notify global end self.set_current_module('') self.stats['statement'] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ # initialize msgs_state now that all messages have been registered into # the store for msg in self.msgs_store.messages: if not msg.may_be_emitted(): self._msgs_state[msg.msgid] = False if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules,) walker = PyLintASTWalker(self) checkers = self.prepare_checkers() tokencheckers = [c for c in checkers if implements(c, ITokenChecker) and c is not self] rawcheckers = [c for c in checkers if implements(c, IRawChecker)] # notify global begin for checker in checkers: checker.open() if implements(checker, IAstroidChecker): walker.add_checker(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr['name'], descr['path'] if not descr['isarg'] and not self.should_analyze_file(modname, filepath): continue if self.config.files_output: reportfile = 'pylint_%s.%s' % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, 'w')) self.set_current_module(modname, filepath) # get the module representation astroid = self.get_ast(filepath, modname) if astroid is None: continue # XXX to be correct we need to keep module_msgs_state for every # analyzed module (the problem stands with localized messages which # are only detected in the .close step) self.file_state = FileState(descr['basename']) self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astroid.file # pylint: disable=maybe-no-member self.check_astroid_module(astroid, walker, rawcheckers, tokencheckers) # warn about spurious inline messages handling for msgid, line, args in self.file_state.iter_spurious_suppression_messages(self.msgs_store): self.add_message(msgid, line, None, args) # notify global end self.set_current_module('') self.stats['statement'] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules, ) walker = PyLintASTWalker(self) checkers = self.prepare_checkers() tokencheckers = [ c for c in checkers if implements(c, ITokenChecker) and c is not self ] rawcheckers = [c for c in checkers if implements(c, IRawChecker)] # notify global begin for checker in checkers: checker.open() if implements(checker, IAstroidChecker): walker.add_checker(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr['name'], descr['path'] if not descr['isarg'] and not self.should_analyze_file( modname, filepath): continue if self.config.files_output: reportfile = 'pylint_%s.%s' % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, 'w')) self.set_current_module(modname, filepath) # get the module representation astroid = self.get_ast(filepath, modname) if astroid is None: continue self.base_name = descr['basename'] self.base_file = descr['basepath'] self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astroid.file # pylint: disable=maybe-no-member self.check_astroid_module(astroid, walker, rawcheckers, tokencheckers) self._add_suppression_messages() # notify global end self.set_current_module('') self.stats['statement'] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def _init_linter(): global _linter global _walker global _token_checkers global _raw_checkers global _reporter _linter = lint.PyLinter(reporter=_reporter) _linter.load_default_plugins() _walker = utils.PyLintASTWalker(_linter) checkers = _linter.prepare_checkers() _token_checkers = [c for c in checkers if implements(c, ITokenChecker) and c is not _linter] _raw_checkers = [c for c in checkers if implements(c, IRawChecker)] for checker in checkers: checker.open() if implements(checker, IAstroidChecker): _walker.add_checker(checker)
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ self.reporter.include_ids = self.config.include_ids if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules, ) walker = PyLintASTWalker(self) checkers = self.prepare_checkers() rawcheckers = [ c for c in checkers if implements(c, IRawChecker) and c is not self ] # notify global begin for checker in checkers: checker.open() if implements(checker, IASTNGChecker): walker.add_checker(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr['name'], descr['path'] self.set_current_module(modname, filepath) # get the module representation astng = self.get_astng(filepath, modname) if astng is None: continue self.base_name = descr['basename'] self.base_file = descr['basepath'] if self.config.files_output: reportfile = 'pylint_%s.%s' % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, 'w')) self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astng.file self.check_astng_module(astng, walker, rawcheckers) # Close file for windows users astng.file_stream.close() # notify global end self.set_current_module('') self.stats['statement'] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ self.reporter.include_ids = self.config.include_ids if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules,) walker = PyLintASTWalker(self) checkers = self.prepare_checkers() rawcheckers = [c for c in checkers if implements(c, IRawChecker) and c is not self] # notify global begin for checker in checkers: checker.open() if implements(checker, IASTNGChecker): walker.add_checker(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr['name'], descr['path'] self.set_current_module(modname, filepath) # get the module representation astng = self.get_astng(filepath, modname) if astng is None: continue self.base_name = descr['basename'] self.base_file = descr['basepath'] if self.config.files_output: reportfile = 'pylint_%s.%s' % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, 'w')) self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astng.file self.check_astng_module(astng, walker, rawcheckers) # Close file for windows users astng.file_stream.close() # notify global end self.set_current_module('') self.stats['statement'] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules,) walker = PyLintASTWalker(self) checkers = self.prepare_checkers() tokencheckers = [c for c in checkers if implements(c, ITokenChecker) and c is not self] rawcheckers = [c for c in checkers if implements(c, IRawChecker)] # notify global begin for checker in checkers: checker.open() if implements(checker, IAstroidChecker): walker.add_checker(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr['name'], descr['path'] if not self.should_analyze_file(modname, filepath): continue if self.config.files_output: reportfile = 'pylint_%s.%s' % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, 'w')) self.set_current_module(modname, filepath) # get the module representation astroid = self.get_ast(filepath, modname) if astroid is None: continue self.base_name = descr['basename'] self.base_file = descr['basepath'] self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astroid.file # pylint: disable=maybe-no-member self.check_astroid_module(astroid, walker, rawcheckers, tokencheckers) self._add_suppression_messages() # notify global end self.set_current_module('') self.stats['statement'] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def _init_linter(): global _linter global _walker global _token_checkers global _raw_checkers global _reporter _linter = lint.PyLinter(reporter=_reporter) _linter.load_default_plugins() _walker = utils.PyLintASTWalker(_linter) checkers = _linter.prepare_checkers() _token_checkers = [ c for c in checkers if implements(c, ITokenChecker) and c is not _linter ] _raw_checkers = [c for c in checkers if implements(c, IRawChecker)] for checker in checkers: checker.open() if implements(checker, IAstroidChecker): _walker.add_checker(checker)
def check(self, files_or_modules): """main checking entry: check a list of files or modules from their name. """ self.reporter.include_ids = self.config.include_ids if not isinstance(files_or_modules, (list, tuple)): files_or_modules = (files_or_modules,) checkers = self._get_checkers() rawcheckers = [] walker = PyLintASTWalker() # notify global begin for checker in checkers: checker.open() if implements(checker, IASTNGChecker): walker.add_checker(checker) if implements(checker, IRawChecker) and checker is not self: # XXX rawcheckers.append(checker) # build ast and check modules or packages for descr in self.expand_files(files_or_modules): modname, filepath = descr["name"], descr["path"] self.set_current_module(modname, filepath) # get the module representation astng = self.get_astng(filepath, modname) if astng is None: continue self.base_name = descr["basename"] self.base_file = descr["basepath"] if self.config.files_output: reportfile = "pylint_%s.%s" % (modname, self.reporter.extension) self.reporter.set_output(open(reportfile, "w")) self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = astng.file self.check_astng_module(astng, walker, rawcheckers) # notify global end self.set_current_module("") self.stats["statement"] = walker.nbstatements checkers.reverse() for checker in checkers: checker.close()
def register_messages(self, checker): """register a dictionary of messages Keys are message ids, values are a 2-uple with the message type and the message itself message ids should be a string of len 4, where the two first characters are the checker id and the two last the message id in this checker """ msgs_dict = checker.msgs chkid = None for msgid, msg_tuple in msgs_dict.iteritems(): if implements(checker, (IRawChecker, ITokenChecker)): scope = WarningScope.LINE else: scope = WarningScope.NODE if len(msg_tuple) > 2: (msg, msgsymbol, msgdescr) = msg_tuple[:3] assert msgsymbol not in self._messages_by_symbol, \ 'Message symbol %r is already defined' % msgsymbol if len(msg_tuple) > 3: if 'scope' in msg_tuple[3]: scope = msg_tuple[3]['scope'] if 'minversion' in msg_tuple[3]: minversion = msg_tuple[3]['minversion'] if minversion > sys.version_info: self._msgs_state[msgid] = False continue if 'maxversion' in msg_tuple[3]: maxversion = msg_tuple[3]['maxversion'] if maxversion <= sys.version_info: self._msgs_state[msgid] = False continue else: # messages should have a symbol, but for backward compatibility # they may not. (msg, msgdescr) = msg_tuple warn( "[pylint 0.26] description of message %s doesn't include " "a symbolic name" % msgid, DeprecationWarning) msgsymbol = None # avoid duplicate / malformed ids assert msgid not in self._messages, \ 'Message id %r is already defined' % msgid assert chkid is None or chkid == msgid[1:3], \ 'Inconsistent checker part in message id %r' % msgid chkid = msgid[1:3] msg = MessageDefinition(checker, msgid, msg, msgdescr, msgsymbol, scope) self._messages[msgid] = msg self._messages_by_symbol[msgsymbol] = msg self._msgs_by_category.setdefault(msgid[0], []).append(msgid)
def register_messages(self, checker): """register a dictionary of messages Keys are message ids, values are a 2-uple with the message type and the message itself message ids should be a string of len 4, where the two first characters are the checker id and the two last the message id in this checker """ msgs_dict = checker.msgs chkid = None for msgid, msg_tuple in msgs_dict.iteritems(): if implements(checker, (IRawChecker, ITokenChecker)): scope = WarningScope.LINE else: scope = WarningScope.NODE if len(msg_tuple) > 2: (msg, msgsymbol, msgdescr) = msg_tuple[:3] assert msgsymbol not in self._messages_by_symbol, \ 'Message symbol %r is already defined' % msgsymbol if len(msg_tuple) > 3: if 'scope' in msg_tuple[3]: scope = msg_tuple[3]['scope'] if 'minversion' in msg_tuple[3]: minversion = msg_tuple[3]['minversion'] if minversion > sys.version_info: self._msgs_state[msgid] = False continue if 'maxversion' in msg_tuple[3]: maxversion = msg_tuple[3]['maxversion'] if maxversion <= sys.version_info: self._msgs_state[msgid] = False continue else: # messages should have a symbol, but for backward compatibility # they may not. (msg, msgdescr) = msg_tuple warn("[pylint 0.26] description of message %s doesn't include " "a symbolic name" % msgid, DeprecationWarning) msgsymbol = None # avoid duplicate / malformed ids assert msgid not in self._messages, \ 'Message id %r is already defined' % msgid assert chkid is None or chkid == msgid[1:3], \ 'Inconsistent checker part in message id %r' % msgid chkid = msgid[1:3] msg = MessageDefinition(checker, msgid, msg, msgdescr, msgsymbol, scope) self._messages[msgid] = msg self._messages_by_symbol[msgsymbol] = msg self._msgs_by_category.setdefault(msgid[0], []).append(msgid)
def build_message_def(checker, msgid, msg_tuple): if implements(checker, (IRawChecker, ITokenChecker)): default_scope = WarningScope.LINE else: default_scope = WarningScope.NODE options = {} if len(msg_tuple) > 3: (msg, symbol, descr, options) = msg_tuple elif len(msg_tuple) > 2: (msg, symbol, descr) = msg_tuple[:3] else: # messages should have a symbol, but for backward compatibility # they may not. (msg, descr) = msg_tuple warn("[pylint 0.26] description of message %s doesn't include " "a symbolic name" % msgid, DeprecationWarning) symbol = None options.setdefault("scope", default_scope) return MessageDefinition(checker, msgid, msg, descr, symbol, **options)
def build_message_def(checker, msgid, msg_tuple): if implements(checker, (IRawChecker, ITokenChecker)): default_scope = WarningScope.LINE else: default_scope = WarningScope.NODE options = {} if len(msg_tuple) > 3: (msg, symbol, descr, options) = msg_tuple elif len(msg_tuple) > 2: (msg, symbol, descr) = msg_tuple[:3] else: # messages should have a symbol, but for backward compatibility # they may not. (msg, descr) = msg_tuple warnings.warn("[pylint 0.26] description of message %s doesn't include " "a symbolic name" % msgid, DeprecationWarning) symbol = None options.setdefault('scope', default_scope) return MessageDefinition(checker, msgid, msg, descr, symbol, **options)
def constraint_by_interface(self, iface): for cstr in self.constraints: if implements(cstr, iface): return cstr return None