forked from cookiecutter-flask/cookiecutter-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookiecutter_spec.py
100 lines (90 loc) · 3.22 KB
/
cookiecutter_spec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import argparse
from typing import List
import columbo
from cookiecutter.main import cookiecutter
from packaging.utils import canonicalize_name
def handle_cli_input():
parser = argparse.ArgumentParser(description="Generate a cookiecutter project")
parser.add_argument("template")
parser.add_argument("--no-input", default=False, action="store_true")
return parser.parse_args()
def _normalize_application_name(answers: columbo.Answers) -> str:
application_name = str(answers.get("project_name", "example_project"))
return application_name.lower().replace("-", "_").replace(" ", "_")
def validate_package_import_name(
answer: str, _: columbo.Answers
) -> columbo.ValidationResponse:
canonical_name = canonicalize_name(answer).replace("-", "_")
if not canonical_name == answer:
error_message = (
"Import names should follow PEP-8 naming conventions."
f" Did you mean {canonical_name}?"
)
return columbo.ValidationFailure(error=error_message)
if not answer.replace("_", "").isalpha():
error_message = (
"Import names may only contain alphabetical characters and underscores. "
"They may not contain spaces, numbers, or other characters."
)
return columbo.ValidationFailure(error=error_message)
return columbo.ValidationSuccess()
interactions: List[columbo.Interaction] = [
columbo.Echo("Please answer the following questions!"),
columbo.BasicQuestion(
"full_name",
"What is your name?",
default="First Last",
),
columbo.BasicQuestion(
"email",
lambda answers: f"What is {answers['full_name']}'s email address?",
default="[email protected]",
),
columbo.BasicQuestion(
"github_username",
lambda answers: f"What is {answers['full_name']}'s github username?",
default="yourGithubUsername",
),
columbo.BasicQuestion(
"project_name",
"What is the name of your project?",
default="My Flask App",
),
columbo.BasicQuestion(
"app_name",
"What will the package import name be?\nThis will be the name used in python code to import from the module",
default=_normalize_application_name,
validator=validate_package_import_name,
),
columbo.BasicQuestion(
"project_short_description",
"Provide a short description for the project.",
default="A flasky app.",
),
columbo.Confirm(
"use_pipenv",
"Should this project use pipenv?",
default=False,
),
columbo.Choice(
"python_version",
"Which version of Python will this application use?",
options=["3.12", "3.11", "3.10", "3.9"],
default="3.12",
),
columbo.Choice(
"node_version",
"Which version of Node will this application use?",
options=["20", "18"],
default="20",
),
columbo.Confirm(
"use_heroku",
"Will this project be deployed using heroku?",
default=False,
),
]
if __name__ == "__main__":
args = handle_cli_input()
answers = columbo.get_answers(interactions, no_user_input=args.no_input)
cookiecutter(args.template, no_input=True, extra_context=answers)