summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-12-24 12:34:55 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-12-29 16:45:28 +0200
commit86efa9c7e9e7f518345adf469b194c1e5d053c0c (patch)
treefc9b9f7dbfffe1403ba1a65550931f8e95a1336d /utils
parentb402734e5ae2b35c0e38381864a9b24c129b03bf (diff)
utils: checkstyle.py: Factor out automatic class registry
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@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'utils')
-rwxr-xr-xutils/checkstyle.py39
1 files changed, 17 insertions, 22 deletions
diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index c04bf385..76267d5e 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -191,20 +191,24 @@ def parse_diff(diff):
# ------------------------------------------------------------------------------
-# Style Checkers
+# Helpers
#
-_style_checkers = []
-
-class StyleCheckerRegistry(type):
+class ClassRegistry(type):
def __new__(cls, clsname, bases, attrs):
newclass = super().__new__(cls, clsname, bases, attrs)
- if clsname != 'StyleChecker':
- _style_checkers.append(newclass)
+ if bases:
+ bases[0].subclasses.append(newclass)
return newclass
-class StyleChecker(metaclass=StyleCheckerRegistry):
+# ------------------------------------------------------------------------------
+# Style Checkers
+#
+
+class StyleChecker(metaclass=ClassRegistry):
+ subclasses = []
+
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)