Exemple #1
0
    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()
Exemple #2
0
    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()
Exemple #3
0
 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()
Exemple #4
0
    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()
Exemple #5
0
    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()
Exemple #6
0
 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()
Exemple #7
0
 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()
Exemple #8
0
    def _remove_unused_imports(self, module_node, fake_node):
        '''
We import objects from fake code and from models, so pylint doesn't complain about undefined objects.
But now it complains a lot about unused imports.
We cannot suppress it, so we call VariableChecker with fake linter to intercept and collect all such error messages,
and then use them to remove unused imports.
        '''
        #Needed for removal of unused import messages
        sniffer = MessageSniffer() #Our linter substitution
        walker = PyLintASTWalker(sniffer)
        var_checker = VariablesChecker(sniffer)
        walker.add_checker(var_checker)

        #Collect unused import messages
        sniffer.set_fake_node(fake_node)
        sniffer.check_astroid_module(module_node, walker, [], [])

        #Remove unneeded globals imported from fake code
        for name in sniffer.unused:
            if name in fake_node.globals and \
              name in module_node.locals: #Maybe it's already deleted
                del module_node.locals[name]

        return module_node
Exemple #9
0
 def walk(self, node):
     """recursive walk on the given node"""
     walker = PyLintASTWalker(linter)
     walker.add_checker(self.checker)
     walker.walk(node)
 def walk(self, node):
     """recursive walk on the given node"""
     walker = PyLintASTWalker(linter)
     walker.add_checker(self.checker)
     walker.walk(node)
def register(linter): #pylint: disable=W0613
    """Pylint calls this hook to actually activate the plugin"""
    walker = PyLintASTWalker(linter)
    walker.add_checker(ImportRewriterVisitor())
    MANAGER.register_transformer(walker.walk)