Skip to content

Commit 6c4decf

Browse files
pdgendtkartben
authored andcommitted
scripts: west_commands: zspdx: cmakefileapijson: Fix linter issues
Fix issues reported by ruff. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
1 parent 2913bfe commit 6c4decf

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,6 @@
11901190
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
11911191
"UP032", # https://docs.astral.sh/ruff/rules/f-string
11921192
]
1193-
"./scripts/west_commands/zspdx/cmakefileapijson.py" = [
1194-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
1195-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
1196-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
1197-
"SIM116", # https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-lookup
1198-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
1199-
]
12001193
"./scripts/west_commands/zspdx/datatypes.py" = [
12011194
"UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters
12021195
]

scripts/west_commands/zspdx/cmakefileapijson.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,29 @@
99

1010
import zspdx.cmakefileapi
1111

12+
1213
def parseReply(replyIndexPath):
1314
replyDir, _ = os.path.split(replyIndexPath)
1415

1516
# first we need to find the codemodel reply file
1617
try:
17-
with open(replyIndexPath, 'r') as indexFile:
18+
with open(replyIndexPath) as indexFile:
1819
js = json.load(indexFile)
1920

2021
# get reply object
2122
reply_dict = js.get("reply", {})
2223
if reply_dict == {}:
23-
log.err(f"no \"reply\" field found in index file")
24+
log.err('no "reply" field found in index file')
2425
return None
2526
# get codemodel object
2627
cm_dict = reply_dict.get("codemodel-v2", {})
2728
if cm_dict == {}:
28-
log.err(f"no \"codemodel-v2\" field found in \"reply\" object in index file")
29+
log.err('no "codemodel-v2" field found in "reply" object in index file')
2930
return None
3031
# and get codemodel filename
3132
jsonFile = cm_dict.get("jsonFile", "")
3233
if jsonFile == "":
33-
log.err(f"no \"jsonFile\" field found in \"codemodel-v2\" object in index file")
34+
log.err('no "jsonFile" field found in "codemodel-v2" object in index file')
3435
return None
3536

3637
return parseCodemodel(replyDir, jsonFile)
@@ -46,23 +47,26 @@ def parseCodemodel(replyDir, codemodelFile):
4647
codemodelPath = os.path.join(replyDir, codemodelFile)
4748

4849
try:
49-
with open(codemodelPath, 'r') as cmFile:
50+
with open(codemodelPath) as cmFile:
5051
js = json.load(cmFile)
5152

5253
cm = zspdx.cmakefileapi.Codemodel()
5354

5455
# for correctness, check kind and version
5556
kind = js.get("kind", "")
5657
if kind != "codemodel":
57-
log.err(f"Error loading CMake API reply: expected \"kind\":\"codemodel\" in {codemodelPath}, got {kind}")
58+
log.err('Error loading CMake API reply: expected "kind":"codemodel" '
59+
f'in {codemodelPath}, got {kind}')
5860
return None
5961
version = js.get("version", {})
6062
versionMajor = version.get("major", -1)
6163
if versionMajor != 2:
6264
if versionMajor == -1:
63-
log.err(f"Error loading CMake API reply: expected major version 2 in {codemodelPath}, no version found")
65+
log.err("Error loading CMake API reply: expected major version 2 "
66+
f"in {codemodelPath}, no version found")
6467
return None
65-
log.err(f"Error loading CMake API reply: expected major version 2 in {codemodelPath}, got {versionMajor}")
68+
log.err("Error loading CMake API reply: expected major version 2 "
69+
f"in {codemodelPath}, got {versionMajor}")
6670
return None
6771

6872
# get paths
@@ -143,7 +147,7 @@ def parseConfig(cfg_dict, replyDir):
143147

144148
def parseTarget(targetPath):
145149
try:
146-
with open(targetPath, 'r') as targetFile:
150+
with open(targetPath) as targetFile:
147151
js = json.load(targetFile)
148152

149153
target = zspdx.cmakefileapi.Target()
@@ -191,20 +195,14 @@ def parseTarget(targetPath):
191195
return None
192196

193197
def parseTargetType(targetType):
194-
if targetType == "EXECUTABLE":
195-
return zspdx.cmakefileapi.TargetType.EXECUTABLE
196-
elif targetType == "STATIC_LIBRARY":
197-
return zspdx.cmakefileapi.TargetType.STATIC_LIBRARY
198-
elif targetType == "SHARED_LIBRARY":
199-
return zspdx.cmakefileapi.TargetType.SHARED_LIBRARY
200-
elif targetType == "MODULE_LIBRARY":
201-
return zspdx.cmakefileapi.TargetType.MODULE_LIBRARY
202-
elif targetType == "OBJECT_LIBRARY":
203-
return zspdx.cmakefileapi.TargetType.OBJECT_LIBRARY
204-
elif targetType == "UTILITY":
205-
return zspdx.cmakefileapi.TargetType.UTILITY
206-
else:
207-
return zspdx.cmakefileapi.TargetType.UNKNOWN
198+
return {
199+
"EXECUTABLE": zspdx.cmakefileapi.TargetType.EXECUTABLE,
200+
"STATIC_LIBRARY": zspdx.cmakefileapi.TargetType.STATIC_LIBRARY,
201+
"SHARED_LIBRARY": zspdx.cmakefileapi.TargetType.SHARED_LIBRARY,
202+
"MODULE_LIBRARY": zspdx.cmakefileapi.TargetType.MODULE_LIBRARY,
203+
"OBJECT_LIBRARY": zspdx.cmakefileapi.TargetType.OBJECT_LIBRARY,
204+
"UTILITY": zspdx.cmakefileapi.TargetType.UTILITY,
205+
}.get(targetType, zspdx.cmakefileapi.TargetType.UNKNOWN)
208206

209207
def parseTargetInstall(target, js):
210208
install_dict = js.get("install", {})

0 commit comments

Comments
 (0)