fastapi-assist 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fa_assist/__init__.py ADDED
File without changes
fa_assist/cli.py ADDED
@@ -0,0 +1,44 @@
1
+ from pathlib import Path
2
+
3
+ import typer
4
+
5
+ from fa_assist.commands.init import init_project
6
+ from .util import (
7
+ check_or_raise_error,
8
+ get_project_name_into_config,
9
+ set_project_name_into_config,
10
+ )
11
+ from fa_assist.commands.create import create_app
12
+
13
+ app = typer.Typer(name="fa_assist", help="FastAPI development assistant.")
14
+
15
+
16
+ @app.command("init")
17
+ def init(
18
+ project_type: str,
19
+ project_name: str,
20
+ path: str = ".",
21
+ ):
22
+
23
+ check_or_raise_error(
24
+ project_type == "project",
25
+ "Provide project as resource to create a new FastAPI project.",
26
+ )
27
+ init_project(project_name, path)
28
+ set_project_name_into_config(project_name)
29
+
30
+
31
+ @app.command("add")
32
+ def add(
33
+ resource: str,
34
+ app_name: str,
35
+ ):
36
+ check_or_raise_error(
37
+ resource == "app", "Provide app as resource to create a new FastAPI app."
38
+ )
39
+ project_name = get_project_name_into_config()
40
+ create_app(project_name, app_name)
41
+
42
+
43
+ def main():
44
+ app()
File without changes
@@ -0,0 +1,78 @@
1
+ from pathlib import Path
2
+ from ..files_code.app_structure import *
3
+
4
+
5
+ def create_app(project_name, app_name: str):
6
+ """
7
+ Creates a new FastAPI app with the given name.
8
+ """
9
+
10
+ # ---------------------------------------------------------
11
+ # 1. Get current FastAPI project
12
+ # ---------------------------------------------------------
13
+ project_path = Path.cwd()
14
+
15
+ apps_path = project_path / project_name / "apps"
16
+ if not apps_path.exists():
17
+ raise FileNotFoundError(
18
+ "apps directory not found. "
19
+ "Make sure you are running this command "
20
+ "from a FastAPI project."
21
+ )
22
+
23
+ # ---------------------------------------------------------
24
+ # 2. Create app directory
25
+ # ---------------------------------------------------------
26
+ app_path = apps_path / app_name
27
+
28
+ if app_path.exists():
29
+ raise FileExistsError(f"App '{app_name}' already exists.")
30
+
31
+ app_path.mkdir(parents=True)
32
+
33
+ # ---------------------------------------------------------
34
+ # 3. Create directories
35
+ # ---------------------------------------------------------
36
+ directories = [
37
+ app_path / "views",
38
+ app_path / "schemas",
39
+ ]
40
+
41
+ for directory in directories:
42
+ directory.mkdir(
43
+ parents=True,
44
+ exist_ok=True,
45
+ )
46
+
47
+ # ---------------------------------------------------------
48
+ # 4. Create files
49
+ # ---------------------------------------------------------
50
+ files = {
51
+ "__init__.py": INIT_PY.format(app=app_name),
52
+ "views/__init__.py": VIEW_INIT_PY,
53
+ f"views/api_{app_name}_view.py": VIEW_PY.format(app=app_name),
54
+ "schemas/__init__.py": "",
55
+ "utils.py": UTILS_PY,
56
+ "models.py": MODELS_PY,
57
+ "constants.py": CONSTANT_PY,
58
+ "enums.py": "",
59
+ "dependencies.py": "",
60
+ "internal_api_calls.py": "",
61
+ }
62
+
63
+ # ---------------------------------------------------------
64
+ # 5. Write files
65
+ # ---------------------------------------------------------
66
+ for relative_path, content in files.items():
67
+
68
+ file_path = app_path / relative_path
69
+
70
+ file_path.write_text(
71
+ content,
72
+ encoding="utf-8",
73
+ )
74
+
75
+ print()
76
+ print(f"✓ FastAPI app '{app_name}' " f"created successfully.")
77
+ print(f" Location: {app_path}")
78
+ print()
@@ -0,0 +1,78 @@
1
+ from pathlib import Path
2
+ from ..files_code.project_structure import *
3
+
4
+ def init_project(project_name: str, path: str):
5
+ """
6
+ Initialize a new FastAPI project with the standard
7
+ FastAPI Assist project structure.
8
+ """
9
+
10
+ # ---------------------------------------------------------
11
+ # 1. Create project root
12
+ # ---------------------------------------------------------
13
+ project_path = Path(path).resolve() / project_name
14
+
15
+ if project_path.exists():
16
+ raise FileExistsError(
17
+ f"Project '{project_name}' already exists at: {project_path}"
18
+ )
19
+
20
+ # ---------------------------------------------------------
21
+ # 3. Create directories
22
+ # ---------------------------------------------------------
23
+ directories = [
24
+ project_path / "logs",
25
+ project_path / "credential",
26
+ project_path / "independent_scripts",
27
+ ]
28
+
29
+ for directory in directories:
30
+ directory.mkdir(parents=True, exist_ok=True)
31
+
32
+ # ---------------------------------------------------------
33
+ # 2. Create files with their initial content
34
+ # ---------------------------------------------------------
35
+
36
+ files = {
37
+ "main.py": MAIN_PY,
38
+ ".env": DOT_ENV_FILE,
39
+
40
+ "configuration/config.py": CONFIG_PY.format(project_name=project_name),
41
+ "configuration/custom_error.py": CUSTOM_ERROR_PY,
42
+ "configuration/db.py": DB_PY,
43
+ "configuration/logger.py": LOGGER_PY,
44
+ "configuration/rate_limit.py": RATE_LIMIT_PY,
45
+ "configuration/server.py": SERVER_PY.format(project_name=project_name),
46
+
47
+ "apps/__init__.py": APPS_INIT_PY,
48
+ "apps/constants.py": CONSTANT_PY,
49
+ "apps/db_operations.py": DB_OPERATIONS_PY,
50
+ "apps/https_response.py": HTTPS_RESPONSE_PY,
51
+ "apps/models.py": MODELS_PY,
52
+ "apps/reg_models.py": REG_MODELS_PY,
53
+ "apps/reg_routers.py": REG_ROUTERS_PY,
54
+ "apps/schemas.py": SCHEMAS_PY,
55
+ "apps/utils.py": UTILS_PY,
56
+
57
+ "Dockerfile": DOCKER,
58
+ ".docker-compose.yaml": DOCKER_COMPOSE.format(project_name=project_name),
59
+ "docker_setup.sh": DOCKER_SETUP_SH,
60
+
61
+ "deployment.sh": DEPLOYMENT_SH.format(project_name=project_name),
62
+ }
63
+
64
+ for relative_path, content in files.items():
65
+ file_path = project_path / relative_path
66
+
67
+ # Make sure the parent directory exists
68
+ file_path.parent.mkdir(parents=True, exist_ok=True)
69
+
70
+ file_path.write_text(
71
+ content,
72
+ encoding="utf-8",
73
+ )
74
+
75
+ print()
76
+ print(f"✓ FastAPI project '{project_name}' created successfully.")
77
+ print(f" Location: {project_path}")
78
+ print()
@@ -0,0 +1,80 @@
1
+ UTILS_PY = """
2
+
3
+ def success_response(
4
+ data=None,
5
+ message="Success",
6
+ ):
7
+ return {
8
+ "success": True,
9
+ "message": message,
10
+ "data": data,
11
+ }
12
+
13
+
14
+ def error_response(
15
+ message="Something went wrong",
16
+ ):
17
+ return {
18
+ "success": False,
19
+ "message": message,
20
+ "data": None,
21
+ }
22
+
23
+
24
+ """
25
+ CONSTANT_PY = """
26
+
27
+ """
28
+
29
+ MODELS_PY = """
30
+
31
+ """
32
+
33
+ INIT_PY = """
34
+
35
+ from configuration.logger import create_get_logger
36
+
37
+ get_logger = create_get_logger("{app}")
38
+
39
+ """
40
+
41
+ VIEW_INIT_PY = """
42
+
43
+ import pkgutil, importlib
44
+ from . import *
45
+ from fastapi import APIRouter
46
+
47
+ _file_routers = tuple()
48
+
49
+ for file in pkgutil.iter_modules(__path__):
50
+ _file = importlib.import_module(f"{__name__}.{file.name}")
51
+ if hasattr(_file, "routers"):
52
+ _file_routers += (_file.routers, )
53
+
54
+ __routers: tuple[APIRouter, ...] = _file_routers
55
+
56
+ """
57
+
58
+ VIEW_PY = """
59
+
60
+ from pathlib import Path
61
+ import traceback
62
+
63
+ import pandas as pd
64
+ from tortoise.expressions import Q
65
+
66
+ from apps import generate_router as APIRoute
67
+ from apps.db_operations import read_instances
68
+ from apps.https_response import error_response, success_response
69
+ from fastapi import Body, Depends
70
+ from ...{app} import get_logger
71
+
72
+ routers = APIRoute('/{app}', tags=["{app} View Section"])
73
+ logger = get_logger("{app}_view")
74
+
75
+ @routers.get("")
76
+ async def api_view():
77
+ logger.info("API view called")
78
+ return success_response(data = "", message = "success")
79
+
80
+ """