如何在自定义pylint扩展名中获取pylint掉毛的文件的名称

时间:2019-01-30 10:35:35

标签: python python-2.7 pylint

我想获取正在删除的文件的名称,因为我只想对特定文件施加一组特定的规则。更具体地说,我想做一个自定义的pylint扩展名,以检查并阻止某些文件通过查看其导入来导入其他文件。

我尝试了谷歌搜索,但找不到答案。

class RestrictedImportChecker(checkers.BaseChecker):
    """Custom pylint checker which checks restricted imports."""

    __implements__ = interfaces.IAstroidChecker
    name = 'disallowed-layer-import'
    priority = -1
    msgs = {
        'C0009': (
            'Importing \"%s\" from \"%s\" is prohibited.',
            'disallowed-layer-import',
            'Importing domain layer from storage layer is '
            'not allowed.'),
    }

    # @checker_utils.check_messages('disallowed-layer-import')
    def visit_import(self, node):
        """Visits every import statement in the file.

        Args:
         node: astroid.scoped_nodes.Function. Node for a function or method
                definition in the AST.
        """

        try:
            modnode = node.root()
            names = [name for name, _ in node.names]
            print '@@@@@@@@@@'
            print names
            self.add_message(
                'disallowed-layer-import',
                node=node,
                args=('sth', 'sth2'),
            )
        except astroid.AstroidBuildingException:
            return

我可以获取导入模块的名称,但是无法获取文件的名称。

0 个答案:

没有答案