-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
211 lines (185 loc) · 6.74 KB
/
seed.py
File metadata and controls
211 lines (185 loc) · 6.74 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import typer
import click
from rich import print
from typing import Optional
from app.db.init_db import InitDB
from app.utils.logger import logger
from app.utils.populate import Populate
class SeedCLI:
__app_name__ = "seedCLI"
__version__ = "0.1.0"
app = typer.Typer(add_completion=False)
@classmethod
def main(
cls,
ctx: typer.Context,
version: Optional[bool] = typer.Option(
None,
"--version",
"-v",
help="Show version and exit",
is_eager=True,
),
) -> None:
if version:
typer.echo(f"{cls.__app_name__} v{cls.__version__}")
raise typer.Exit()
@classmethod
def init_db(
cls,
recreate_database: bool = typer.Option(
False,
"--recreate-database",
"-rdb",
help="Recreate database",
),
create_tables: bool = typer.Option(
True,
"--no-create-tables",
"-nct",
help="Create tables",
),
force: bool = typer.Option(
False,
"--yes",
"-y",
help="Automatic yes to prompts and run non-interactively",
),
) -> None:
if force:
if recreate_database:
print("Recreating database.")
InitDB.run(delete_db=True, create_db=True)
logger.debug("Database recreated by force.")
else:
print("Creating database.")
InitDB.run(create_db=True)
logger.debug("Database created by force.")
if create_tables:
print("Creating tables.")
InitDB.run(create_table=True)
logger.debug("Tables created by force.")
else:
confirm = typer.confirm("Are you sure!!!, You wanna continue?")
if confirm:
if recreate_database:
print("Recreating database.")
InitDB.run(delete_db=True, create_db=True)
logger.debug("Database recreated with user consent.")
else:
print("Creating database.")
InitDB.run(create_db=True)
logger.debug("Database created with user consent")
if create_tables:
print("Creating tables.")
InitDB.run(create_table=True)
logger.debug("Tables created with user consent.")
else:
print("Operation canceled.")
@classmethod
def populate(
cls,
movie_count: int = typer.Option(
20,
"--movies",
"-m",
help="Number of movies to populate",
),
force: bool = typer.Option(
False,
"--yes",
"-y",
help="Automatic yes to prompts and run non-interactively",
),
) -> None:
if force:
print(f"Adding {movie_count} movies to database.")
Populate.run(movie_count=movie_count)
logger.debug(f"{movie_count} movies has been added to database by force.")
else:
confirm = typer.confirm("Are you sure!!!, You wanna continue?")
if confirm:
print(f"Adding {movie_count} movies to database.")
Populate.run(movie_count=movie_count)
logger.debug(
f"{movie_count} movies has been added to database with user consent."
)
else:
print("Operation canceled.")
@classmethod
def fullsetup(
cls,
recreate_database: bool = typer.Option(
False,
"--recreate-database",
"-rdb",
help="Recreate database",
),
movie_count: int = typer.Option(
20,
"--movies",
"-m",
help="Number of movies",
),
force: bool = typer.Option(
False,
"--yes",
"-y",
help="Automatic yes to prompts and run non-interactively",
),
) -> None:
if force:
if recreate_database:
print("Recreating database and tables.")
InitDB.run(delete_db=True, create_db=True, create_table=True)
logger.debug("Database and Tables recreated by force.")
print(f"Adding {movie_count} movies to database.")
Populate.run(movie_count=movie_count)
logger.debug(
f"{movie_count} movies has been added to database by force."
)
else:
print("Creating database and tables.")
InitDB.run(create_db=True, create_table=True)
logger.debug("Database and Tables created by force.")
print(f"Adding {movie_count} movies to database.")
Populate.run(movie_count=movie_count)
logger.debug(
f"{movie_count} movies has been added to database by force."
)
else:
confirm = typer.confirm("Are you sure!!!, You wanna continue?")
if confirm:
if recreate_database:
print("Recreating database and tables.")
InitDB.run(delete_db=True, create_db=True, create_table=True)
logger.debug("Database and Tables recreated with user consent.")
print(f"Adding {movie_count} movies to database.")
Populate.run(movie_count=movie_count)
logger.debug(
f"{movies} movies has been added to database with user consent."
)
else:
print("Recreating database and tables.")
InitDB.run(create_db=True, create_table=True)
logger.debug("Database and Tables created with user consent.")
print(f"Adding {movie_count} movies to database.")
Populate.run(movie_count=movie_count)
logger.debug(
f"{movie_count} movies has been added to database with user consent."
)
else:
print("Operation canceled.")
@classmethod
def register(cls) -> None:
# * calling the decorators returns callable function so -> decorator()(to_be_wrapped_function)
cls.app.callback(invoke_without_command=True)(cls.main)
cls.app.command()(cls.init_db)
cls.app.command()(cls.populate)
cls.app.command()(cls.fullsetup)
@classmethod
def run(cls) -> None:
cls.register()
cls.app()
if __name__ == "__main__":
SeedCLI.run()