-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfast_creator.py
97 lines (67 loc) · 2.96 KB
/
fast_creator.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
import typer
import os
def main(project_name: str,
routers: str = typer.Option(default='', help='Router names to create, write every router name with spaces'),
models: str = typer.Option(default='', help='Model names to create, write every router name with spaces')):
typer.confirm(f'Project name: {project_name} \n routers: {routers} \n models: {models}', default=True, abort=True)
# Create the base directory
os.mkdir(project_name)
# Create high level main file
create_base_main(project_name)
# Create the app package
os.mkdir(f'{project_name}/app')
open(f'{project_name}/app/__init__.py', 'w').close()
# If there are model classes to create then create these classes as well
if models != '':
create_models(project_name, models.split())
# If there are routers to create then create these as well
if routers != '':
create_routers(project_name, routers.split())
# Create the base fastapi main file. FastAPI object will be created here
create_main(project_name, routers.split())
create_docker_file(project_name)
def create_docker_file(project_name:str):
with open(f'{project_name}/Dockerfile','w',encoding='utf8') as file:
file.write('FROM tiangolo/uvicorn-gunicorn-fastapi:latest\n')
file.write('COPY ./app /app/app')
def create_base_main(project_name: str):
main_file = """import uvicorn
from app.main import app
if __name__ == '__main__':
uvicorn.run(app)"""
with open(f'{project_name}/main.py', 'w', encoding='utf8') as file:
file.write(main_file)
def create_main(project_name: str, router_names: []):
open(f'{project_name}/app/main.py', 'w').close()
lines = [
'from fastapi import FastAPI\n',
]
for router in router_names:
lines.append(f'from .routers.{router} import {router}\n')
lines.append('\napp = FastAPI()\n\n')
for router in router_names:
lines.append(f"app.include_router({router},prefix='/{router}')\n")
lines.append("""
@app.get('/')
def hello():
return 'Hello World!'""")
with open(f'{project_name}/app/main.py', 'w', encoding='utf8') as file:
file.writelines(lines)
def create_routers(project_name: str, router_names: []):
os.mkdir(f'{project_name}/app/routers')
open(f'{project_name}/app/routers/__init__.py', 'w').close()
for router in router_names:
with open(f'{project_name}/app/routers/{router}.py', 'w', encoding='utf8') as file:
file.write(f"""from fastapi import APIRouter
{router} = APIRouter()
""")
def create_models(project_name: str, model_names: []):
os.mkdir(f'{project_name}/app/models')
open(f'{project_name}/app/models/__init__.py', 'w').close()
for model in model_names:
with open(f'{project_name}/app/models/{model}.py', 'w', encoding='utf8') as file:
file.write(f"""from pydantic import BaseModel
class {model.capitalize()}(BaseModel):
pass""")
if __name__ == "__main__":
typer.run(main)