Skip to content

Commit 2cd1760

Browse files
committed
Switch .format() to f-strings and apply various flake8 recommendations
1 parent 5363262 commit 2cd1760

12 files changed

Lines changed: 106 additions & 219 deletions

pre_commit_hooks/check_autopkg_recipe_list.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def main(argv=None):
4848
with open(filename, "rb") as openfile:
4949
recipe_list = plistlib.load(openfile).get("recipes")
5050
except (ExpatError, ValueError) as err:
51-
print("{}: plist parsing error: {}".format(filename, err))
51+
print(f"{filename}: plist parsing error: {err}")
5252
retval = 1
5353
elif filename.endswith((".yaml", ".yml")):
5454
# AutoPkg does not support YAML recipe lists, but AutoPkg users
@@ -57,7 +57,7 @@ def main(argv=None):
5757
with open(filename, "r", encoding="utf-8") as openfile:
5858
recipe_list = yaml.load(openfile)
5959
except Exception as err:
60-
print("{}: yaml parsing error: {}".format(filename, err))
60+
print(f"{filename}: yaml parsing error: {err}")
6161
retval = 1
6262
elif filename.endswith(".json"):
6363
# AutoPkg does not support JSON recipe lists, but AutoPkg users
@@ -66,11 +66,11 @@ def main(argv=None):
6666
with open(filename, "r", encoding="utf-8") as openfile:
6767
recipe_list = json.load(openfile)
6868
except Exception as err:
69-
print("{}: json parsing error: {}".format(filename, err))
69+
print(f"{filename}: json parsing error: {err}")
7070
retval = 1
7171

7272
if not recipe_list or not isinstance(recipe_list, list):
73-
print("{}: invalid recipe list".format(filename))
73+
print(f"{filename}: invalid recipe list")
7474
retval = 1
7575
else:
7676
if any((".munki" in recipe for recipe in recipe_list)):

pre_commit_hooks/check_autopkg_recipes.py

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,13 @@ def validate_comments(filename, strict):
105105
if "<!--" in recipe_text and "-->" in recipe_text:
106106
if strict:
107107
print(
108-
"{}: Convert from <!-- --> style comments "
109-
"to a Comment key.".format(filename)
108+
f"{filename}: Convert from <!-- --> style comments to a Comment key."
110109
)
111110
passed = False
112111
else:
113112
print(
114-
"{}: WARNING: Recommend converting from <!-- --> style comments "
115-
"to a Comment key.".format(filename)
113+
f"{filename}: WARNING: Recommend converting from <!-- --> style comments "
114+
"to a Comment key."
116115
)
117116

118117
return passed
@@ -126,9 +125,7 @@ def validate_processor_keys(process, filename):
126125
if missing_processor_keys:
127126
for missing_proc in missing_processor_keys:
128127
print(
129-
'{}: Item in processor array is missing "Processor" key:\n{}'.format(
130-
filename, missing_proc
131-
)
128+
f'{filename}: Item in processor array is missing "Processor" key:\n{missing_proc}'
132129
)
133130
passed = False
134131

@@ -159,14 +156,12 @@ def validate_endofcheckphase(process, filename):
159156
)
160157
if endofcheck_idx is None:
161158
print(
162-
"{}: Contains a download processor, but no EndOfCheckPhase "
163-
"processor.".format(filename)
159+
f"{filename}: Contains a download processor, but no EndOfCheckPhase processor."
164160
)
165161
passed = False
166162
elif endofcheck_idx < downloader_idx:
167163
print(
168-
"{}: EndOfCheckPhase typically goes after a download processor, "
169-
"not before.".format(filename)
164+
f"{filename}: EndOfCheckPhase typically goes after a download processor, not before."
170165
)
171166
passed = False
172167

@@ -223,8 +218,7 @@ def validate_minimumversion(process, min_vers, ignore_min_vers_before, filename)
223218
if proc in [x.get("Processor") for x in process]:
224219
if Version(min_vers) < Version(proc_min_versions[proc]):
225220
print(
226-
"{}: {} processor requires minimum AutoPkg "
227-
"version {}".format(filename, proc, proc_min_versions[proc])
221+
f"{filename}: {proc} processor requires minimum AutoPkg version {proc_min_versions[proc]}"
228222
)
229223
passed = False
230224

@@ -241,8 +235,7 @@ def validate_no_deprecated_procs(process, filename):
241235
for proc in process:
242236
if proc.get("Processor") in deprecated_procs:
243237
print(
244-
"{}: WARNING: Deprecated processor {} "
245-
"is used.".format(filename, proc.get("Processor"))
238+
f'{filename}: WARNING: Deprecated processor {proc.get("Processor")} is used.'
246239
)
247240

248241
return passed
@@ -259,10 +252,8 @@ def validate_no_superclass_procs(process, filename):
259252
for proc in process:
260253
if proc.get("Processor") in superclass_procs:
261254
print(
262-
"{}: WARNING: The processor {} is intended to be used "
263-
"by other processors, not used directly in recipes.".format(
264-
filename, proc.get("Processor")
265-
)
255+
f"{filename}: WARNING: The processor {proc.get('Processor')} is intended to be used "
256+
"by other processors, not used directly in recipes."
266257
)
267258

