From a245eb3990843d24a30180c836602c265c71a709 Mon Sep 17 00:00:00 2001 From: Michal Minar Date: Wed, 23 Jan 2013 17:09:23 +0100 Subject: pyling plugins improvements added allow_cmpi_logging module for suppressing errors concerning logging statements disabled our custom C9904 warning message because of providers like CapabilitiesProvider having no prefix - but still checking for consistency in module name and class name --- tools/pylint/plugins/allow_cmpi_logging.py | 90 ++++++++++++++++++++++++++++ tools/pylint/plugins/cim_provider_checker.py | 16 ++--- tools/pylint/pylintrc | 2 +- 3 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 tools/pylint/plugins/allow_cmpi_logging.py (limited to 'tools/pylint') diff --git a/tools/pylint/plugins/allow_cmpi_logging.py b/tools/pylint/plugins/allow_cmpi_logging.py new file mode 100644 index 0000000..4597301 --- /dev/null +++ b/tools/pylint/plugins/allow_cmpi_logging.py @@ -0,0 +1,90 @@ +# -*- encoding: utf-8 -*- +# Software Management Providers +# +# Copyright (C) 2012 Red Hat, Inc. All rights reserved. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Authors: Michal Minar +""" +Pylint plugin supressing error concerning the use of cmpi_logging +facilities. +""" +from logilab.astng import scoped_nodes +from logilab.astng import MANAGER +from logilab.astng.builder import ASTNGBuilder +from pylint.interfaces import IASTNGChecker +from pylint.checkers import BaseChecker + +def logging_transform(module): + """ + Make the logging.RootLogger look, like it has following methods: + * trace_warn + * trace_info + * trace_verbose + These are actually defined in its subclass CMPILogger. + But pylint thinks, that RootLogger is used all the time. + """ + if module.name != 'logging' or 'RootLogger' not in module: + return + fake = ASTNGBuilder(MANAGER).string_build(''' + +def trace_warn(self, msg, *args, **kwargs): + print(self, msg, args, kwargs) + +def trace_info(self, msg, *args, **kwargs): + print(self, msg, args, kwargs) + +def trace_verbose(self, msg, *args, **kwargs): + print(self, msg, args, kwargs) + +''') + for level in ('warn', 'info', 'verbose'): + method_name = 'trace_' + level + for child in module.locals['RootLogger']: + if not isinstance(child, scoped_nodes.Class): + continue + child.locals[method_name] = fake.locals[method_name] + +class CMPILoggingChecker(BaseChecker): + """ + Checker that only disables some error messages of our custom + cmpi_logging facilities. + """ + + __implements__ = IASTNGChecker + + name = 'cmpi_logging' + # When we do have some real warning messages/reports, + # remote W9921. We need it just to get registered. + msgs = { 'W9921': ('Dummy', "This is a dummy message.")} + + def visit_class(self, node): + """ + This supresses some warnings for CMPILogger class. + """ + if ( isinstance(node.parent, scoped_nodes.Module) + and node.parent.name.split('.')[-1] == "cmpi_logging" + and node.name == "CMPILogger"): + # supress warning about too many methods defined + self.linter.disable('R0904', scope='module', line=node.lineno) + +def register(linter): + """ + Called when loaded by pylint --load-plugins. + Let's register our tranformation function and checker here. + """ + MANAGER.register_transformer(logging_transform) + linter.register_checker(CMPILoggingChecker(linter)) diff --git a/tools/pylint/plugins/cim_provider_checker.py b/tools/pylint/plugins/cim_provider_checker.py index 8691b5a..a1b557b 100644 --- a/tools/pylint/plugins/cim_provider_checker.py +++ b/tools/pylint/plugins/cim_provider_checker.py @@ -78,10 +78,6 @@ class CIMProviderChecker(BaseChecker): name = 'cim_provider' msgs = { - 'C9904': ('Invalid provider class name %s', - "Class name representing cim provider should be in inform " - "_. Where and should be both " - "written in CamelCase."), 'C9905': ('Invalid provider module name %s', "Module containing cim provider(s) should be named as " "_. Where both and are " @@ -114,21 +110,17 @@ class CIMProviderChecker(BaseChecker): """ if "CIMProvider2" in [a.name for a in node.ancestors()]: supress_cim_provider_messages(self.linter, node) - clsm = _RE_PROVIDER_CLASS_NAME.match(node.name) - if not clsm: - self.add_message('C9904', node=node, args=node.name) parent = node.parent while not isinstance(parent, scoped_nodes.Module): parent = parent.parent + clsm = _RE_PROVIDER_CLASS_NAME.match(node.name) modm = _RE_PROVIDER_MODULE_NAME.match(parent.name) - if not modm: + if clsm and not modm: self.add_message('C9905', node=node, args=parent.name) - if not clsm: - return - if clsm.group('prefix') != modm.group('prefix'): + if clsm and clsm.group('prefix') != modm.group('prefix'): self.add_message('C9906', node=node, args=(modm.group('prefix'), clsm.group('prefix'))) - if clsm.group('prefix') != 'LMI': + if clsm and clsm.group('prefix') != 'LMI': self.add_message('C9910', node=node, args=clsm.group('prefix')) if not 'get_providers' in parent.keys(): self.add_message('E9907', node=parent, args=parent.name) diff --git a/tools/pylint/pylintrc b/tools/pylint/pylintrc index f065e65..45a25ea 100644 --- a/tools/pylint/pylintrc +++ b/tools/pylint/pylintrc @@ -19,7 +19,7 @@ persistent=yes # List of plugins (as comma separated values of python modules names) to load, # usually to register additional checkers. -load-plugins=unittest_checker,cim_provider_checker +load-plugins=unittest_checker,cim_provider_checker,allow_cmpi_logging [MESSAGES CONTROL] -- cgit