Skip to content

Commit b878196

Browse files
authored
Merge pull request #7 from brianobot/dev
Dev
2 parents 98af88c + 3aa6c05 commit b878196

File tree

2 files changed

+67
-36
lines changed

2 files changed

+67
-36
lines changed

src/fastapi_gen8/main.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,53 +49,84 @@ def generate_default_project_details() -> dict[str, str | int | tuple]:
4949
"email": "brianobot9@gmail.com",
5050
"repository_link": "",
5151
"open_source_license": (1, [
52-
(1, "MIT"),
53-
(2, "BSD"),
54-
(3, "GPLv3"),
55-
(4, "Apache Software License 2.0"),
56-
(5, "Not open source"),
52+
"MIT",
53+
"BSD",
54+
"GPLv3",
55+
"Apache Software License 2.0",
56+
"Not open source",
5757
]),
5858
}
5959

60+
61+
class ProjectOptionConfig:
62+
"""
63+
Utility Functions for working with Project Detail Options.
64+
"""
65+
66+
@classmethod
67+
def get_option_at(cls, option: list[str], position: int) -> str:
68+
"""
69+
Since indexes are zero based and position are 1 based
70+
substracting 1 from each get the option at the current index
71+
"""
72+
return option[position - 1]
73+
74+
@classmethod
75+
def get_default_option(cls, default: tuple[int, list[str]]) -> str:
76+
"""
77+
Takes the whole default value, extracts the default index and extract the default value
78+
based on the default index
79+
"""
80+
default_index = default[0]
81+
return cls.get_option_at(default[1], default_index)
82+
6083

6184
def get_project_detail(
6285
attr: str,
63-
default: str | int | tuple[str, ...],
86+
default: str | tuple[int, list[str]],
6487
project_detail: dict[str, str | int | tuple]
65-
) -> str | int | tuple[str, ...]:
88+
) -> str | int:
6689
"""
6790
Get a project detail (attr) from the user via the command line interface, if nothing is provided
6891
the default value is used for the project detail value.
6992
"""
93+
94+
if "_" in attr:
95+
attr = attr.lower()
96+
97+
if attr == "slug_name":
98+
default = slugify(str(project_detail["name"]))
7099

71100
# Tuples Attrs means the detail have options, so process them differently
72-
if not isinstance(attr, tuple):
101+
if not isinstance(default, tuple):
73102
detail = input(f"Enter Project {attr} ['{default}']: ")
74103
else:
75104
count = 0
76105
detail = ""
77106
is_not_valid = True
78-
default_index = int(cast(tuple, project_detail[attr])[0]) - 1
79-
default = cast(tuple, project_detail[attr])[1][default_index][1]
107+
108+
default_value = ProjectOptionConfig.get_default_option(default)
80109

81110
while is_not_valid:
82-
options = cast(tuple[str, list[tuple[int, str]]], project_detail[attr])[1]
111+
options = default[1]
112+
print("Options: ", options)
113+
83114
print(f"Select {attr}:")
84-
for index, option in options:
115+
for index, option in enumerate(options, start=1):
85116
print(f"\t{index} - {option}")
86117

87-
prompt_msg = f"Choose from {', '.join(str(i) for i in range(index))}: [{cast(tuple, project_detail[attr])[0]}]: "
118+
prompt_msg = f"Choose from {', '.join(str(i) for i in range(1, index + 1))}: [{cast(tuple, project_detail[attr])[0]}]: "
88119
detail_index = input(prompt_msg)
89120

90121
if not detail_index or detail_index.isspace():
91-
detail = default
122+
detail = default_value
92123
is_not_valid = False
93124
elif not detail_index.isdigit():
94125
warning_print(f"Invalid Value {detail_index} for {attr}... Please Try Again!")
95-
elif int(detail_index) not in range(index + 1):
126+
elif int(detail_index) not in range(1, index + 1):
96127
warning_print(f"Invalid Value {detail_index} for {attr}... Please Try Again!")
97128
else:
98-
detail = detail = cast(tuple, project_detail[attr])[1][int(detail_index) - 1][1]
129+
detail = ProjectOptionConfig.get_option_at(options, int(detail_index))
99130
is_not_valid = False
100131