268259
return passed
@@ -324,9 +315,7 @@ def validate_jamf_processor_order(process, filename):
324315
# subst = "%" + input_var + "%"
325316
# if subst not in recipe_text:
326317
# print(
327-
# "{}: WARNING: Input variable {} not referenced in recipe.".format(
328-
# filename, input_var
329-
# )
318+
# f"{filename}: WARNING: Input variable {input_var} not referenced in recipe."
330319
# )
331320

332321
# return passed
@@ -353,8 +342,8 @@ def validate_no_var_in_app_path(process, filename):
353342
for _, argvalue in proc["Arguments"].items():
354343
if isinstance(argvalue, str) and "%NAME%.app" in argvalue:
355344
print(
356-
"{}: Use actual app name instead of %NAME%.app in {} "
357-
"processor argument.".format(filename, proc.get("Processor"))
345+
f"{filename}: Use actual app name instead of %NAME%.app in {proc.get('Processor')} "
346+
"processor argument."
358347
)
359348
passed = False
360349

@@ -405,13 +394,13 @@ def validate_proc_type_conventions(process, filename):
405394
passed = True
406395
processors = [x.get("Processor") for x in process]
407396
for recipe_type in proc_type_conventions:
408-
type_hint = ".{}.".format(recipe_type)
397+
type_hint = f".{recipe_type}."
409398
if type_hint not in filename:
410399
for processor in processors:
411400
if processor in proc_type_conventions[recipe_type]:
412401
print(
413-
"{}: Processor {} is not conventional for this "
414-
"recipe type.".format(filename, processor)
402+
f"{filename}: Processor {processor} is not conventional for this "
403+
"recipe type."
415404
)
416405
passed = False
417406

@@ -445,7 +434,7 @@ def validate_required_proc_for_types(process, filename):
445434
processors = [x.get("Processor") for x in process]
446435
for recipe_type in required_proc_for_type:
447436
req_procs = required_proc_for_type[recipe_type]
448-
type_hint = ".{}.".format(recipe_type)
437+
type_hint = f".{recipe_type}."
449438
if type_hint in filename:
450439
if recipe_type == "pkg" and processors == []:
451440
# OK for pkg recipes to have an empty process list, as long as
@@ -455,13 +444,13 @@ def validate_required_proc_for_types(process, filename):
455444
if not any([x in processors for x in req_procs]):
456445
if len(req_procs) == 1:
457446
print(
458-
"{}: Recipe type {} should contain processor "
459-
"{}.".format(filename, recipe_type, req_procs[0])
447+
f"{filename}: Recipe type {recipe_type} should contain processor "
448+
f"{req_procs[0]}."
460449
)
461450
else:
462451
print(
463-
"{}: Recipe type {} should contain one of these "
464-
"processors: {}.".format(filename, recipe_type, req_procs)
452+
f"{filename}: Recipe type {recipe_type} should contain one of these "
453+
f"processors: {req_procs}."
465454
)
466455
passed = False
467456

@@ -495,23 +484,14 @@ def validate_proc_args(process, filename):
495484

496485
if not core_procs[proc["Processor"]]:
497486
print(
498-
"{}: Unknown argument {} for processor {}, "
499-
"which does not accept any arguments.".format(
500-
filename,
501-
arg,
502-
proc["Processor"],
503-
)
487+
f"{filename}: Unknown argument {arg} for processor {proc['Processor']}, "
488+
"which does not accept any arguments."
504489
)
505490
passed = False
506491
elif arg not in core_procs[proc["Processor"]]:
507492
print(
508-
"{}: Unknown argument {} for processor {}. "
509-
"Allowed arguments are: {}".format(
510-
filename,
511-
arg,
512-
proc["Processor"],
513-
", ".join(core_procs[proc["Processor"]]),
514-
)
493+
f"{filename}: Unknown argument {arg} for processor {proc['Processor']}. Allowed arguments are: "
494+
+ ", ".join(core_procs[proc["Processor"]])
515495
)
516496
passed = False
517497

@@ -550,9 +530,7 @@ def main(argv=None):
550530
# Ensure the recipe identifier isn't duplicated.
551531
if recipe["Identifier"] in seen_identifiers:
552532
print(
553-
'{}: Identifier "{}" is shared by another recipe in this repo.'.format(
554-
filename, recipe["Identifier"]
555-
)
533+
f'{filename}: Identifier "{recipe["Identifier"]}" is shared by another recipe in this repo.'
556534
)
557535
retval = 1
558536
else:
@@ -566,10 +544,7 @@ def main(argv=None):
566544
if not validate_recipe_prefix(recipe, filename, args.recipe_prefix):
567545
retval = 1
568546
if recipe["Identifier"] == recipe.get("ParentRecipe"):
569-
print(
570-
"{}: Identifier and ParentRecipe should not "
571-
"be the same.".format(filename)
572-
)
547+
print(f"{filename}: Identifier and ParentRecipe should not be the same.")
573548
retval = 1
574549

