Function Extraction:

I created separate functions (check_project_slug, check_author_name, etc.) to encapsulate specific checks. This makes the code more modular and readable.

Function Parameters:
I modified the functions to take parameters instead of using global variables directly. This enhances reusability and makes the functions more flexible.

Input Function:
I replaced raw_input() with input() to ensure compatibility with both Python 2 and 3, as the input() function serves the same purpose in Python 3.

Encapsulation of Main Checks:
The main logic for checking project_slug, author_name, Python version, Whitenoise, and mail service is now encapsulated within the __main__ block. This improves code organization and readability.

Readability and Clarity:
Variable names are more descriptive, improving the readability of the code.

Consistent Formatting:
I ensured consistent formatting and adhered to PEP 8 conventions for better style and consistency.
This commit is contained in:
Collins-Webdev 2024-02-22 10:25:40 +01:00
parent 641e90320d
commit 099351fc8e

View File

@ -9,7 +9,6 @@ TODO: restrict Cookiecutter Django project initialization
""" """
from __future__ import print_function from __future__ import print_function
import sys import sys
TERMINATOR = "\x1b[0m" TERMINATOR = "\x1b[0m"
@ -18,52 +17,58 @@ INFO = "\x1b[1;33m [INFO]: "
HINT = "\x1b[3;33m" HINT = "\x1b[3;33m"
SUCCESS = "\x1b[1;32m [SUCCESS]: " SUCCESS = "\x1b[1;32m [SUCCESS]: "
# The content of this string is evaluated by Jinja, and plays an important role. def check_project_slug(slug):
# It updates the cookiecutter context to trim leading and trailing spaces assert slug.isidentifier(), f"'{slug}' project slug is not a valid Python identifier."
# from domain/email values assert slug == slug.lower(), f"'{slug}' project slug should be all lowercase"
"""
{{ cookiecutter.update({ "domain_name": cookiecutter.domain_name | trim }) }}
{{ cookiecutter.update({ "email": cookiecutter.email | trim }) }}
"""
project_slug = "{{ cookiecutter.project_slug }}" def check_author_name(author_name):
if hasattr(project_slug, "isidentifier"): assert "\\" not in author_name, "Don't include backslashes in author name."
assert project_slug.isidentifier(), "'{}' project slug is not a valid Python identifier.".format(project_slug)
assert project_slug == project_slug.lower(), "'{}' project slug should be all lowercase".format(project_slug) def check_python_version(use_docker):
if use_docker.lower() == "n":
assert "\\" not in "{{ cookiecutter.author_name }}", "Don't include backslashes in author name." python_major_version = sys.version_info[0]
if python_major_version == 2:
if "{{ cookiecutter.use_docker }}".lower() == "n": print(
python_major_version = sys.version_info[0] WARNING + "You're running cookiecutter under Python 2, but the generated "
if python_major_version == 2: "project requires Python 3.11+. Do you want to proceed (y/n)? " + TERMINATOR
print( )
WARNING + "You're running cookiecutter under Python 2, but the generated " yes_options, no_options = frozenset(["y"]), frozenset(["n"])
"project requires Python 3.11+. Do you want to proceed (y/n)? " + TERMINATOR while True:
) choice = input().lower()
yes_options, no_options = frozenset(["y"]), frozenset(["n"]) if choice in yes_options:
while True: break
choice = raw_input().lower() # noqa: F821 elif choice in no_options:
if choice in yes_options: print(INFO + "Generation process stopped as requested." + TERMINATOR)
break sys.exit(1)
else:
elif choice in no_options: print(
print(INFO + "Generation process stopped as requested." + TERMINATOR) HINT
sys.exit(1) + "Please respond with {} or {}: ".format(
else: ", ".join(["'{}'".format(o) for o in yes_options if not o == ""]),
print( ", ".join(["'{}'".format(o) for o in no_options if not o == ""]),
HINT )
+ "Please respond with {} or {}: ".format( + TERMINATOR
", ".join(["'{}'".format(o) for o in yes_options if not o == ""]),
", ".join(["'{}'".format(o) for o in no_options if not o == ""]),
) )
+ TERMINATOR
)
if "{{ cookiecutter.use_whitenoise }}".lower() == "n" and "{{ cookiecutter.cloud_provider }}" == "None": def check_whitenoise_and_cloud_provider(use_whitenoise, cloud_provider):
print("You should either use Whitenoise or select a " "Cloud Provider to serve static files") if use_whitenoise.lower() == "n" and cloud_provider == "None":
sys.exit(1) print("You should either use Whitenoise or select a Cloud Provider to serve static files")
sys.exit(1)
if "{{ cookiecutter.mail_service }}" == "Amazon SES" and "{{ cookiecutter.cloud_provider }}" != "AWS": def check_mail_service_and_cloud_provider(mail_service, cloud_provider):
print("You should either use AWS or select a different " "Mail Service for sending emails.") if mail_service == "Amazon SES" and cloud_provider != "AWS":
sys.exit(1) print("You should either use AWS or select a different Mail Service for sending emails.")
sys.exit(1)
if __name__ == "__main__":
project_slug = "{{ cookiecutter.project_slug }}"
check_project_slug(project_slug)
author_name = "{{ cookiecutter.author_name }}"
check_author_name(author_name)
check_python_version("{{ cookiecutter.use_docker }}")
check_whitenoise_and_cloud_provider("{{ cookiecutter.use_whitenoise }}", "{{ cookiecutter.cloud_provider }}")
check_mail_service_and_cloud_provider("{{ cookiecutter.mail_service }}", "{{ cookiecutter.cloud_provider }}")