Skip to content

Commit fefc2b9

Browse files
committed
updates and refactorings
1 parent f682e65 commit fefc2b9

26 files changed

+3432
-919
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,9 @@ venv.bak/
106106
# idea
107107
.idea/
108108

109-
poetry.lock
109+
# pdm
110+
.pdm-python
111+
.pdm-build
112+
113+
# ruff
114+
.ruff_cache

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: end-of-file-fixer
6+
- id: check-toml
7+
8+
- repo: https://github.com/charliermarsh/ruff-pre-commit
9+
rev: v0.1.2
10+
hooks:
11+
- id: ruff
12+
args:
13+
- '--fix'
14+
- id: ruff-format
15+
16+
- repo: https://github.com/pdm-project/pdm
17+
rev: 2.9.2
18+
hooks:
19+
- id: pdm-export
20+
args:
21+
- -o
22+
- 'requirements.txt'
23+
- '--without-hashes'
24+
files: ^pdm.lock$

fastapi_ccli/__init__.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
import typer
43

5-
__version__ = '0.0.1'
6-
7-
# bool fonts
8-
GREEN = typer.style('True', fg='green', bold=True)
9-
RED = typer.style('False', fg='red', bold=True)
10-
11-
# GitHub src
12-
github_fs_src = 'https://github.com/wu-clan/fastapi_sqlalchemy_mysql.git'
13-
github_ft_src = 'https://github.com/wu-clan/fastapi_tortoise_mysql.git'
14-
# Gitee src
15-
gitee_fs_src = 'https://gitee.com/wu_cl/fastapi_sqlalchemy_mysql.git'
16-
gitee_ft_src = 'https://gitee.com/wu_cl/fastapi_tortoise_mysql.git'
4+
__version__ = "0.0.1"

fastapi_ccli/cloner/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
import typer
4+
5+
# Boolean fonts
6+
GREEN = typer.style("True", fg="green", bold=True)
7+
RED = typer.style("False", fg="red", bold=True)
8+
9+
# GitHub src
10+
github_fba_src = "https://github.com/wu-clan/fastapi_best_architecture.git"
11+
github_ftm_src = "https://github.com/wu-clan/fastapi_tortoise_mysql.git"
12+
github_fsm_src = "https://github.com/fastapi-practices/fastapi_sqlmodel_mysql.git"
13+
14+
# Gitee src
15+
gitee_fba_src = "https://gitee.com/wu_cl/fastapi_best_architecture.git"
16+
gitee_ftm_src = "https://gitee.com/wu_cl/fastapi_tortoise_mysql.git"

