[python tools] Add to flake8 in workflow and fix python files (part #25193) (#27881)
* Flake8 fix error in python tools
* Restyled by isort
---------
Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/.flake8 b/.flake8
index 2481f91..934102b 100644
--- a/.flake8
+++ b/.flake8
@@ -6,7 +6,6 @@
./examples/common/QRCode/*
# temporarily scan only directories with fixed files
# TODO: Remove the paths below when all bugs are fixed
- src/tools/chip-cert/*
build/chip/java/tests/*
build/chip/linux/*
build/config/linux/*
@@ -14,17 +13,6 @@
docs/_extensions/external_content.py
examples/common/pigweed/rpc_console/py/chip_rpc/console.py
examples/lighting-app/python/lighting.py
- scripts/tools/check_zcl_file_sync.py
- scripts/tools/convert_ini.py
- scripts/tools/memory/memdf/__init__.py
- scripts/tools/memory/report_summary.py
- scripts/tools/silabs/FactoryDataProvider.py
- scripts/tools/zap/generate.py
- scripts/tools/zap/prune_outputs.py
- scripts/tools/zap/version_update.py
- scripts/tools/zap/zap_download.py
- scripts/tools/zap_convert_all.py
- src/app/ota_image_tool.py
src/app/zap_cluster_list.py
src/controller/python/chip/yaml/__init__.py
src/controller/python/chip/yaml/format_converter.py
diff --git a/scripts/tools/check_zcl_file_sync.py b/scripts/tools/check_zcl_file_sync.py
index 4a68415..a9fd936 100755
--- a/scripts/tools/check_zcl_file_sync.py
+++ b/scripts/tools/check_zcl_file_sync.py
@@ -60,15 +60,15 @@
print("%s and %s have unexpected differences." % (base_name, ext_name))
print("Differences between expected and actual:")
- for l in difflib.unified_diff(
+ for line in difflib.unified_diff(
json.dumps(ext_data, indent=2).split('\n'),
json.dumps(base_data, indent=2).split('\n'),
fromfile=ext_name,
tofile="<Expected extension file content>",
):
- if l.endswith('\n'):
- l = l[:-1]
- print(l)
+ if line.endswith('\n'):
+ line = line[:-1]
+ print(line)
return 1
diff --git a/scripts/tools/memory/memdf/__init__.py b/scripts/tools/memory/memdf/__init__.py
index 60bd210..fa0e107 100644
--- a/scripts/tools/memory/memdf/__init__.py
+++ b/scripts/tools/memory/memdf/__init__.py
@@ -15,5 +15,7 @@
#
"""Package for working with memory usage information using Pandas DataFrame."""
-from memdf.df import DF, SymbolDF, SectionDF, SegmentDF, ExtentDF, DFs
+from memdf.df import DF, DFs, ExtentDF, SectionDF, SegmentDF, SymbolDF
from memdf.util.config import Config, ConfigDescription
+
+__all__ = [DF, SymbolDF, SectionDF, SegmentDF, ExtentDF, DFs, Config, ConfigDescription]
diff --git a/scripts/tools/memory/report_summary.py b/scripts/tools/memory/report_summary.py
index 88ba23c..e528e41 100755
--- a/scripts/tools/memory/report_summary.py
+++ b/scripts/tools/memory/report_summary.py
@@ -31,7 +31,6 @@
import memdf.collect
import memdf.report
import memdf.select
-import numpy # type: ignore
from memdf import Config, DFs, SymbolDF
diff --git a/scripts/tools/silabs/FactoryDataProvider.py b/scripts/tools/silabs/FactoryDataProvider.py
index 5d3ef39..8cb4895 100644
--- a/scripts/tools/silabs/FactoryDataProvider.py
+++ b/scripts/tools/silabs/FactoryDataProvider.py
@@ -15,12 +15,9 @@
#
import argparse
-import base64
import datetime
import os
-import struct
import subprocess
-import sys
class FactoryDataWriter:
@@ -185,7 +182,7 @@
cmdList.extend(["--serialno", self._args.jtagSerial])
def create_nvm3injected_image(self):
- """ Use commander command lines create a binary flashable to the EFR32
+ """ Use commander command lines create a binary flashable to the EFR32
containing the factory commissioning data in NVM3 section
"""
isDeviceConnected = True
@@ -212,7 +209,7 @@
inputImage = self.BASE_MG24_FILE
else:
raise Exception('Invalid MCU')
- except:
+ except Exception:
isDeviceConnected = False
print("Device not connected")
# When no device is connected user needs to provide the mcu family for which those credentials are to be created
diff --git a/scripts/tools/zap/generate.py b/scripts/tools/zap/generate.py
index 46ae0eb..003279d 100755
--- a/scripts/tools/zap/generate.py
+++ b/scripts/tools/zap/generate.py
@@ -24,7 +24,6 @@
import sys
import tempfile
import traceback
-import urllib.request
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
@@ -195,7 +194,7 @@
if not target_path.endswith(".matter"):
# We expect "something.zap" and don't handle corner cases of
# multiple extensions. This is to work with existing codebase only
- raise Error("Unexpected input zap file %s" % self.zap_config)
+ raise Exception("Unexpected input zap file %s" % zap_config_path)
return target_path
@@ -290,7 +289,7 @@
print('WARNING: clang-format may not be the right version:')
print(' PIGWEED TAG: %s' % clang_config['tags'][0])
print(' ACTUAL VERSION: %s' % version_string)
- except:
+ except Exception:
print("Failed to validate clang version.")
traceback.print_last()
@@ -318,12 +317,11 @@
args = [clang_format, '-i']
args.extend(clangOutputs)
subprocess.check_call(args)
- err = None
print('Formatted using %s (%s)' % (clang_format, subprocess.check_output([clang_format, '--version'])))
for outputName in clangOutputs:
print(' - %s' % outputName)
- except Exception as err:
- print('clang-format error:', err)
+ except subprocess.CalledProcessError as err:
+ print('clang-format error: %s', err)
class LockFileSerializer:
@@ -351,7 +349,7 @@
checkPythonVersion()
cmdLineArgs = runArgumentsParser()
- with LockFileSerializer(cmdLineArgs.lock_file) as lock:
+ with LockFileSerializer(cmdLineArgs.lock_file) as _:
if cmdLineArgs.runBootstrap:
subprocess.check_call(getFilePath("scripts/tools/zap/zap_bootstrap.sh"), shell=True)
diff --git a/scripts/tools/zap/prune_outputs.py b/scripts/tools/zap/prune_outputs.py
index bc223fa..09f92a9 100644
--- a/scripts/tools/zap/prune_outputs.py
+++ b/scripts/tools/zap/prune_outputs.py
@@ -16,7 +16,6 @@
import argparse
import os
import shutil
-import sys
def main():
diff --git a/scripts/tools/zap/version_update.py b/scripts/tools/zap/version_update.py
index ba40ca5..4e60313 100755
--- a/scripts/tools/zap/version_update.py
+++ b/scripts/tools/zap/version_update.py
@@ -125,7 +125,7 @@
found_versions = set()
for m in ZAP_VERSION_RE.finditer(file_data):
version = file_data[m.start():m.end()]
- if not version in found_versions:
+ if version not in found_versions:
logging.info('%s currently used in %s', version, name)
found_versions.add(version)
diff --git a/scripts/tools/zap/zap_download.py b/scripts/tools/zap/zap_download.py
index 4e814c6..c2f6f38 100755
--- a/scripts/tools/zap/zap_download.py
+++ b/scripts/tools/zap/zap_download.py
@@ -32,7 +32,7 @@
try:
import coloredlogs
_has_coloredlogs = True
-except:
+except ImportError:
_has_coloredlogs = False
# Supported log levels, mapping string values required for argument
@@ -58,10 +58,10 @@
def _LogPipeLines(pipe, prefix):
- l = logging.getLogger().getChild(prefix)
+ log = logging.getLogger().getChild(prefix)
for line in iter(pipe.readline, b''):
line = line.strip().decode('utf-8', errors="ignore")
- l.info('%s' % line)
+ log.info('%s' % line)
def _ExecuteProcess(cmd, cwd):
@@ -93,7 +93,7 @@
install_directory
)
- _ExecuteProcess(f"npm ci".split(), install_directory)
+ _ExecuteProcess("npm ci".split(), install_directory)
def _SetupReleaseZap(install_directory: str, zap_version: str):
diff --git a/src/app/ota_image_tool.py b/src/app/ota_image_tool.py
index c666c22..9f2bf04 100755
--- a/src/app/ota_image_tool.py
+++ b/src/app/ota_image_tool.py
@@ -262,7 +262,7 @@
print(f'Magic: {magic:x}')
print(f'Total Size: {total_size}')
print(f'Header Size: {header_size}')
- print(f'Header TLV:')
+ print('Header TLV:')
for tag in header_tlv:
tag_name = HeaderTag(tag).name.replace('_', ' ').title()
diff --git a/src/tools/chip-cert/dacs.py b/src/tools/chip-cert/dacs.py
index 7670516..96722b3 100755
--- a/src/tools/chip-cert/dacs.py
+++ b/src/tools/chip-cert/dacs.py
@@ -5,7 +5,7 @@
import subprocess
import sys
import typing
-from binascii import hexlify, unhexlify
+from binascii import unhexlify
from enum import Enum
copyrightNotice = """/*