代码之家  ›  专栏  ›  技术社区  ›  David Parks

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

  •  0
  • David Parks  · 技术社区  · 6 年前

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

    E、 g.验证以下两行以每个文件开头:

    # Copyright Spacely Sprockets, 2018-2062
    #
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   yorodm    6 年前

    你可以自己写支票:

    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 .