fastapi_ccli/cloner/cloner_en.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import re
4+
import time
5+
from typing import Optional
6+
7+
import typer
8+
from rich import print
9+
10+
from fastapi_ccli import __version__
11+
from fastapi_ccli.cloner import GREEN, RED, github_fba_src, github_ftm_src, github_fsm_src, gitee_fba_src, gitee_ftm_src
12+
from fastapi_ccli.utils.get_country import get_current_country
13+
from fastapi_ccli.utils.get_ip import get_net_ip
14+
from fastapi_ccli.utils.get_path import get_project_path
15+
16+
app_en = typer.Typer(rich_markup_mode="rich")
17+
18+
19+
def is_china(dns: bool) -> str:
20+
"""
21+
Whether to use dns
22+
23+
:param dns:
24+
:return:
25+
"""
26+
with typer.progressbar(range(5), label="Analyzing") as progress:
27+
for i in progress:
28+
ip = get_net_ip()
29+
if ip:
30+
progress.update(5)
31+
break
32+
else:
33+
time.sleep(0.3)
34+
progress.update(i)
35+
continue
36+
rp = get_current_country(ip)
37+
if "CN" in rp:
38+
ending = GREEN if dns else RED
39+
else:
40+
ending = RED if dns else GREEN
41+
return ending
42+
43+
44+
def exec_clone(orm: str, country: str, project: str, path: str) -> None:
45+
"""
46+
Perform clone
47+
48+
:param orm:
49+
:param country:
50+
:param project:
51+
:param path:
52+
:return:
53+
"""
54+
typer.echo("Project name: " + typer.style(project, fg="blue", bold=True))
55+
typer.echo("Select orm: " + orm)
56+
if orm == "sqlalchemy":
57+
source = github_fba_src if "True" in country else gitee_fba_src
58+
elif orm == "tortoise":
59+
source = github_ftm_src if "True" in country else gitee_ftm_src
60+
elif orm == "sqlmodel":
61+
source = github_fsm_src
62+
try:
63+
print(f"⏳ Start clone {source.split('/')[-1].split('.')[0]} project...") # noqa
64+
# out = os.system(f"git clone {source} {path}")
65+
# if out != 0:
66+
# raise RuntimeError(out)
67+
print(orm, country, project, path)
68+
except Exception as e:
69+
print(f"❌ Clone project failed: {e}")
70+
raise typer.Exit(1)
71+
else:
72+
print("✅ The project was cloned successfully")
73+
typer.echo(f"Please go to the directory {typer.style(path, fg='green', bold=True)} to view")
74+
75+
76+
@app_en.command(epilog="Made by :beating_heart: wu-clan")
77+
def cloner(
78+
version: Optional[bool] = typer.Option(
79+
None,
80+
"--version",
81+
"-V",
82+
help="Print version information.",
83+
),
84+
orm: Optional[str] = typer.Option(
85+
"sqlalchemy",
86+
"--orm",
87+
"-o",
88+
metavar="<ORM>",
89+
help="Select the orm to use, the default is sqlalchemy, support 'sqlalchemy' / 'tortoise' / 'sqlmodel'.",
90+
),
91+
project_path: Optional[str] = typer.Option(
92+
None,
93+
"--path",
94+
"-p",
95+
metavar="<PATH>",
96+
show_default=False,
97+
help="Project clone path, the default is '../fastapi_project', supports absolute path or relative path.",
98+
),
99+
):
100+
"""
101+
FastAPI project cloner
102+
"""
103+
if version:
104+
typer.secho("\n🔥 FastAPI CCLI " + __version__, fg="green", bold=True)
105+
if orm:
106+
if orm not in ["sqlalchemy", "tortoise", "sqlmodel"]:
107+
raise typer.BadParameter("Enter unknown parameters, only allowed 'sqlalchemy' / 'tortoise' / 'sqlmodel'")
108+
if project_path:
109+
if not isinstance(project_path, str):
110+
raise typer.BadParameter("Wrong parameter input, please enter the correct path")
111+
use_project_name = project_path or "../fastapi_project"
112+
path = get_project_path(use_project_name)
113+
project_name = re.split(r"/|\'|\\|\\\\", use_project_name)[-1]
114+
_country = typer.confirm("Is your region China?", default=False)
115+
country = is_china(_country)
116+
exec_clone(orm, country, project_name, path)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import re
4+
from typing import Optional
5+
6+
import questionary
7+
import typer
8+
9+
from fastapi_ccli import __version__
10+
from fastapi_ccli.cloner.cloner_en import exec_clone, is_china
11+
from fastapi_ccli.utils.get_path import get_project_path
12+
13+
app_en_form = typer.Typer(rich_markup_mode="rich")
14+
15+
16+
@app_en_form.command(epilog="Made by :beating_heart: wu-clan")
17+
def cloner(
18+
version: Optional[bool] = typer.Option(
19+
None,
20+
"--version",
21+
"-V",
22+
help="Print version information.",
23+
),
24+
project_path: Optional[str] = typer.Option(
25+
None,
26+
"--path",
27+
"-p",
28+
metavar="<PATH>",
29+
show_default=False,
30+
help="Project clone path, the default is '../fastapi_project', supports absolute path or relative path.",
31+
),
32+
):
33+
"""
34+
FastAPI project cloner
35+
"""
36+
if version:
37+
typer.secho("\n🔥 FastAPI CCLI " + __version__, fg="green", bold=True)
38+
if project_path:
39+
if not isinstance(project_path, str):
40+
raise typer.BadParameter("Wrong parameter input, please enter the correct path")
41+
use_project_name = project_path or "../fastapi_project"
42+
path = get_project_path(use_project_name)
43+
project_name = re.split(r"/|\'|\\|\\\\", use_project_name)[-1]
44+
result_if = questionary.form(
45+
orm=questionary.select(
46+
"Please select the orm you want to use:",
47+
choices=["sqlalchemy", "tortoise", "sqlmodel"],
48+
default="sqlalchemy",
49+
),
50+
country=questionary.select("Is your region China?", choices=["Yes", "No"], default="No"),
51+
).ask()
52+
if len(result_if) == 0:
53+
raise typer.Exit(1)
54+
country = is_china(result_if["country"])
55+
orm = result_if["orm"]
56+
exec_clone(orm, country, project_name, path)

0 commit comments

Comments
 (0)