PyArch-CLI 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.
- pyarch/__init__.py +1 -0
- pyarch/cli/__init__.py +1 -0
- pyarch/cli/add.py +27 -0
- pyarch/cli/app.py +83 -0
- pyarch/cli/common.py +28 -0
- pyarch/cli/generate.py +34 -0
- pyarch/config/__init__.py +1 -0
- pyarch/config/manifest.py +161 -0
- pyarch/config/models.py +51 -0
- pyarch/generators/__init__.py +1 -0
- pyarch/generators/common/__init__.py +1 -0
- pyarch/generators/common/commands.py +6 -0
- pyarch/generators/common/filesystem.py +55 -0
- pyarch/generators/common/gitignore.py +65 -0
- pyarch/generators/common/renderer.py +32 -0
- pyarch/generators/integration/__init__.py +1 -0
- pyarch/generators/integration/auth.py +158 -0
- pyarch/generators/module/__init__.py +1 -0
- pyarch/generators/module/layered.py +186 -0
- pyarch/generators/project/__init__.py +1 -0
- pyarch/generators/project/base.py +52 -0
- pyarch/generators/project/layered.py +167 -0
- pyarch/main.py +9 -0
- pyarch/services/__init__.py +1 -0
- pyarch/services/add_integration.py +37 -0
- pyarch/services/create_module.py +46 -0
- pyarch/services/create_project.py +20 -0
- pyarch/templates/layered/alembic_env.py.j2 +58 -0
- pyarch/templates/layered/auth/config.py.j2 +30 -0
- pyarch/templates/layered/auth/dependencies.py.j2 +75 -0
- pyarch/templates/layered/auth/refresh_session_model.py.j2 +31 -0
- pyarch/templates/layered/auth/routes.py.j2 +51 -0
- pyarch/templates/layered/auth/schemas.py.j2 +31 -0
- pyarch/templates/layered/auth/service.py.j2 +160 -0
- pyarch/templates/layered/auth/tokens.py.j2 +29 -0
- pyarch/templates/layered/auth/user_model.py.j2 +52 -0
- pyarch/templates/layered/auth/utils.py.j2 +70 -0
- pyarch/templates/layered/config.py.j2 +14 -0
- pyarch/templates/layered/env.example.j2 +7 -0
- pyarch/templates/layered/models_init.py.j2 +5 -0
- pyarch/templates/layered/module/module_model.py.j2 +25 -0
- pyarch/templates/layered/module/module_repo.py.j2 +49 -0
- pyarch/templates/layered/module/module_router.py.j2 +87 -0
- pyarch/templates/layered/module/module_router_protected.py.j2 +97 -0
- pyarch/templates/layered/module/module_schema.py.j2 +30 -0
- pyarch/templates/layered/module/module_service.py.j2 +76 -0
- pyarch/templates/layered/mongodb/config.py.j2 +15 -0
- pyarch/templates/layered/mongodb/conftest.py.j2 +36 -0
- pyarch/templates/layered/mongodb/env.example.j2 +2 -0
- pyarch/templates/layered/mongodb/module/module_model.py.j2 +11 -0
- pyarch/templates/layered/mongodb/module/module_repo.py.j2 +99 -0
- pyarch/templates/layered/mongodb/module/module_router.py.j2 +97 -0
- pyarch/templates/layered/mongodb/module/module_service.py.j2 +69 -0
- pyarch/templates/layered/mongodb/session.py.j2 +14 -0
- pyarch/templates/layered/router.py.j2 +8 -0
- pyarch/templates/layered/session.py.j2 +33 -0
- pyarch/templates/project/base/README.md.j2 +22 -0
- pyarch/templates/project/base/conftest.py.j2 +101 -0
- pyarch/templates/project/base/main.py.j2 +11 -0
- pyarch_cli-0.1.0.dist-info/METADATA +378 -0
- pyarch_cli-0.1.0.dist-info/RECORD +64 -0
- pyarch_cli-0.1.0.dist-info/WHEEL +4 -0
- pyarch_cli-0.1.0.dist-info/entry_points.txt +2 -0
- pyarch_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from cryptography.hazmat.primitives import serialization
|
|
4
|
+
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
5
|
+
|
|
6
|
+
from pyarch.config.models import DatabaseEngine
|
|
7
|
+
from pyarch.generators.common.commands import run_command
|
|
8
|
+
from pyarch.generators.common.filesystem import (
|
|
9
|
+
append_text_once,
|
|
10
|
+
create_empty_dir,
|
|
11
|
+
create_module_path,
|
|
12
|
+
insert_line_before_marker,
|
|
13
|
+
)
|
|
14
|
+
from pyarch.generators.common.gitignore import (
|
|
15
|
+
AUTH_GITIGNORE_ENTRIES,
|
|
16
|
+
ensure_gitignore_entries,
|
|
17
|
+
)
|
|
18
|
+
from pyarch.generators.common.renderer import create_file_from_template
|
|
19
|
+
from pyarch.generators.module.layered import ensure_layered_project, register_model
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
AUTH_INTEGRATION_NAME = "auth"
|
|
23
|
+
AUTH_ENV_MARKER = "# PyArch auth integration"
|
|
24
|
+
AUTH_ENV_BLOCK = f"""
|
|
25
|
+
{AUTH_ENV_MARKER}
|
|
26
|
+
AUTH_JWT__ALGORITHM=RS256
|
|
27
|
+
AUTH_JWT__ACCESS_TOKEN_EXPIRE_MINUTES=15
|
|
28
|
+
AUTH_JWT__REFRESH_TOKEN_EXPIRE_DAYS=30
|
|
29
|
+
# AUTH_JWT__PUBLIC_KEY_PATH=certs/public.pem
|
|
30
|
+
# AUTH_JWT__PRIVATE_KEY_PATH=certs/private.pem
|
|
31
|
+
"""
|
|
32
|
+
AUTH_RUNTIME_DEPENDENCIES = (
|
|
33
|
+
"pyjwt",
|
|
34
|
+
"bcrypt",
|
|
35
|
+
"pydantic[email]",
|
|
36
|
+
"python-multipart",
|
|
37
|
+
"cryptography",
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def create_auth_integration(
|
|
42
|
+
project_dir: Path,
|
|
43
|
+
database: DatabaseEngine | str,
|
|
44
|
+
) -> tuple[Path, ...]:
|
|
45
|
+
database = DatabaseEngine(database)
|
|
46
|
+
if database is DatabaseEngine.MONGODB:
|
|
47
|
+
raise ValueError("Auth integration is not supported for MongoDB projects yet")
|
|
48
|
+
|
|
49
|
+
app_path = project_dir / "app"
|
|
50
|
+
auth_path = app_path / "auth"
|
|
51
|
+
models_init = app_path / "models" / "__init__.py"
|
|
52
|
+
main_router = app_path / "api" / "v1" / "router.py"
|
|
53
|
+
|
|
54
|
+
ensure_layered_project(models_init, main_router)
|
|
55
|
+
|
|
56
|
+
auth_targets = (
|
|
57
|
+
("layered/auth/dependencies.py.j2", auth_path / "dependencies.py"),
|
|
58
|
+
("layered/auth/routes.py.j2", auth_path / "routes.py"),
|
|
59
|
+
("layered/auth/schemas.py.j2", auth_path / "schemas.py"),
|
|
60
|
+
("layered/auth/service.py.j2", auth_path / "service.py"),
|
|
61
|
+
("layered/auth/tokens.py.j2", auth_path / "tokens.py"),
|
|
62
|
+
("layered/auth/utils.py.j2", auth_path / "utils.py"),
|
|
63
|
+
("layered/auth/user_model.py.j2", app_path / "models" / "user.py"),
|
|
64
|
+
(
|
|
65
|
+
"layered/auth/refresh_session_model.py.j2",
|
|
66
|
+
app_path / "models" / "refresh_session.py",
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
generated_targets = [path for _, path in auth_targets]
|
|
70
|
+
generated_targets.extend(
|
|
71
|
+
(
|
|
72
|
+
project_dir / "certs" / "private.pem",
|
|
73
|
+
project_dir / "certs" / "public.pem",
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
existing_targets = [path for path in generated_targets if path.exists()]
|
|
78
|
+
if existing_targets:
|
|
79
|
+
existing = ", ".join(str(path) for path in existing_targets)
|
|
80
|
+
raise FileExistsError(f"Auth integration files already exist: {existing}")
|
|
81
|
+
|
|
82
|
+
install_auth_dependencies(project_dir)
|
|
83
|
+
|
|
84
|
+
created_files: list[Path] = []
|
|
85
|
+
create_module_path(auth_path)
|
|
86
|
+
created_files.append(auth_path / "__init__.py")
|
|
87
|
+
|
|
88
|
+
created_files.extend(
|
|
89
|
+
create_file_from_template(template_name, output_path)
|
|
90
|
+
for template_name, output_path in auth_targets
|
|
91
|
+
)
|
|
92
|
+
created_files.append(
|
|
93
|
+
create_file_from_template(
|
|
94
|
+
template_name="layered/auth/config.py.j2",
|
|
95
|
+
output_path=app_path / "core" / "config.py",
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
created_files.extend(create_auth_key_pair(project_dir / "certs"))
|
|
99
|
+
|
|
100
|
+
register_model(models_init, "user", "User")
|
|
101
|
+
register_model(models_init, "refresh_session", "RefreshSession")
|
|
102
|
+
register_auth_router(main_router)
|
|
103
|
+
created_files.append(append_auth_env_example(project_dir / ".env.example"))
|
|
104
|
+
created_files.append(
|
|
105
|
+
ensure_gitignore_entries(project_dir, "auth", AUTH_GITIGNORE_ENTRIES)
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
return tuple(created_files)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def install_auth_dependencies(project_dir: Path) -> None:
|
|
112
|
+
run_command("uv", "add", *AUTH_RUNTIME_DEPENDENCIES, cwd=project_dir)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def create_auth_key_pair(certs_dir: Path) -> tuple[Path, Path]:
|
|
116
|
+
create_empty_dir(certs_dir)
|
|
117
|
+
private_path = certs_dir / "private.pem"
|
|
118
|
+
public_path = certs_dir / "public.pem"
|
|
119
|
+
|
|
120
|
+
private_key = rsa.generate_private_key(
|
|
121
|
+
public_exponent=65537,
|
|
122
|
+
key_size=2048,
|
|
123
|
+
)
|
|
124
|
+
public_key = private_key.public_key()
|
|
125
|
+
|
|
126
|
+
private_path.write_bytes(
|
|
127
|
+
private_key.private_bytes(
|
|
128
|
+
encoding=serialization.Encoding.PEM,
|
|
129
|
+
format=serialization.PrivateFormat.PKCS8,
|
|
130
|
+
encryption_algorithm=serialization.NoEncryption(),
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
public_path.write_bytes(
|
|
134
|
+
public_key.public_bytes(
|
|
135
|
+
encoding=serialization.Encoding.PEM,
|
|
136
|
+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
return private_path, public_path
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def register_auth_router(main_router: Path) -> None:
|
|
144
|
+
insert_line_before_marker(
|
|
145
|
+
main_router,
|
|
146
|
+
"# pyarch:router-imports",
|
|
147
|
+
"from app.auth.routes import router as auth_router",
|
|
148
|
+
)
|
|
149
|
+
insert_line_before_marker(
|
|
150
|
+
main_router,
|
|
151
|
+
"# pyarch:router-includes",
|
|
152
|
+
"v1_router.include_router(auth_router)",
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def append_auth_env_example(env_example: Path) -> Path:
|
|
157
|
+
append_text_once(env_example, AUTH_ENV_MARKER, AUTH_ENV_BLOCK)
|
|
158
|
+
return env_example
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Business module generators."""
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import keyword
|
|
2
|
+
import re
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from pyarch.config.models import DatabaseEngine
|
|
6
|
+
from pyarch.generators.common.filesystem import insert_line_before_marker
|
|
7
|
+
from pyarch.generators.common.renderer import create_file_from_template
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
MODULE_NAME_PATTERN = re.compile(r"^[a-z][a-z0-9_]*$")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def create_layered_module(
|
|
14
|
+
project_dir: Path,
|
|
15
|
+
module_name: str,
|
|
16
|
+
database: DatabaseEngine | str = DatabaseEngine.POSTGRES,
|
|
17
|
+
*,
|
|
18
|
+
protected: bool = False,
|
|
19
|
+
) -> tuple[Path, ...]:
|
|
20
|
+
database = DatabaseEngine(database)
|
|
21
|
+
if protected and database is DatabaseEngine.MONGODB:
|
|
22
|
+
raise ValueError("Protected modules are not supported for MongoDB projects yet")
|
|
23
|
+
|
|
24
|
+
module_name = normalize_module_name(module_name)
|
|
25
|
+
model_name = to_pascal_case(module_name)
|
|
26
|
+
resource_name = pluralize_identifier(module_name)
|
|
27
|
+
|
|
28
|
+
app_path = project_dir / "app"
|
|
29
|
+
models_init = app_path / "models" / "__init__.py"
|
|
30
|
+
main_router = app_path / "api" / "v1" / "router.py"
|
|
31
|
+
|
|
32
|
+
ensure_layered_project(models_init, main_router)
|
|
33
|
+
|
|
34
|
+
relational_targets = (
|
|
35
|
+
(
|
|
36
|
+
"layered/module/module_model.py.j2",
|
|
37
|
+
app_path / "models" / f"{module_name}.py",
|
|
38
|
+
),
|
|
39
|
+
(
|
|
40
|
+
"layered/module/module_schema.py.j2",
|
|
41
|
+
app_path / "schemas" / f"{module_name}.py",
|
|
42
|
+
),
|
|
43
|
+
(
|
|
44
|
+
"layered/module/module_repo.py.j2",
|
|
45
|
+
app_path / "repositories" / f"{module_name}.py",
|
|
46
|
+
),
|
|
47
|
+
(
|
|
48
|
+
"layered/module/module_service.py.j2",
|
|
49
|
+
app_path / "services" / f"{module_name}.py",
|
|
50
|
+
),
|
|
51
|
+
(
|
|
52
|
+
(
|
|
53
|
+
"layered/module/module_router_protected.py.j2"
|
|
54
|
+
if protected
|
|
55
|
+
else "layered/module/module_router.py.j2"
|
|
56
|
+
),
|
|
57
|
+
app_path / "api" / "v1" / f"{module_name}.py",
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
mongodb_targets = (
|
|
61
|
+
(
|
|
62
|
+
"layered/mongodb/module/module_model.py.j2",
|
|
63
|
+
app_path / "models" / f"{module_name}.py",
|
|
64
|
+
),
|
|
65
|
+
(
|
|
66
|
+
"layered/module/module_schema.py.j2",
|
|
67
|
+
app_path / "schemas" / f"{module_name}.py",
|
|
68
|
+
),
|
|
69
|
+
(
|
|
70
|
+
"layered/mongodb/module/module_repo.py.j2",
|
|
71
|
+
app_path / "repositories" / f"{module_name}.py",
|
|
72
|
+
),
|
|
73
|
+
(
|
|
74
|
+
"layered/mongodb/module/module_service.py.j2",
|
|
75
|
+
app_path / "services" / f"{module_name}.py",
|
|
76
|
+
),
|
|
77
|
+
(
|
|
78
|
+
"layered/mongodb/module/module_router.py.j2",
|
|
79
|
+
app_path / "api" / "v1" / f"{module_name}.py",
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
targets = (
|
|
83
|
+
mongodb_targets
|
|
84
|
+
if database is DatabaseEngine.MONGODB
|
|
85
|
+
else relational_targets
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
existing_targets = [path for _, path in targets if path.exists()]
|
|
89
|
+
if existing_targets:
|
|
90
|
+
existing = ", ".join(str(path) for path in existing_targets)
|
|
91
|
+
raise FileExistsError(f"Module files already exist: {existing}")
|
|
92
|
+
|
|
93
|
+
context = {
|
|
94
|
+
"module_name": module_name,
|
|
95
|
+
"model_name": model_name,
|
|
96
|
+
"resource_name": resource_name,
|
|
97
|
+
"table_name": resource_name,
|
|
98
|
+
"id_type": "str" if database is DatabaseEngine.MONGODB else "int",
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
created_files = tuple(
|
|
102
|
+
create_file_from_template(template_name, output_path, **context)
|
|
103
|
+
for template_name, output_path in targets
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
register_model(models_init, module_name, model_name)
|
|
107
|
+
register_router(main_router, module_name)
|
|
108
|
+
return created_files
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def normalize_module_name(module_name: str) -> str:
|
|
112
|
+
normalized = module_name.strip().lower().replace("-", "_")
|
|
113
|
+
|
|
114
|
+
if not MODULE_NAME_PATTERN.fullmatch(normalized):
|
|
115
|
+
raise ValueError(
|
|
116
|
+
"Module name must start with a letter and contain only "
|
|
117
|
+
"lowercase letters, numbers, and underscores"
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
if keyword.iskeyword(normalized):
|
|
121
|
+
raise ValueError(f"Module name cannot be a Python keyword: {normalized}")
|
|
122
|
+
|
|
123
|
+
return normalized
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def to_pascal_case(module_name: str) -> str:
|
|
127
|
+
return "".join(part.capitalize() for part in module_name.split("_"))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def pluralize_identifier(module_name: str) -> str:
|
|
131
|
+
prefix, separator, word = module_name.rpartition("_")
|
|
132
|
+
|
|
133
|
+
if word.endswith(("s", "x", "z", "ch", "sh")):
|
|
134
|
+
plural = f"{word}es"
|
|
135
|
+
elif word.endswith("y") and len(word) > 1 and word[-2] not in "aeiou":
|
|
136
|
+
plural = f"{word[:-1]}ies"
|
|
137
|
+
else:
|
|
138
|
+
plural = f"{word}s"
|
|
139
|
+
|
|
140
|
+
return f"{prefix}{separator}{plural}" if prefix else plural
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def ensure_layered_project(models_init: Path, main_router: Path) -> None:
|
|
144
|
+
required_markers = (
|
|
145
|
+
(models_init, "# pyarch:model-imports"),
|
|
146
|
+
(models_init, "# pyarch:model-exports"),
|
|
147
|
+
(main_router, "# pyarch:router-imports"),
|
|
148
|
+
(main_router, "# pyarch:router-includes"),
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
for file_path, marker in required_markers:
|
|
152
|
+
if not file_path.is_file():
|
|
153
|
+
raise FileNotFoundError(f"Required Layered file is missing: {file_path}")
|
|
154
|
+
|
|
155
|
+
if marker not in file_path.read_text(encoding="utf-8"):
|
|
156
|
+
raise ValueError(f"Marker {marker!r} was not found in {file_path}")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def register_model(
|
|
160
|
+
models_init: Path,
|
|
161
|
+
module_name: str,
|
|
162
|
+
model_name: str,
|
|
163
|
+
) -> None:
|
|
164
|
+
insert_line_before_marker(
|
|
165
|
+
models_init,
|
|
166
|
+
"# pyarch:model-imports",
|
|
167
|
+
f"from app.models.{module_name} import {model_name}",
|
|
168
|
+
)
|
|
169
|
+
insert_line_before_marker(
|
|
170
|
+
models_init,
|
|
171
|
+
" # pyarch:model-exports",
|
|
172
|
+
f' "{model_name}",',
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def register_router(main_router: Path, module_name: str) -> None:
|
|
177
|
+
insert_line_before_marker(
|
|
178
|
+
main_router,
|
|
179
|
+
"# pyarch:router-imports",
|
|
180
|
+
f"from app.api.v1.{module_name} import router as {module_name}_router",
|
|
181
|
+
)
|
|
182
|
+
insert_line_before_marker(
|
|
183
|
+
main_router,
|
|
184
|
+
"# pyarch:router-includes",
|
|
185
|
+
f"v1_router.include_router({module_name}_router)",
|
|
186
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Project scaffold generators."""
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from pyarch.generators.common.commands import run_command
|
|
4
|
+
from pyarch.generators.common.filesystem import create_empty_dir, create_empty_file
|
|
5
|
+
from pyarch.generators.common.gitignore import (
|
|
6
|
+
BASE_GITIGNORE_ENTRIES,
|
|
7
|
+
ensure_gitignore_entries,
|
|
8
|
+
)
|
|
9
|
+
from pyarch.generators.common.renderer import create_file_from_template
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def create_base_dir(project_name: str) -> Path:
|
|
13
|
+
"""
|
|
14
|
+
create base dir with all README.md, gitignore, ,env.example and etc files
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
project_dir = Path.cwd() / project_name
|
|
18
|
+
|
|
19
|
+
# creating base uv app
|
|
20
|
+
run_command("uv", "init", project_name)
|
|
21
|
+
ensure_gitignore_entries(project_dir, "base", BASE_GITIGNORE_ENTRIES)
|
|
22
|
+
|
|
23
|
+
# files shared by all supported architectures
|
|
24
|
+
create_empty_file(project_dir / "Dockerfile")
|
|
25
|
+
create_empty_file(project_dir / "docker-compose.yml")
|
|
26
|
+
|
|
27
|
+
create_docs_dir(project_dir)
|
|
28
|
+
|
|
29
|
+
return project_dir
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def create_docs_dir(project_dir: Path) -> None:
|
|
33
|
+
"""
|
|
34
|
+
creating files where you can describe your project, rbac roles etc.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
docs_dir = create_empty_dir(project_dir / "docs")
|
|
38
|
+
create_empty_file(docs_dir / "RBAC.md")
|
|
39
|
+
create_empty_file(docs_dir / "ARCHITECTURE.md")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def create_readme_file(
|
|
43
|
+
project_dir: Path,
|
|
44
|
+
project_name: str,
|
|
45
|
+
database: str,
|
|
46
|
+
) -> None:
|
|
47
|
+
create_file_from_template(
|
|
48
|
+
template_name="project/base/README.md.j2",
|
|
49
|
+
output_path=project_dir / "README.md",
|
|
50
|
+
project_name=project_name,
|
|
51
|
+
database=database,
|
|
52
|
+
)
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from pyarch.config.models import DatabaseEngine
|
|
4
|
+
from pyarch.generators.common.commands import run_command
|
|
5
|
+
from pyarch.generators.common.filesystem import create_module_path
|
|
6
|
+
from pyarch.generators.common.renderer import create_file_from_template
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
COMMON_RUNTIME_DEPENDENCIES = (
|
|
10
|
+
"fastapi",
|
|
11
|
+
"uvicorn",
|
|
12
|
+
"python-dotenv",
|
|
13
|
+
"pydantic-settings",
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
RELATIONAL_RUNTIME_DEPENDENCIES = (
|
|
17
|
+
"sqlalchemy",
|
|
18
|
+
"alembic",
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
DATABASE_RUNTIME_DEPENDENCIES = {
|
|
22
|
+
DatabaseEngine.POSTGRES: ("psycopg[binary]",),
|
|
23
|
+
DatabaseEngine.SQLITE: (),
|
|
24
|
+
DatabaseEngine.MONGODB: ("pymongo",),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
COMMON_DEV_DEPENDENCIES = (
|
|
28
|
+
"pytest",
|
|
29
|
+
"httpx2",
|
|
30
|
+
"ruff",
|
|
31
|
+
"mypy",
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def create_layered_project(
|
|
36
|
+
project_dir: Path,
|
|
37
|
+
database: DatabaseEngine,
|
|
38
|
+
) -> Path:
|
|
39
|
+
install_dependencies(project_dir, database)
|
|
40
|
+
|
|
41
|
+
generated_main = project_dir / "main.py"
|
|
42
|
+
if generated_main.exists():
|
|
43
|
+
generated_main.unlink()
|
|
44
|
+
|
|
45
|
+
app_path = create_module_path(project_dir / "app")
|
|
46
|
+
create_layered_arch(app_path)
|
|
47
|
+
create_database_setup(project_dir, app_path, database)
|
|
48
|
+
create_test_setup(project_dir, database)
|
|
49
|
+
|
|
50
|
+
if database is not DatabaseEngine.MONGODB:
|
|
51
|
+
initialize_alembic(project_dir)
|
|
52
|
+
|
|
53
|
+
return app_path
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def install_dependencies(
|
|
57
|
+
project_dir: Path,
|
|
58
|
+
database: DatabaseEngine,
|
|
59
|
+
) -> None:
|
|
60
|
+
runtime_dependencies = list(COMMON_RUNTIME_DEPENDENCIES)
|
|
61
|
+
|
|
62
|
+
if database is not DatabaseEngine.MONGODB:
|
|
63
|
+
runtime_dependencies.extend(RELATIONAL_RUNTIME_DEPENDENCIES)
|
|
64
|
+
|
|
65
|
+
runtime_dependencies.extend(DATABASE_RUNTIME_DEPENDENCIES[database])
|
|
66
|
+
run_command("uv", "add", *runtime_dependencies, cwd=project_dir)
|
|
67
|
+
|
|
68
|
+
dev_dependencies = list(COMMON_DEV_DEPENDENCIES)
|
|
69
|
+
if database is DatabaseEngine.MONGODB:
|
|
70
|
+
dev_dependencies.append("mongomock")
|
|
71
|
+
|
|
72
|
+
run_command("uv", "add", "--dev", *dev_dependencies, cwd=project_dir)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def create_layered_arch(app_path: Path) -> None:
|
|
76
|
+
"""Create packages and shared files for a Layered application."""
|
|
77
|
+
|
|
78
|
+
create_file_from_template(
|
|
79
|
+
template_name="project/base/main.py.j2",
|
|
80
|
+
output_path=app_path / "main.py",
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
base_layers = (
|
|
84
|
+
"core",
|
|
85
|
+
"db",
|
|
86
|
+
"models",
|
|
87
|
+
"schemas",
|
|
88
|
+
"api",
|
|
89
|
+
"services",
|
|
90
|
+
"repositories",
|
|
91
|
+
"dependencies",
|
|
92
|
+
)
|
|
93
|
+
for layer in base_layers:
|
|
94
|
+
create_module_path(app_path / layer)
|
|
95
|
+
|
|
96
|
+
create_module_path(app_path / "api" / "v1")
|
|
97
|
+
|
|
98
|
+
create_file_from_template(
|
|
99
|
+
template_name="layered/router.py.j2",
|
|
100
|
+
output_path=app_path / "api" / "v1" / "router.py",
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
create_file_from_template(
|
|
104
|
+
template_name="layered/models_init.py.j2",
|
|
105
|
+
output_path=app_path / "models" / "__init__.py",
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def create_database_setup(
|
|
110
|
+
project_dir: Path,
|
|
111
|
+
app_path: Path,
|
|
112
|
+
database: DatabaseEngine,
|
|
113
|
+
) -> None:
|
|
114
|
+
if database is DatabaseEngine.MONGODB:
|
|
115
|
+
create_file_from_template(
|
|
116
|
+
template_name="layered/mongodb/config.py.j2",
|
|
117
|
+
output_path=app_path / "core" / "config.py",
|
|
118
|
+
)
|
|
119
|
+
create_file_from_template(
|
|
120
|
+
template_name="layered/mongodb/session.py.j2",
|
|
121
|
+
output_path=app_path / "db" / "session.py",
|
|
122
|
+
)
|
|
123
|
+
create_file_from_template(
|
|
124
|
+
template_name="layered/mongodb/env.example.j2",
|
|
125
|
+
output_path=project_dir / ".env.example",
|
|
126
|
+
)
|
|
127
|
+
return
|
|
128
|
+
|
|
129
|
+
create_file_from_template(
|
|
130
|
+
template_name="layered/config.py.j2",
|
|
131
|
+
output_path=app_path / "core" / "config.py",
|
|
132
|
+
)
|
|
133
|
+
create_file_from_template(
|
|
134
|
+
template_name="layered/session.py.j2",
|
|
135
|
+
output_path=app_path / "db" / "session.py",
|
|
136
|
+
database=database.value,
|
|
137
|
+
)
|
|
138
|
+
create_file_from_template(
|
|
139
|
+
template_name="layered/env.example.j2",
|
|
140
|
+
output_path=project_dir / ".env.example",
|
|
141
|
+
database=database.value,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def create_test_setup(
|
|
146
|
+
project_dir: Path,
|
|
147
|
+
database: DatabaseEngine,
|
|
148
|
+
) -> None:
|
|
149
|
+
create_module_path(project_dir / "tests")
|
|
150
|
+
|
|
151
|
+
template_name = (
|
|
152
|
+
"layered/mongodb/conftest.py.j2"
|
|
153
|
+
if database is DatabaseEngine.MONGODB
|
|
154
|
+
else "project/base/conftest.py.j2"
|
|
155
|
+
)
|
|
156
|
+
create_file_from_template(
|
|
157
|
+
template_name=template_name,
|
|
158
|
+
output_path=project_dir / "tests" / "conftest.py",
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def initialize_alembic(project_dir: Path) -> None:
|
|
163
|
+
run_command("uv", "run", "alembic", "init", "alembic", cwd=project_dir)
|
|
164
|
+
create_file_from_template(
|
|
165
|
+
template_name="layered/alembic_env.py.j2",
|
|
166
|
+
output_path=project_dir / "alembic" / "env.py",
|
|
167
|
+
)
|
pyarch/main.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Application services used by the CLI."""
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from pyarch.config.manifest import find_project_root, load_manifest, save_manifest
|
|
4
|
+
from pyarch.generators.integration.auth import (
|
|
5
|
+
AUTH_INTEGRATION_NAME,
|
|
6
|
+
create_auth_integration,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def add_integration(
|
|
11
|
+
integration_name: str,
|
|
12
|
+
start: Path | None = None,
|
|
13
|
+
) -> tuple[Path, tuple[Path, ...]]:
|
|
14
|
+
project_root = find_project_root(start)
|
|
15
|
+
manifest = load_manifest(project_root)
|
|
16
|
+
normalized_name = integration_name.strip().lower().replace("-", "_")
|
|
17
|
+
|
|
18
|
+
if not normalized_name:
|
|
19
|
+
raise ValueError("Integration name cannot be empty")
|
|
20
|
+
|
|
21
|
+
if normalized_name in manifest.state.integrations:
|
|
22
|
+
raise FileExistsError(
|
|
23
|
+
f"Integration {normalized_name!r} is already registered"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
if normalized_name != AUTH_INTEGRATION_NAME:
|
|
27
|
+
raise NotImplementedError(
|
|
28
|
+
f"Integration {normalized_name!r} does not have a generator yet"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
created_files = create_auth_integration(
|
|
32
|
+
project_root,
|
|
33
|
+
manifest.database.engine,
|
|
34
|
+
)
|
|
35
|
+
manifest.state.integrations.append(normalized_name)
|
|
36
|
+
save_manifest(project_root, manifest)
|
|
37
|
+
return project_root, created_files
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from pyarch.config.manifest import find_project_root, load_manifest, save_manifest
|
|
4
|
+
from pyarch.config.models import Architecture
|
|
5
|
+
from pyarch.generators.integration.auth import AUTH_INTEGRATION_NAME
|
|
6
|
+
from pyarch.generators.module.layered import (
|
|
7
|
+
create_layered_module,
|
|
8
|
+
normalize_module_name,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def create_module(
|
|
13
|
+
module_name: str,
|
|
14
|
+
start: Path | None = None,
|
|
15
|
+
protected: bool = False,
|
|
16
|
+
) -> tuple[Path, tuple[Path, ...]]:
|
|
17
|
+
project_root = find_project_root(start)
|
|
18
|
+
manifest = load_manifest(project_root)
|
|
19
|
+
normalized_name = normalize_module_name(module_name)
|
|
20
|
+
|
|
21
|
+
if normalized_name in manifest.state.modules:
|
|
22
|
+
raise FileExistsError(
|
|
23
|
+
f"Module {normalized_name!r} is already registered"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
if manifest.project.architecture is not Architecture.LAYERED:
|
|
27
|
+
raise ValueError(
|
|
28
|
+
f"Unsupported architecture: {manifest.project.architecture.value}"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if protected and AUTH_INTEGRATION_NAME not in manifest.state.integrations:
|
|
32
|
+
raise ValueError(
|
|
33
|
+
"Protected module requires auth integration. "
|
|
34
|
+
"Run `pyarch add integration auth` first."
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
created_files = create_layered_module(
|
|
38
|
+
project_root,
|
|
39
|
+
normalized_name,
|
|
40
|
+
manifest.database.engine,
|
|
41
|
+
protected=protected,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
manifest.state.modules.append(normalized_name)
|
|
45
|
+
save_manifest(project_root, manifest)
|
|
46
|
+
return project_root, created_files
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from pyarch.config.manifest import create_manifest
|
|
5
|
+
from pyarch.config.models import DatabaseEngine
|
|
6
|
+
from pyarch.generators.project.base import create_base_dir, create_readme_file
|
|
7
|
+
from pyarch.generators.project.layered import create_layered_project
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def create_project(
|
|
11
|
+
project_name: str,
|
|
12
|
+
database: DatabaseEngine | str = DatabaseEngine.POSTGRES,
|
|
13
|
+
) -> Path:
|
|
14
|
+
database = DatabaseEngine(database)
|
|
15
|
+
|
|
16
|
+
project_dir = create_base_dir(project_name)
|
|
17
|
+
create_layered_project(project_dir, database)
|
|
18
|
+
create_readme_file(project_dir, project_name, database.value)
|
|
19
|
+
create_manifest(project_dir, project_name, database)
|
|
20
|
+
return project_dir
|