[PATCH 3/3] utils: checkstyle: Add a python formatter

Stefan Klug stefan.klug at ideasonboard.com
Fri Aug 30 14:53:00 CEST 2024


Reporting style issues on python files is great, automatically fixing
them is even better. Add a call to autopep8 for python files.

Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>
---
 utils/checkstyle.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index 5901f1a71562..ed15145b64a4 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -954,6 +954,21 @@ class StripTrailingSpaceFormatter(Formatter):
         return ''.join(lines)
 
 
+class Pep8Formatter(Formatter):
+    patterns = ('*.py',)
+
+    @classmethod
+    def format(cls, filename, data):
+        try:
+            ret = subprocess.run(['autopep8', '--ignore=E501', '-'],
+                                 input=data.encode('utf-8'), stdout=subprocess.PIPE)
+        except FileNotFoundError:
+            issues.append(StyleIssue(0, None, None, 'Please install autopep8 to format python additions'))
+            return issues
+
+        return ret.stdout.decode('utf-8')
+
+
 # ------------------------------------------------------------------------------
 # Style checking
 #
-- 
2.43.0



More information about the libcamera-devel mailing list