[libcamera-devel] [PATCH 2/8] utils: checkstyle.py: Factor out automatic class registry
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Thu Dec 24 13:28:49 CET 2020
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 <laurent.pinchart at ideasonboard.com>
---
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)
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list