♻️ Replace os.mkdirs in favor of Path.mkdir
This commit is contained in:
parent
af1d502570
commit
eac45bdeb3
1 changed files with 4 additions and 5 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import os
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
|
|
@ -11,7 +10,7 @@ def render_template_file(template_path: Path, context: dict, output_path: Path):
|
||||||
"""Render a single Jinja2 template file to disk."""
|
"""Render a single Jinja2 template file to disk."""
|
||||||
template = env.get_template(str(template_path.relative_to(TEMPLATE_DIR)).replace("\\", "/"))
|
template = env.get_template(str(template_path.relative_to(TEMPLATE_DIR)).replace("\\", "/"))
|
||||||
content = template.render(**context)
|
content = template.render(**context)
|
||||||
os.makedirs(output_path.parent, exist_ok=True)
|
output_path.parent.mkdir(exist_ok=True)
|
||||||
with open(output_path, "w", encoding="utf-8") as f:
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
|
|
@ -22,7 +21,7 @@ def render_template_dir(template_subdir: str, target_dir: Path, context: dict, s
|
||||||
If subdir_name is given, files are placed inside target_dir/subdir_name.
|
If subdir_name is given, files are placed inside target_dir/subdir_name.
|
||||||
"""
|
"""
|
||||||
template_dir = TEMPLATE_DIR / template_subdir
|
template_dir = TEMPLATE_DIR / template_subdir
|
||||||
for root, _, files in os.walk(template_dir):
|
for root, _, files in template_dir.walk():
|
||||||
root_path = Path(root)
|
root_path = Path(root)
|
||||||
rel_root = root_path.relative_to(template_dir) # path relative to template_subdir
|
rel_root = root_path.relative_to(template_dir) # path relative to template_subdir
|
||||||
|
|
||||||
|
|
@ -45,7 +44,7 @@ def render_template_dir(template_subdir: str, target_dir: Path, context: dict, s
|
||||||
def startproject(name: str):
|
def startproject(name: str):
|
||||||
"""Create a new project: flat structure, all project templates go into <name>/"""
|
"""Create a new project: flat structure, all project templates go into <name>/"""
|
||||||
target_dir = Path(name).resolve()
|
target_dir = Path(name).resolve()
|
||||||
os.makedirs(target_dir, exist_ok=True)
|
target_dir.mkdir(exist_ok=True)
|
||||||
render_template_dir("project", target_dir, {"project_name": name})
|
render_template_dir("project", target_dir, {"project_name": name})
|
||||||
print(f"✅ Project '{name}' created successfully.")
|
print(f"✅ Project '{name}' created successfully.")
|
||||||
print(f"🔧 Next, configure your project in {target_dir / 'settings.py'}")
|
print(f"🔧 Next, configure your project in {target_dir / 'settings.py'}")
|
||||||
|
|
@ -54,7 +53,7 @@ def startproject(name: str):
|
||||||
def startapp(name: str, project: str):
|
def startapp(name: str, project: str):
|
||||||
"""Create a new app inside a project: templates go into <project_dir>/<name>/"""
|
"""Create a new app inside a project: templates go into <project_dir>/<name>/"""
|
||||||
target_dir = Path(project)
|
target_dir = Path(project)
|
||||||
os.makedirs(target_dir, exist_ok=True)
|
target_dir.makedirs(exist_ok=True)
|
||||||
render_template_dir("app", target_dir, {"project_name": target_dir.resolve().name, "app_name": name}, subdir_name=name)
|
render_template_dir("app", target_dir, {"project_name": target_dir.resolve().name, "app_name": name}, subdir_name=name)
|
||||||
print(f"✅ App '{name}' created in project '{target_dir}' successfully.")
|
print(f"✅ App '{name}' created in project '{target_dir}' successfully.")
|
||||||
print(f"🔧 Remember to add '{name}' to your INSTALLED_APPS!")
|
print(f"🔧 Remember to add '{name}' to your INSTALLED_APPS!")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue