pylint 可以检查所有文档顶部的静态评论/版权声明吗?
Can pylint check for a static comment / copyright notice at the top of all documents?
是否可以将 pylint 配置为检查特定的静态文本,例如每个文件顶部的版权声明?
例如验证以下两行是否开始每个文件:
# Copyright Spacely Sprockets, 2018-2062
#
您可以编写自己的检查器:
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. Also this excellent post about the subject。
是否可以将 pylint 配置为检查特定的静态文本,例如每个文件顶部的版权声明?
例如验证以下两行是否开始每个文件:
# Copyright Spacely Sprockets, 2018-2062
#
您可以编写自己的检查器:
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. Also this excellent post about the subject。