refactor(djinit/utils): centralize utility functions into CommonUtils class

- Enhancements:
  - Moved file and directory creation helpers into CommonUtils class
  - Updated all call sites to use CommonUtils methods
  - Removed unused standalone utility functions and imports
  - Preserved existing behavior without functional changes
This commit is contained in:
sankalp committed 2026-01-31 02:48:26 +05:45
1 parent 744f65efc7
commit 85981115ce
2 files changed
+307 -297

No files matched your search

+289 -278
View File
@@ -8,322 +8,333 @@ import subprocess
import sys
from contextlib import contextmanager
from djinit.core.base import BaseUtils
from djinit.services.templates import template_engine
from djinit.ui.console import UIFormatter
def format_file(filename: str) -> None:
"""Format Python file using Ruff formatter."""
subprocess.run([sys.executable, "-m", "ruff", "format", filename], check=False, capture_output=True)
class CommonUtils(BaseUtils):
@staticmethod
def format_file(filename: str) -> None:
"""Format Python file using Ruff formatter."""
subprocess.run([sys.executable, "-m", "ruff", "format", filename], check=False, capture_output=True)
@staticmethod
def create_file_with_content(
filename: str, content: str, success_message: str, should_format: bool = False
) -> None:
from djinit.utils.exceptions import FileError
def create_file_with_content(filename: str, content: str, success_message: str, should_format: bool = False) -> None:
from djinit.utils.exceptions import FileError
try:
with open(filename, "w") as file:
file.write(content)
except OSError as e:
raise FileError(f"Failed to write file: {filename}", details=str(e)) from e
try:
with open(filename, "w") as file:
file.write(content)
except OSError as e:
raise FileError(f"Failed to write file: {filename}", details=str(e)) from e
if should_format:
CommonUtils.format_file(filename)
if should_format:
format_file(filename)
UIFormatter.print_success(success_message)
UIFormatter.print_success(success_message)
@staticmethod
def create_file_from_template(
file_path: str,
template_path: str,
context: dict,
success_message: str,
should_format: bool = True,
) -> None:
try:
content = template_engine.render_template(template_path, context)
except Exception as e:
from djinit.utils.exceptions import TemplateError
raise TemplateError(f"Failed to render template: {template_path}", details=str(e)) from e
def create_file_from_template(
file_path: str,
template_path: str,
context: dict,
success_message: str,
should_format: bool = True,
) -> None:
try:
content = template_engine.render_template(template_path, context)
except Exception as e:
from djinit.utils.exceptions import TemplateError
CommonUtils.create_file_with_content(file_path, content, success_message, should_format=should_format)
raise TemplateError(f"Failed to render template: {template_path}", details=str(e)) from e
@staticmethod
def create_init_file(directory: str, success_message: str) -> None:
init_path = os.path.join(directory, "__init__.py")
CommonUtils.create_file_from_template(
init_path, "project/init.py-tpl", {}, success_message, should_format=False
)
create_file_with_content(file_path, content, success_message, should_format=should_format)
@staticmethod
def create_directory_with_init(dir_path: str, message: str) -> None:
"""Create directory and __init__.py file."""
os.makedirs(dir_path, exist_ok=True)
CommonUtils.create_init_file(dir_path, message)
@staticmethod
def create_files_from_templates(base_dir: str, files: list, prefix: str = "", should_format: bool = False) -> None:
"""Create multiple files from templates.
def create_init_file(directory: str, success_message: str) -> None:
init_path = os.path.join(directory, "__init__.py")
create_file_from_template(init_path, "project/init.py-tpl", {}, success_message, should_format=False)
def create_directory_with_init(dir_path: str, message: str) -> None:
"""Create directory and __init__.py file."""
os.makedirs(dir_path, exist_ok=True)
create_init_file(dir_path, message)
def create_files_from_templates(base_dir: str, files: list, prefix: str = "", should_format: bool = False) -> None:
"""Create multiple files from templates.
Args:
base_dir: Base directory where files will be created
files: List of tuples (filename, template_path, context)
prefix: Optional prefix for success message
should_format: Whether to format the created files
"""
for file_spec in files:
if len(file_spec) == 3:
filename, template_path, context = file_spec
else:
# Support (filename, template_path) format with empty context
filename, template_path = file_spec
context = {}
filepath = os.path.join(base_dir, filename)
message = f"Created {prefix}{filename}" if prefix else f"Created {filename}"
create_file_from_template(filepath, template_path, context, message, should_format=should_format)
def get_package_name(project_dir: str) -> str:
"""Get package name, defaulting to 'backend' if project_dir is '.' or empty."""
return "backend" if project_dir == "." or not project_dir else project_dir
@contextmanager
def change_cwd(path: str):
original_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(original_cwd)
def _parse_app_entry_from_line(line: str) -> str | None:
"""Extract app name from a line in USER_DEFINED_APPS section."""
line_stripped = line.strip().strip(",").strip()
for quote in ('"', "'"):
if line_stripped.startswith(quote) and line_stripped.endswith(quote):
return line_stripped.strip(quote)
return None
def _is_user_apps_section_start(line: str) -> bool:
"""Check if line starts USER_DEFINED_APPS section."""
return "USER_DEFINED_APPS" in line and "=" in line
def _iterate_user_apps_lines(content: str):
"""Generator that yields lines within USER_DEFINED_APPS section."""
lines = content.split("\n")
in_user_apps = False
for line in lines:
if _is_user_apps_section_start(line):
in_user_apps = True
elif in_user_apps and line.strip() == "]":
break
elif in_user_apps:
yield line
def extract_existing_apps(content: str) -> set:
existing_apps = set()
for line in _iterate_user_apps_lines(content):
app_name = _parse_app_entry_from_line(line)
if app_name:
existing_apps.add(app_name)
return existing_apps
def format_app_entries(apps: list) -> list:
return [f' "{app}",' for app in apps]
def is_app_section_start(line: str) -> bool:
return "=" in line and any(keyword in line for keyword in ("BUILT_IN_APPS", "THIRD_PARTY_APPS", "INSTALLED_APPS"))
def is_user_apps_closing_bracket(lines: list, current_idx: int) -> bool:
for next_idx in range(current_idx + 1, min(current_idx + 3, len(lines))):
next_line = lines[next_idx].strip()
if next_line and "BUILT_IN_APPS" in next_line:
return True
return True # Default to True if we're inside USER_DEFINED_APPS
def split_bracket_line(line: str, apps_to_add: list) -> list:
bracket_pos = line.rfind("]")
before_bracket = line[:bracket_pos].rstrip() if bracket_pos > 0 else line.rstrip()
after_bracket = line[bracket_pos:] if bracket_pos > 0 else "]"
result = [before_bracket]
result.extend(format_app_entries(apps_to_add))
result.append(after_bracket)
return result
def insert_apps_into_user_defined_apps(content: str, apps_to_add: list) -> str:
lines = content.split("\n")
new_lines = []
in_user_apps = False
apps_added = False
for i, line in enumerate(lines):
if _is_user_apps_section_start(line) and not apps_added:
in_user_apps = True
if "]" in line:
new_lines.extend(split_bracket_line(line, apps_to_add))
apps_added = True
in_user_apps = False
Args:
base_dir: Base directory where files will be created
files: List of tuples (filename, template_path, context)
prefix: Optional prefix for success message
should_format: Whether to format the created files
"""
for file_spec in files:
if len(file_spec) == 3:
filename, template_path, context = file_spec
else:
new_lines.append(line)
# Support (filename, template_path) format with empty context
filename, template_path = file_spec
context = {}
filepath = os.path.join(base_dir, filename)
message = f"Created {prefix}{filename}" if prefix else f"Created {filename}"
CommonUtils.create_file_from_template(
filepath, template_path, context, message, should_format=should_format
)
elif in_user_apps and line.strip() == "]" and not apps_added:
if is_user_apps_closing_bracket(lines, i):
new_lines.extend(format_app_entries(apps_to_add))
new_lines.append(line)
apps_added = True
in_user_apps = False
else:
new_lines.append(line)
@staticmethod
def get_package_name(project_dir: str) -> str:
"""Get package name, defaulting to 'backend' if project_dir is '.' or empty."""
return "backend" if project_dir == "." or not project_dir else project_dir
elif in_user_apps and is_app_section_start(line) and not apps_added:
new_lines.extend(format_app_entries(apps_to_add))
new_lines.append("]")
new_lines.append(line)
apps_added = True
in_user_apps = False
@staticmethod
@contextmanager
def change_cwd(path: str):
original_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(original_cwd)
else:
new_lines.append(line)
if not apps_added:
UIFormatter.print_error("Could not find or update USER_DEFINED_APPS section in base.py")
@staticmethod
def _parse_app_entry_from_line(line: str) -> str | None:
"""Extract app name from a line in USER_DEFINED_APPS section."""
line_stripped = line.strip().strip(",").strip()
for quote in ('"', "'"):
if line_stripped.startswith(quote) and line_stripped.endswith(quote):
return line_stripped.strip(quote)
return None
return "\n".join(new_lines)
@staticmethod
def _is_user_apps_section_start(line: str) -> bool:
"""Check if line starts USER_DEFINED_APPS section."""
return "USER_DEFINED_APPS" in line and "=" in line
@staticmethod
def _iterate_user_apps_lines(content: str):
"""Generator that yields lines within USER_DEFINED_APPS section."""
lines = content.split("\n")
in_user_apps = False
def replace_app_in_user_defined_apps(content: str, old_app: str, new_app: str) -> str | None:
"""Replace an app entry in USER_DEFINED_APPS with a new one."""
lines = content.split("\n")
new_lines = []
replaced = False
for line in lines:
if CommonUtils._is_user_apps_section_start(line):
in_user_apps = True
elif in_user_apps and line.strip() == "]":
break
elif in_user_apps:
yield line
for line in lines:
if not replaced and _parse_app_entry_from_line(line) == old_app:
# Try to preserve indentation
indent_idx = 0
for char in line:
if char.isspace():
indent_idx += 1
@staticmethod
def extract_existing_apps(content: str) -> set:
existing_apps = set()
for line in CommonUtils._iterate_user_apps_lines(content):
app_name = CommonUtils._parse_app_entry_from_line(line)
if app_name:
existing_apps.add(app_name)
return existing_apps
@staticmethod
def format_app_entries(apps: list) -> list:
return [f' "{app}",' for app in apps]
@staticmethod
def is_app_section_start(line: str) -> bool:
return "=" in line and any(
keyword in line for keyword in ("BUILT_IN_APPS", "THIRD_PARTY_APPS", "INSTALLED_APPS")
)
@staticmethod
def is_user_apps_closing_bracket(lines: list, current_idx: int) -> bool:
for next_idx in range(current_idx + 1, min(current_idx + 3, len(lines))):
next_line = lines[next_idx].strip()
if next_line and "BUILT_IN_APPS" in next_line:
return True
return True # Default to True if we're inside USER_DEFINED_APPS
@staticmethod
def split_bracket_line(line: str, apps_to_add: list) -> list:
bracket_pos = line.rfind("]")
before_bracket = line[:bracket_pos].rstrip() if bracket_pos > 0 else line.rstrip()
after_bracket = line[bracket_pos:] if bracket_pos > 0 else "]"
result = [before_bracket]
result.extend(CommonUtils.format_app_entries(apps_to_add))
result.append(after_bracket)
return result
@staticmethod
def insert_apps_into_user_defined_apps(content: str, apps_to_add: list) -> str:
lines = content.split("\n")
new_lines = []
in_user_apps = False
apps_added = False
for i, line in enumerate(lines):
if CommonUtils._is_user_apps_section_start(line) and not apps_added:
in_user_apps = True
if "]" in line:
new_lines.extend(CommonUtils.split_bracket_line(line, apps_to_add))
apps_added = True
in_user_apps = False
else:
break
indent = line[:indent_idx] if indent_idx > 0 else " "
new_lines.append(f'{indent}"{new_app}",')
replaced = True
else:
new_lines.append(line)
new_lines.append(line)
elif in_user_apps and line.strip() == "]" and not apps_added:
if CommonUtils.is_user_apps_closing_bracket(lines, i):
new_lines.extend(CommonUtils.format_app_entries(apps_to_add))
new_lines.append(line)
apps_added = True
in_user_apps = False
else:
new_lines.append(line)
elif in_user_apps and CommonUtils.is_app_section_start(line) and not apps_added:
new_lines.extend(CommonUtils.format_app_entries(apps_to_add))
new_lines.append("]")
new_lines.append(line)
apps_added = True
in_user_apps = False
else:
new_lines.append(line)
if not apps_added:
UIFormatter.print_error("Could not find or update USER_DEFINED_APPS section in base.py")
return None
if replaced:
return "\n".join(new_lines)
return None
@staticmethod
def replace_app_in_user_defined_apps(content: str, old_app: str, new_app: str) -> str | None:
"""Replace an app entry in USER_DEFINED_APPS with a new one."""
lines = content.split("\n")
new_lines = []
replaced = False
def calculate_app_module_paths(app_names: list, metadata: dict) -> list:
nested = bool(metadata.get("nested_apps"))
nested_dir_name = metadata.get("nested_dir") if nested else None
return [calculate_app_module_path(app_name, nested, nested_dir_name) for app_name in app_names]
for line in lines:
if not replaced and CommonUtils._parse_app_entry_from_line(line) == old_app:
# Try to preserve indentation
indent_idx = 0
for char in line:
if char.isspace():
indent_idx += 1
else:
break
indent = line[:indent_idx] if indent_idx > 0 else " "
new_lines.append(f'{indent}"{new_app}",')
replaced = True
else:
new_lines.append(line)
def calculate_app_module_path(app_name: str, nested: bool, nested_dir: str | None) -> str:
return f"{nested_dir}.{app_name}" if nested and nested_dir else app_name
def is_django_project(directory: str = None) -> bool:
if directory is None:
directory = os.getcwd()
manage_py_path = os.path.join(directory, "manage.py")
return os.path.exists(manage_py_path)
def find_project_dir(search_dir: str = None) -> tuple[str | None, str | None]:
if search_dir is None:
search_dir = os.getcwd()
for item in os.listdir(search_dir):
if os.path.isdir(item) and not item.startswith(".") and item != "__pycache__":
candidate = os.path.join(search_dir, item)
base_py = os.path.join(candidate, "settings", "base.py")
if os.path.exists(base_py):
return candidate, base_py
return None, None
def find_settings_path(search_dir: str = None) -> str | None:
project_dir, settings_base_path = find_project_dir(search_dir)
if settings_base_path:
return os.path.dirname(settings_base_path)
return None
def get_base_settings_path(project_root: str, project_name: str) -> str:
settings_dir = os.path.join(project_root, project_name, "settings")
return os.path.join(settings_dir, "base.py")
def read_base_settings(project_root: str, project_name: str) -> str | None:
base_settings_path = get_base_settings_path(project_root, project_name)
if not os.path.exists(base_settings_path):
if replaced:
return "\n".join(new_lines)
return None
with open(base_settings_path) as f:
return f.read()
@staticmethod
def calculate_app_module_paths(app_names: list, metadata: dict) -> list:
nested = bool(metadata.get("nested_apps"))
nested_dir_name = metadata.get("nested_dir") if nested else None
return [CommonUtils.calculate_app_module_path(app_name, nested, nested_dir_name) for app_name in app_names]
@staticmethod
def calculate_app_module_path(app_name: str, nested: bool, nested_dir: str | None) -> str:
return f"{nested_dir}.{app_name}" if nested and nested_dir else app_name
def detect_nested_structure_from_settings(
base_settings_path: str, search_dir: str = None
) -> tuple[bool, str | None, str]:
if search_dir is None:
search_dir = os.getcwd()
@staticmethod
def is_django_project(directory: str = None) -> bool:
if directory is None:
directory = os.getcwd()
manage_py_path = os.path.join(directory, "manage.py")
return os.path.exists(manage_py_path)
@staticmethod
def find_project_dir(search_dir: str = None) -> tuple[str | None, str | None]:
if search_dir is None:
search_dir = os.getcwd()
for item in os.listdir(search_dir):
if os.path.isdir(item) and not item.startswith(".") and item != "__pycache__":
candidate = os.path.join(search_dir, item)
base_py = os.path.join(candidate, "settings", "base.py")
if os.path.exists(base_py):
return candidate, base_py
return None, None
@staticmethod
def find_settings_path(search_dir: str = None) -> str | None:
project_dir, settings_base_path = CommonUtils.find_project_dir(search_dir)
if settings_base_path:
return os.path.dirname(settings_base_path)
return None
@staticmethod
def get_base_settings_path(project_root: str, project_name: str) -> str:
settings_dir = os.path.join(project_root, project_name, "settings")
return os.path.join(settings_dir, "base.py")
@staticmethod
def read_base_settings(project_root: str, project_name: str) -> str | None:
base_settings_path = CommonUtils.get_base_settings_path(project_root, project_name)
if not os.path.exists(base_settings_path):
return None
with open(base_settings_path) as f:
return f.read()
@staticmethod
def detect_nested_structure_from_settings(
base_settings_path: str, search_dir: str = None
) -> tuple[bool, str | None, str]:
if search_dir is None:
search_dir = os.getcwd()
if not os.path.exists(base_settings_path):
return False, None, search_dir
try:
with open(base_settings_path) as f:
base_content = f.read()
for line in CommonUtils._iterate_user_apps_lines(base_content):
app_label = CommonUtils._parse_app_entry_from_line(line)
if app_label and "." in app_label:
nested_dir_name = app_label.split(".", 1)[0]
nested_path = os.path.join(search_dir, nested_dir_name)
if os.path.isdir(nested_path):
return True, nested_dir_name, nested_path
except Exception:
pass
if not os.path.exists(base_settings_path):
return False, None, search_dir
try:
with open(base_settings_path) as f:
base_content = f.read()
@staticmethod
def get_djinit_config(project_root: str = None) -> dict | None:
"""Read .djinit configuration file."""
import json
for line in _iterate_user_apps_lines(base_content):
app_label = _parse_app_entry_from_line(line)
if app_label and "." in app_label:
nested_dir_name = app_label.split(".", 1)[0]
nested_path = os.path.join(search_dir, nested_dir_name)
if os.path.isdir(nested_path):
return True, nested_dir_name, nested_path
except Exception:
pass
if project_root is None:
project_root = os.getcwd()
return False, None, search_dir
config_path = os.path.join(project_root, ".djinit")
if not os.path.exists(config_path):
# Try finding it in parent directories
parent = os.path.dirname(project_root)
if parent and parent != project_root:
return CommonUtils.get_djinit_config(parent)
return None
def get_djinit_config(project_root: str = None) -> dict | None:
"""Read .djinit configuration file."""
import json
if project_root is None:
project_root = os.getcwd()
config_path = os.path.join(project_root, ".djinit")
if not os.path.exists(config_path):
# Try finding it in parent directories
parent = os.path.dirname(project_root)
if parent and parent != project_root:
return get_djinit_config(parent)
return None
try:
with open(config_path) as f:
return json.load(f)
except Exception:
return None
try:
with open(config_path) as f:
return json.load(f)
except Exception:
return None
+18 -19
View File
@@ -7,12 +7,7 @@ This module provides functions to create Django projects and apps without requir
import os
from djinit.core.config import DJANGO_VERSION
from djinit.utils.common import (
create_directory_with_init,
create_file_from_template,
create_files_from_templates,
create_init_file,
)
from djinit.utils.common import CommonUtils
class DjangoHelper:
@@ -24,7 +19,7 @@ class DjangoHelper:
os.makedirs(directory, exist_ok=True)
manage_py_path = os.path.join(directory, "manage.py")
create_file_from_template(
CommonUtils.create_file_from_template(
manage_py_path, "project/manage.py-tpl", {"project_name": project_name}, "Created manage.py"
)
os.chmod(manage_py_path, 0o755)
@@ -33,10 +28,10 @@ class DjangoHelper:
return
project_config_dir = os.path.join(directory, project_name)
create_directory_with_init(project_config_dir, f"Created {project_name}/__init__.py")
CommonUtils.create_directory_with_init(project_config_dir, f"Created {project_name}/__init__.py")
settings_dir = os.path.join(project_config_dir, "settings")
create_directory_with_init(settings_dir, f"Created {project_name}/settings/__init__.py")
CommonUtils.create_directory_with_init(settings_dir, f"Created {project_name}/settings/__init__.py")
base_context = {"project_name": project_name, "app_names": []}
settings_files = [
@@ -44,15 +39,19 @@ class DjangoHelper:
("development.py", "config/settings/development.py-tpl", {}),
("production.py", "config/settings/production.py-tpl", {}),
]
create_files_from_templates(settings_dir, settings_files, f"{project_name}/settings/")
CommonUtils.create_files_from_templates(settings_dir, settings_files, f"{project_name}/settings/")
urls_context = {"url_type": "project", "project_name": project_name, "django_version": DjangoHelper.DJANGO_VERSION}
urls_context = {
"url_type": "project",
"project_name": project_name,
"django_version": DjangoHelper.DJANGO_VERSION,
}
project_files = [
("urls.py", "config/urls.py-tpl", urls_context),
("wsgi.py", "config/wsgi.py-tpl", {}),
("asgi.py", "config/asgi.py-tpl", {}),
]
create_files_from_templates(project_config_dir, project_files, f"{project_name}/")
CommonUtils.create_files_from_templates(project_config_dir, project_files, f"{project_name}/")
except Exception as e:
from djinit.utils.exceptions import DjinitError
@@ -66,20 +65,20 @@ class DjangoHelper:
os.makedirs(app_dir, exist_ok=True)
# Prepare context
app_config_name = app_name.title().replace('_', '')
app_config_name = app_name.title().replace("_", "")
context = {
"app_name": app_name,
"app_config_name": app_config_name,
"django_version": DjangoHelper.DJANGO_VERSION
"django_version": DjangoHelper.DJANGO_VERSION,
}
# Use provided app_module or default to app_name (path calculation handled by caller)
if not app_module:
app_module = app_name
context["app_module"] = app_module
create_init_file(app_dir, f"Created {app_name}/__init__.py")
CommonUtils.create_init_file(app_dir, f"Created {app_name}/__init__.py")
app_files = [
("apps.py", "components/apps.py-tpl", context),
@@ -92,10 +91,10 @@ class DjangoHelper:
("tests.py", "components/tests.py-tpl", context),
]
create_files_from_templates(app_dir, app_files, f"{app_name}/")
CommonUtils.create_files_from_templates(app_dir, app_files, f"{app_name}/")
migrations_dir = os.path.join(app_dir, "migrations")
create_directory_with_init(migrations_dir, f"Created {app_name}/migrations/__init__.py")
CommonUtils.create_directory_with_init(migrations_dir, f"Created {app_name}/migrations/__init__.py")
except Exception as e:
from djinit.utils.exceptions import DjinitError