-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
141 lines (101 loc) · 2.97 KB
/
manage.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
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
import os
import click
from pathlib import Path
TEMPLATE_CONTROLLER = """from fastapi import APIRouter
from src.schemas import {capitalize_model_class}
{model_class}_router = APIRouter()
@{model_class}_router.get("/{{model_class}}")
async def get_handler({model_class}: {capitalize_model_class}):
...
@{model_class}_router.post("/")
async def post_handler({model_class}: {capitalize_model_class}):
...
@{model_class}_router.put("/{{model_class}}")
async def put_handler({model_class}: {capitalize_model_class}):
...
@{model_class}_router.patch("/{{model_class}}")
async def patch_handler({model_class}: {capitalize_model_class}):
...
@{model_class}_router.delete("/{{model_class}}")
async def delete_handler({model_class}: {capitalize_model_class}):
...
"""
TEMPLATE_SERVICE = """from abc import ABC, abstractmethod
class {capitalize_model_class}ServiceI(ABC):
...
class {capitalize_model_class}Service({capitalize_model_class}ServiceI):
...
"""
TEMPLATE_GATEWAY = """from abc import ABC, abstractmethod
class {capitalize_model_class}GatewayI(ABC):
...
class {capitalize_model_class}Gateway({capitalize_model_class}GatewayI):
...
"""
TEMPLATE_SCHEMA = """from pydantic import BaseModel
class {capitalize_model_class}(BaseModel):
...
"""
def write_file(path, content):
with open(path, 'w') as f:
f.write(content)
def create_file(directory: str, name: str, template: str) -> None:
write_file(
Path(f'src/{directory}') / f'{name}.py',
template
)
@click.group()
def cli():
"""Manage your Python web application."""
pass
@cli.command()
@click.argument('name')
def new_controller(name: str):
"""Create a new controller."""
create_file(
name=name,
directory='controller',
template=TEMPLATE_CONTROLLER.format(
model_class=name,
capitalize_model_class=name.capitalize()
)
)
print(f'controller {name} created successful!')
@cli.command()
@click.argument('name')
def new_service(name: str):
"""Create a new service."""
create_file(
name=name,
directory='service',
template=TEMPLATE_SERVICE.format(
capitalize_model_class=name.capitalize()
)
)
print(f'service {name} created successful!')
@cli.command()
@click.argument('name')
def new_gateway(name: str):
"""Create a new gateway."""
create_file(
name=name,
directory='gateway',
template=TEMPLATE_GATEWAY.format(
capitalize_model_class=name.capitalize()
)
)
print(f'gateway {name} created successful!')
@cli.command()
@click.argument('name')
def new_schema(name: str):
"""Create a new schema."""
create_file(
name=name,
directory='schema',
template=TEMPLATE_SCHEMA.format(
capitalize_model_class=name.capitalize()
)
)
print(f'schema {name} created successful!')
if __name__ == '__main__':
cli()