101132
count += 1
@@ -185,8 +216,8 @@ def generate_project_scaffold(project_detail: dict[str, str]):
185216

186217
# create and activate virtual environment
187218
subprocess.Popen(["python3", "-m", "venv", "venv"]).wait()
188-
subprocess.Popen(["bash", "-c", "source", "venv/bin/activate"]).wait()
189-
subprocess.Popen(["pip", "install", "-r", "requirements.txt"]).wait()
219+
subprocess.Popen(["bash", "-c", "source venv/bin/activate && pip install -r requirements.txt"]).wait()
220+
# subprocess.Popen(["pip", "install", "-r", "requirements.txt"]).wait()
190221

191222

192223
print("____________________________________________")
@@ -228,7 +259,8 @@ def main():
228259

229260
# Generate Projects with the Details Provided by the User
230261
generate_project_scaffold(cast(dict[str, str], project_details))
262+
231263

232-
264+
233265
if __name__ == "__main__":
234266
main()

src/fastapi_gen8/tests/test_main.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ def test_generate_default_project_details():
2424
@pytest.mark.parametrize(
2525
"attr,default_value,project_detail",
2626
[
27-
# ("name", "My Awesome FastAPI Project Test", main.generate_default_project_details()),
28-
# ("slug_name", "my_awesome_fastapi_project_test", main.generate_default_project_details()),
29-
# ("description", "FastAPI Project Description", main.generate_default_project_details()),
30-
# ("author(s)", "John Doe", main.generate_default_project_details()),
31-
# ("virtual_env_folder_name", "venv", main.generate_default_project_details()),
32-
# ("version", "0.0.1", main.generate_default_project_details()),
33-
# ("email", "brianobot9@gmail.com", main.generate_default_project_details()),
34-
# ("repository_url", "Default Name", main.generate_default_project_details()),
27+
("name", "My Awesome FastAPI Project Test", main.generate_default_project_details()),
28+
("slug_name", "my_awesome_fastapi_project", main.generate_default_project_details()),
29+
("description", "FastAPI Project Description", main.generate_default_project_details()),
30+
("author(s)", "John Doe", main.generate_default_project_details()),
31+
("virtual_env_folder_name", "venv", main.generate_default_project_details()),
32+
("version", "0.0.1", main.generate_default_project_details()),
33+
("email", "brianobot9@gmail.com", main.generate_default_project_details()),
34+
("repository_url", "Default Name", main.generate_default_project_details()),
3535
("open_source_license", (1, [
36-
(1, "MIT"),
37-
(2, "BSD"),
38-
(3, "GPLv3"),
39-
(4, "Apache Software License 2.0"),
40-
(5, "Not open source"),
36+
"MIT",
37+
"BSD",
38+
"GPLv3",
39+
"Apache Software License 2.0",
40+
"Not open source",
4141
]), main.generate_default_project_details()),
4242
]
4343
)
@@ -47,10 +47,9 @@ def test_get_project_detail(attr: str, default_value: str | int | tuple, project
4747
print("✅ Response: ", result)
4848
# assert None
4949
if isinstance(default_value, tuple):
50-
print("Default Value is a Tuple")
5150
default_value = cast(tuple, default_value)
52-
assert result == cast(list, default_value[1])[default_value[0]][1]
51+
assert result == cast(list, default_value[1])[default_value[0] - 1]
5352
else:
54-
print("Default Value is not a Tuple")
5553
assert result == default_value
56-
assert isinstance(result, str | int | tuple)
54+
assert isinstance(result, str | int | tuple)
55+

0 commit comments

Comments
 (0)