pylint 可以检查所有文档顶部的静态注释/版权声明吗?

问题描述 投票:0回答:1

pylint 是否可以配置为检查特定的静态文本,例如每个文件顶部的版权声明?

例如验证每个文件是否以以下两行开头:

# Copyright Spacely Sprockets, 2018-2062
#
python pylint
1个回答
6
投票

您可以编写自己的检查器:

from pylint.checkers import BaseRawFileChecker

class CopyrightChecker(BaseRawFileChecker):
    """ Check the first line for copyright notice
    """

    name = 'custom_copy'
    msgs = {'W9902': ('Include copyright in file',
                      'file-no-copyright',
                      ('Your file has no copyright')),
            }
    options = ()

    def process_module(self, node):
        """process a module
        the module's content is accessible via node.stream() function
        """
        with node.stream() as stream:
            for (lineno, line) in enumerate(stream):
                if lineno == 1:
                    # Check for copyright notice
                    # if it fails add an error message
                    self.add_message('file-no-copyright',
                                     line=lineno)


    def register(linter):
        """required method to auto register this checker"""
        linter.register_checker(CopyrightChecker(linter))

查看有关自定义检查器的更多信息在 pylint 文档中。还有这篇关于这个主题的优秀文章

更新 感谢评论中的 @Diego 注意到这已经过时了,现在我们可以从

BaseRawFileChecker
继承。

© www.soinside.com 2019 - 2024. All rights reserved.