Skip to content

Commit

Permalink
Merge pull request #125 from at-gmbh/fstring
Browse files Browse the repository at this point in the history
replace str.format with f-strings
  • Loading branch information
klamann authored Dec 22, 2023
2 parents 2dfb503 + 3bbc737 commit 66942b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
22 changes: 11 additions & 11 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
warnings.simplefilter("ignore", category=DeprecationWarning)
# check Python version (3.8 or higher)
if StrictVersion(platform.python_version()) < StrictVersion("3.8.0"):
print("ERROR: You are using Python {}, but Python 3.8 or higher is required "
"to use this template".format(platform.python_version()))
print(f"ERROR: You are using Python {platform.python_version()},",
"but Python 3.8 or higher is required to use this template")
sys.exit(1)
# check cookiecutter version (1.7.2 or higher)
if StrictVersion(cookiecutter.__version__) < StrictVersion('1.7.2'):
print("ERROR: You are using cookiecutter {}, but cookiecutter 1.7.2 or higher is required "
"to use this template".format(cookiecutter.__version__))
print(f"ERROR: You are using cookiecutter {cookiecutter.__version__}"
"but cookiecutter 1.7.2 or higher is required to use this template")
sys.exit(1)

# check the slug and module name
Expand All @@ -39,20 +39,20 @@

invalid_inputs = list()
if not re.match(SLUG_REGEX, project_slug):
print("ERROR: {} is not a valid slug! It may only consist of numbers and letters of the "
"english alphabet, begin with a letter, and must use dashes instead of whitespace."
.format(project_slug))
print(f"ERROR: {project_slug} is not a valid slug! "
"It may only consist of numbers and letters of the english alphabet, "
"begin with a letter, and must use dashes instead of whitespace.")
invalid_inputs.append("project_slug")

if not re.match(MODULE_REGEX, module_name):
print("ERROR: {} is not a valid Python module name! "
print(f"ERROR: {module_name} is not a valid Python module name! "
"See https://www.python.org/dev/peps/pep-0008/#package-and-module-names "
"for naming standards.".format(module_name))
"for naming standards.")
invalid_inputs.append("module_name")

if invalid_inputs:
print("\nYou have entered invalid configuration values for: "
"{}".format(", ".join(invalid_inputs)))
print("\nYou have entered invalid configuration values for:",
", ".join(invalid_inputs))
print("\nPlease fix your inputs in ~/.cookiecutter_replay/at-python-template.json")
print("After that, rerun the template with: cookiecutter . --replay\n")
sys.exit(1)
4 changes: 2 additions & 2 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def list_files(base_dir, indent=4):
for base, dirs, files in os.walk(base_dir):
level = len(Path(base).relative_to(base_dir).parents)
space = ' ' * indent * level
print('{}{}/'.format(space, os.path.basename(base)))
print(f'{space}{os.path.basename(base)}/')
space_sub = ' ' * indent * (level + 1)
for f in files:
print('{}{}'.format(space_sub, f))
print(f'{space_sub}{f}')


def check_project(
Expand Down
23 changes: 11 additions & 12 deletions tests/version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
return_code = 1
try:
p = subprocess.Popen(
[sys.executable, '-m', 'cookiecutter', '--no-input', '-o', '"' + temp_dir + '"', '.'],
[sys.executable, '-m', 'cookiecutter', '--no-input', '-o', f'"{temp_dir}"', '.'],
cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
return_code = p.returncode
Expand All @@ -24,21 +24,20 @@

# handle possible issues & give proper return codes
if b'Python 3.8 or higher' in stdout or b'successfully created' in stdout:
status = {True: "failed", False: "succeded"}
if actual_fail == expect_fail:
print("Python {} {} as expected".format(
platform.python_version(),
"failed" if expect_fail else "succeeded"))
print(f"Python {platform.python_version()} {status[expect_fail]} as expected")
else:
print("Python {} should have {}, but actually {}".format(
platform.python_version(),
"failed" if expect_fail else "succeeded",
"failed" if actual_fail else "succeeded"))
print(
f"Python {platform.python_version()} should have {status[expect_fail]},",
f"but actually {status[actual_fail]}",
)
sys.exit(return_code)
elif b'SyntaxError' in stderr:
print("got a syntax error in pre_gen_project.py:\n" + str(stderr))
print(f"got a syntax error in pre_gen_project.py:\n{stderr}")
sys.exit(return_code)
else:
print("unexpected error, code" + str(return_code))
print("stderr:" + str(stderr))
print("stdout:" + str(stdout))
print(f"unexpected error, code {return_code}")
print(f"stderr: {stderr}")
print(f"stdout: {stdout}")
sys.exit(return_code)

0 comments on commit 66942b2

Please sign in to comment.