Skip to content

Commit 5b277ea

Browse files
committed
[FIX] moved backend operations to backend.py module
1 parent f3c7c9f commit 5b277ea

File tree

2 files changed

+94
-77
lines changed

2 files changed

+94
-77
lines changed

src/fastapi_fastkit/backend.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# --------------------------------------------------------------------------
2+
# The Module defines backend operations for FastAPI-fastkit CLI.
3+
#
4+
# @author bnbong bbbong9@gmail.com
5+
# --------------------------------------------------------------------------
6+
import re
7+
import os
8+
import click
9+
10+
from typing import Any, Union
11+
12+
from logging import getLogger
13+
14+
from click.core import Context
15+
16+
from fastapi_fastkit.core.exceptions import TemplateExceptions
17+
18+
19+
logger = getLogger(__name__)
20+
21+
REGEX = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"
22+
23+
24+
def validate_email(ctx: Context, param: Any, value: Any) -> Any:
25+
"""
26+
Check if the provided email is in a valid format.
27+
This will recursively loop until a valid email input entry is given.
28+
29+
:param ctx: context of passing configurations (NOT specify it at CLI)
30+
:type ctx: <Object click.Context>
31+
:param param: parameters from CLI
32+
:param value: values from CLI
33+
:return:
34+
"""
35+
try:
36+
if not re.match(REGEX, value):
37+
raise ValueError(value)
38+
else:
39+
return value
40+
except ValueError as e:
41+
click.echo("Incorrect email address given: {}".format(e))
42+
value = click.prompt(param.prompt)
43+
return validate_email(ctx, param, value)
44+
45+
46+
def inject_project_metadata(
47+
target_dir: str,
48+
project_name: str,
49+
author: str,
50+
author_email: str,
51+
description: str,
52+
) -> None:
53+
"""
54+
Inject metadata into the main.py and setup.py files.
55+
56+
:param target_dir: Directory for the new project to deploy
57+
:param project_name: new project name
58+
:param author: cli username
59+
:param author_email: cli user email
60+
:param description: new project description
61+
"""
62+
main_py_path = os.path.join(target_dir, "main.py")
63+
setup_py_path = os.path.join(target_dir, "setup.py")
64+
65+
try:
66+
with open(main_py_path, "r+") as f:
67+
content = f.read()
68+
content = content.replace("app_title", f'"{project_name}"')
69+
content = content.replace("app_description", f'"{description}"')
70+
f.seek(0)
71+
f.write(content)
72+
f.truncate()
73+
74+
with open(setup_py_path, "r+") as f:
75+
content = f.read()
76+
content = content.replace("<project_name>", project_name, 1)
77+
content = content.replace("<description>", description, 1)
78+
content = content.replace("<author>", author, 1)
79+
content = content.replace("<author_email>", author_email, 1)
80+
f.seek(0)
81+
f.write(content)
82+
f.truncate()
83+
except Exception as e:
84+
click.echo(e)
85+
raise TemplateExceptions("ERROR : Having some errors with injecting metadata")
86+
87+
88+
def read_template_stack() -> Union[list, None]:
89+
pass

src/fastapi_fastkit/cli.py

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
#
55
# @author bnbong bbbong9@gmail.com
66
# --------------------------------------------------------------------------
7-
import re
87
import os
98
import click
109

11-
from typing import Union, Any
10+
from typing import Union
1211

1312
from logging import getLogger
1413

@@ -18,89 +17,17 @@
1817
from rich.panel import Panel
1918

2019
from . import __version__
20+
from .backend import validate_email, inject_project_metadata, read_template_stack
2121
from fastapi_fastkit.utils.logging import setup_logging
2222
from fastapi_fastkit.core.settings import FastkitConfig
23-
from fastapi_fastkit.core.exceptions import CLIExceptions, TemplateExceptions
23+
from fastapi_fastkit.core.exceptions import CLIExceptions
2424
from fastapi_fastkit.utils.transducer import copy_and_convert_template
2525
from fastapi_fastkit.utils.inspector import delete_project
2626

2727

2828
logger = getLogger(__name__)
2929

3030

31-
# --------------------------------------------------------------------------
32-
# Backend operators
33-
# --------------------------------------------------------------------------
34-
REGEX = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"
35-
36-
37-
def validate_email(ctx: Context, param: Any, value: Any) -> Any:
38-
"""
39-
Check if the provided email is in a valid format.
40-
This will recursively loop until a valid email input entry is given.
41-
42-
:param ctx: context of passing configurations (NOT specify it at CLI)
43-
:type ctx: <Object click.Context>
44-
:param param: parameters from CLI
45-
:param value: values from CLI
46-
:return:
47-
"""
48-
try:
49-
if not re.match(REGEX, value):
50-
raise ValueError(value)
51-
else:
52-
return value
53-
except ValueError as e:
54-
click.echo("Incorrect email address given: {}".format(e))
55-
value = click.prompt(param.prompt)
56-
return validate_email(ctx, param, value)
57-
58-
59-
def _inject_project_metadata(
60-
target_dir: str,
61-
project_name: str,
62-
author: str,
63-
author_email: str,
64-
description: str,
65-
) -> None:
66-
"""
67-
Inject metadata into the main.py and setup.py files.
68-
69-
:param target_dir: Directory for the new project to deploy
70-
:param project_name: new project name
71-
:param author: cli username
72-
:param author_email: cli user email
73-
:param description: new project description
74-
"""
75-
main_py_path = os.path.join(target_dir, "main.py")
76-
setup_py_path = os.path.join(target_dir, "setup.py")
77-
78-
try:
79-
with open(main_py_path, "r+") as f:
80-
content = f.read()
81-
content = content.replace("app_title", f'"{project_name}"')
82-
content = content.replace("app_description", f'"{description}"')
83-
f.seek(0)
84-
f.write(content)
85-
f.truncate()
86-
87-
with open(setup_py_path, "r+") as f:
88-
content = f.read()
89-
content = content.replace("<project_name>", project_name, 1)
90-
content = content.replace("<description>", description, 1)
91-
content = content.replace("<author>", author, 1)
92-
content = content.replace("<author_email>", author_email, 1)
93-
f.seek(0)
94-
f.write(content)
95-
f.truncate()
96-
except Exception as e:
97-
click.echo(e)
98-
raise TemplateExceptions("ERROR : Having some errors with injecting metadata")
99-
100-
101-
# --------------------------------------------------------------------------
102-
# Click operator methods
103-
# --------------------------------------------------------------------------
10431
@click.group()
10532
@click.option("--debug/--no-debug", default=False)
10633
@click.version_option(__version__, prog_name="fastapi-fastkit")
@@ -232,6 +159,7 @@ def startup(
232159
click.echo(f"Author: {author}")
233160
click.echo(f"Author Email: {author_email}")
234161
click.echo(f"Description: {description}")
162+
read_template_stack()
235163
# click.echo("Project Stack: [FastAPI, Uvicorn, SQLAlchemy, Docker (optional)]") # TODO : impl this?
236164

237165
confirm = click.confirm(
@@ -249,7 +177,7 @@ def startup(
249177

250178
copy_and_convert_template(target_template, user_local, project_name)
251179

252-
_inject_project_metadata(
180+
inject_project_metadata(
253181
project_dir, project_name, author, author_email, description
254182
)
255183

0 commit comments

Comments
 (0)