From patchwork Thu Dec 24 12:28:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10738 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 08C5CC0F1A for ; Thu, 24 Dec 2020 12:29:13 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CCD78615B0; Thu, 24 Dec 2020 13:29:12 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="fVnESeqV"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AC52160528 for ; Thu, 24 Dec 2020 13:29:08 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4CC2FA1D for ; Thu, 24 Dec 2020 13:29:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1608812948; bh=9n2XxMWv5HmxGxi11lL7HODHwJPLMDLZV43LTTuKVIM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fVnESeqVkqv5Ffmx7TYDJ/Ys8uW1AQaMz6K9dmUNE8FuIjPYgt4AVtBJsWrUbzxeR FWQGfddwHOwAiBIgghhhdWAa/psdw/XhoC0OODOvflyv7L8OLeI9OAKFUuQ6ZneTBD m0MOakVxlkGc5SlQ7di/ZR2vbXzrMqwYnjffwPDo= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 24 Dec 2020 14:28:49 +0200 Message-Id: <20201224122855.22200-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201224122855.22200-1-laurent.pinchart@ideasonboard.com> References: <20201224122855.22200-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/8] utils: checkstyle.py: Factor out automatic class registry X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The style checkers and formatters duplicate automatic class registry code. Factor it out to a common ClassRegistry helper class. The list of subclasses is moved to a class member variable of the auto-registered base class type. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- utils/checkstyle.py | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index c04bf3850dcd..76267d5ea764 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -190,21 +190,25 @@ def parse_diff(diff): return hunks +# ------------------------------------------------------------------------------ +# Helpers +# + +class ClassRegistry(type): + def __new__(cls, clsname, bases, attrs): + newclass = super().__new__(cls, clsname, bases, attrs) + if bases: + bases[0].subclasses.append(newclass) + return newclass + + # ------------------------------------------------------------------------------ # Style Checkers # -_style_checkers = [] +class StyleChecker(metaclass=ClassRegistry): + subclasses = [] -class StyleCheckerRegistry(type): - def __new__(cls, clsname, bases, attrs): - newclass = super().__new__(cls, clsname, bases, attrs) - if clsname != 'StyleChecker': - _style_checkers.append(newclass) - return newclass - - -class StyleChecker(metaclass=StyleCheckerRegistry): def __init__(self): pass @@ -213,7 +217,7 @@ class StyleChecker(metaclass=StyleCheckerRegistry): # @classmethod def checkers(cls, filename): - for checker in _style_checkers: + for checker in cls.subclasses: if checker.supports(filename): yield checker @@ -227,7 +231,7 @@ class StyleChecker(metaclass=StyleCheckerRegistry): @classmethod def all_patterns(cls): patterns = set() - for checker in _style_checkers: + for checker in cls.subclasses: patterns.update(checker.patterns) return patterns @@ -383,18 +387,9 @@ class ShellChecker(StyleChecker): # Formatters # -_formatters = [] - -class FormatterRegistry(type): - def __new__(cls, clsname, bases, attrs): - newclass = super().__new__(cls, clsname, bases, attrs) - if clsname != 'Formatter': - _formatters.append(newclass) - return newclass - - -class Formatter(metaclass=FormatterRegistry): +class Formatter(metaclass=ClassRegistry): enabled = True + subclasses = [] def __init__(self): pass @@ -404,7 +399,7 @@ class Formatter(metaclass=FormatterRegistry): # @classmethod def formatters(cls, filename): - for formatter in _formatters: + for formatter in cls.subclasses: if not cls.enabled: continue if formatter.supports(filename): @@ -422,7 +417,7 @@ class Formatter(metaclass=FormatterRegistry): @classmethod def all_patterns(cls): patterns = set() - for formatter in _formatters: + for formatter in cls.subclasses: if not cls.enabled: continue patterns.update(formatter.patterns)