575550
# Validate that all input variables are used.
@@ -601,9 +576,7 @@ def main(argv=None):
601576
for os_vers_key in os_vers_corrections:
602577
if os_vers_key in input_key["pkginfo"]:
603578
print(
604-
"{}: You used {} when you probably meant {}.".format(
605-
filename, os_vers_key, os_vers_corrections[os_vers_key]
606-
)
579+
f"{filename}: You used {os_vers_key} when you probably meant {os_vers_corrections[os_vers_key]}."
607580
)
608581
retval = 1
609582

pre_commit_hooks/check_jamf_json_manifests.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,8 @@ def validate_key_types(name, manifest, filename):
7171
if manifest_key in manifest:
7272
if not isinstance(manifest[manifest_key], expected_type):
7373
print(
74-
"{}: {} key {} should be type {}, not type {}".format(
75-
filename,
76-
name,
77-
manifest_key,
78-
expected_type,
79-
type(manifest[manifest_key]),
80-
)
74+
f"{filename}: {name} key {manifest_key} should be type "
75+
f"{expected_type}, not type {type(manifest[manifest_key])}"
8176
)
8277
passed = False
8378

@@ -98,7 +93,7 @@ def validate_type(name, property, filename):
9893
break
9994

10095
if type_found not in MANIFEST_TYPES:
101-
print('{}: Unexpected "{}" type "{}"'.format(filename, name, type_found))
96+
print(f'{filename}: Unexpected "{name}" type "{type_found}"')
10297
passed = False
10398

10499
return passed, type_found
@@ -121,55 +116,42 @@ def validate_list_item_types(name, manifest, filename):
121116
desired_types = [MANIFEST_LIST_TYPES[name]]
122117
if actual_type not in desired_types:
123118
print(
124-
'{}: "{}" items should be {}, not {}'.format(
125-
filename, name, MANIFEST_LIST_TYPES[name], actual_type
126-
)
119+
f'{filename}: "{name}" items should be {MANIFEST_LIST_TYPES[name]}, not {actual_type}'
127120
)
128121
passed = False
129122

130123
return passed
131124

132125

133-
def validate_default(name, property, type_found, filename):
126+
def validate_default(name, prop, type_found, filename):
134127
"""Ensure that default values have the expected type."""
135128
passed = True
136129

137130
for test_key in ("default",):
138-
if test_key in property:
139-
if type(property[test_key]) == datetime:
131+
if test_key in prop:
132+
if isinstance(prop[test_key], datetime):
140133
actual_type = str
141134
else:
142-
actual_type = type(property[test_key])
135+
actual_type = type(prop[test_key])
143136
if actual_type != PLIST_TYPES.get(type_found):
144137
print(
145-
"{}: {} value for {} should be {}, not {}".format(
146-
filename,
147-
test_key,
148-
name,
149-
PLIST_TYPES.get(type_found),
150-
type(property[test_key]),
151-
)
138+
f"{filename}: {test_key} value for {name} should be {PLIST_TYPES.get(type_found)}, not {type(prop[test_key])}"
152139
)
153140
passed = False
154141

155142
return passed
156143

157144

158-
def validate_urls(name, property, filename):
145+
def validate_urls(name, prop, filename):
159146
"""Ensure that URL values are actual URLs."""
160147
passed = True
161148

162149
url_keys = ("pfm_app_url", "pfm_documentation_url")
163150
for url_key in url_keys:
164-
if url_key in property:
165-
if not property[url_key].startswith("http"):
151+
if url_key in prop:
152+
if not prop[url_key].startswith("http"):
166153
print(
167-
"{}: {} {} value doesn't look like a URL: {}".format(
168-
filename,
169-
name,
170-
url_key,
171-
property[url_key],
172-
)
154+
f"{filename}: {name} {url_key} value doesn't look like a URL: {prop[url_key]}"
173155
)
174156
passed = False
175157

@@ -228,7 +210,7 @@ def main(argv=None):
228210
with open(filename, "rb") as openfile:
229211
manifest = json.load(openfile)
230212
except json.decoder.JSONDecodeError as err:
231-
print("{}: json parsing error: {}".format(filename, err))
213+
print(f"{filename}: json parsing error: {err}")
232214
retval = 1
233215
break # No need to continue checking this file
234216

pre_commit_hooks/check_jamf_profiles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def main(argv=None):
2828
for filename in args.filenames:
2929
try:
3030
with open(filename, "rb") as openfile:
31-
profile = plistlib.load(openfile)
31+
_ = plistlib.load(openfile)
3232
except (ExpatError, ValueError) as err:
33-
print("{}: plist parsing error: {}".format(filename, err))
33+
print(f"{filename}: plist parsing error: {err}")
3434
retval = 1
3535

3636
return retval

0 commit comments

Comments
 (0)