pylint能否在所有文档的顶部检查静态注释/版权声明?

2024-09-30 14:29:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

例如,验证以下两行开始于每个文件:

# Copyright Spacely Sprockets, 2018-2062
#

Tags: 文件文本声明静态版权sprocketspylintcopyright
1条回答
网友
1楼 · 发布于 2024-09-30 14:29:22

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

from pylint.interfaces import IRawChecker
from pylint.checkers import BaseChecker

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

    __implements__ = IRawChecker

    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))

查看有关自定义方格的更多信息in the pylint documentation。还有this excellent post about the subject。在

相关问题 更多 >