[v2,1/2] utils: checkstyle: Add a python formatter
diff mbox series

Message ID 20240903161059.246075-2-stefan.klug@ideasonboard.com
State Accepted
Commit 8ffaf376bb53f6c4433e1fda4078f66dc5e69af5
Headers show
Series
  • Python improvements to checkstyle.py
Related show

Commit Message

Stefan Klug Sept. 3, 2024, 4:10 p.m. UTC
Reporting style issues on python files is great, automatically fixing them is
even better. Add a call to autopep8 for python files. This fixes the same
issues as the ones reported by pycodestyle.

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

Comments

Laurent Pinchart Sept. 3, 2024, 6:24 p.m. UTC | #1
Hi Stefan,

Thank you for the patch.

On Tue, Sep 03, 2024 at 06:10:54PM +0200, Stefan Klug wrote:
> Reporting style issues on python files is great, automatically fixing them is
> even better. Add a call to autopep8 for python files. This fixes the same
> issues as the ones reported by pycodestyle.

Please reflow the commit message. You could set up your editor to do so
automatically.

> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  utils/checkstyle.py | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/utils/checkstyle.py b/utils/checkstyle.py
> index c9e41d4149f7..1ee211c3bb9b 100755
> --- a/utils/checkstyle.py
> +++ b/utils/checkstyle.py
> @@ -943,6 +943,21 @@ class IncludeOrderFormatter(Formatter):
>          return '\n'.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')
> +
> +
>  class StripTrailingSpaceFormatter(Formatter):
>      patterns = ('*.c', '*.cpp', '*.h', '*.py', 'meson.build')
>
Kieran Bingham Sept. 4, 2024, 9:58 a.m. UTC | #2
Quoting Stefan Klug (2024-09-03 17:10:54)
> Reporting style issues on python files is great, automatically fixing them is
> even better. Add a call to autopep8 for python files. This fixes the same
> issues as the ones reported by pycodestyle.
> 

Ohhh this seems like a good helper series!

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> ---
>  utils/checkstyle.py | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/utils/checkstyle.py b/utils/checkstyle.py
> index c9e41d4149f7..1ee211c3bb9b 100755
> --- a/utils/checkstyle.py
> +++ b/utils/checkstyle.py
> @@ -943,6 +943,21 @@ class IncludeOrderFormatter(Formatter):
>          return '\n'.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')
> +
> +
>  class StripTrailingSpaceFormatter(Formatter):
>      patterns = ('*.c', '*.cpp', '*.h', '*.py', 'meson.build')
>  
> -- 
> 2.43.0
>

Patch
diff mbox series

diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index c9e41d4149f7..1ee211c3bb9b 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -943,6 +943,21 @@  class IncludeOrderFormatter(Formatter):
         return '\n'.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')
+
+
 class StripTrailingSpaceFormatter(Formatter):
     patterns = ('*.c', '*.cpp', '*.h', '*.py', 'meson.build')