harshadh01 commited on
Commit
6bdf1be
·
verified ·
1 Parent(s): 83673a6

create folder error update

Browse files
Files changed (1) hide show
  1. settings_generator.py +119 -117
settings_generator.py CHANGED
@@ -1,117 +1,119 @@
1
- import re
2
- from pathlib import Path
3
-
4
-
5
- def ensure_dir(path: Path):
6
- path.mkdir(parents=True, exist_ok=True)
7
-
8
-
9
- def _add_to_installed_apps(content: str, app_name: str) -> str:
10
- """
11
- Safely add an app to INSTALLED_APPS if not already present.
12
- """
13
- pattern = re.compile(r"INSTALLED_APPS\s*=\s*\[(.*?)\]", re.DOTALL)
14
- match = pattern.search(content)
15
-
16
- if not match:
17
- return content
18
-
19
- apps_block = match.group(1)
20
-
21
- if f"'{app_name}'" in apps_block or f'"{app_name}"' in apps_block:
22
- return content
23
-
24
- updated_apps = apps_block.rstrip() + f"\n '{app_name}',"
25
- return content[:match.start(1)] + updated_apps + content[match.end(1):]
26
-
27
-
28
- def update_settings_py(settings_path, db_spec, app_names=None):
29
- settings_path = Path(settings_path)
30
- content = settings_path.read_text(encoding="utf-8")
31
-
32
- # --------------------------------------------------
33
- # 1. Ensure BASE_DIR
34
- # --------------------------------------------------
35
- if "BASE_DIR" not in content:
36
- content = (
37
- "from pathlib import Path\n\n"
38
- "BASE_DIR = Path(__file__).resolve().parent.parent\n\n"
39
- + content
40
- )
41
-
42
- # --------------------------------------------------
43
- # 2. Ensure rest_framework + project apps
44
- # --------------------------------------------------
45
- content = _add_to_installed_apps(content, "rest_framework")
46
-
47
- if app_names:
48
- for app in app_names:
49
- content = _add_to_installed_apps(content, app)
50
-
51
- # --------------------------------------------------
52
- # 3. Database config (ONLY if NOT sqlite)
53
- # --------------------------------------------------
54
- engine = (db_spec or {}).get("engine", "").lower()
55
-
56
- if engine and engine != "sqlite":
57
- engine_map = {
58
- "postgresql": "django.db.backends.postgresql",
59
- "postgres": "django.db.backends.postgresql",
60
- "mysql": "django.db.backends.mysql",
61
- "mariadb": "django.db.backends.mysql",
62
- }
63
-
64
- django_engine = engine_map.get(engine)
65
- if not django_engine:
66
- raise ValueError(f"Unsupported DB engine: {engine}")
67
-
68
- database_block = f"""
69
- DATABASES = {{
70
- 'default': {{
71
- 'ENGINE': '{django_engine}',
72
- 'NAME': '{db_spec.get("name")}',
73
- 'USER': '{db_spec.get("user", "")}',
74
- 'PASSWORD': '{db_spec.get("password", "")}',
75
- 'HOST': '{db_spec.get("host", "")}',
76
- 'PORT': '{db_spec.get("port", "")}',
77
- }}
78
- }}
79
- """.strip()
80
-
81
- content = re.sub(
82
- r"DATABASES\s*=\s*\{.*?\}\s*\}\s*",
83
- database_block + "\n\n",
84
- content,
85
- flags=re.DOTALL
86
- )
87
-
88
- # --------------------------------------------------
89
- # 4. Static & Media (idempotent)
90
- # --------------------------------------------------
91
- if "STATIC_ROOT" not in content:
92
- content += """
93
-
94
- # Static files
95
- STATIC_URL = "/static/"
96
- STATIC_ROOT = BASE_DIR / "staticfiles"
97
- STATICFILES_DIRS = [BASE_DIR / "static"]
98
-
99
- # Media files
100
- MEDIA_URL = "/media/"
101
- MEDIA_ROOT = BASE_DIR / "media"
102
- """
103
-
104
- # --------------------------------------------------
105
- # 5. Write file
106
- # --------------------------------------------------
107
- settings_path.write_text(content, encoding="utf-8")
108
-
109
- # --------------------------------------------------
110
- # 6. Create folders
111
- # --------------------------------------------------
112
- project_root = settings_path.parent.parent
113
- ensure_dir(project_root / "static")
114
- ensure_dir(project_root / "media")
115
- ensure_dir(project_root / "staticfiles")
116
-
117
- print("✅ settings.py updated correctly")
 
 
 
1
+ import re
2
+ from pathlib import Path
3
+
4
+
5
+ def ensure_dir(path: Path):
6
+ path.mkdir(parents=True, exist_ok=True)
7
+
8
+
9
+ def _add_to_installed_apps(content: str, app_name: str) -> str:
10
+ """
11
+ Safely add an app to INSTALLED_APPS if not already present.
12
+ """
13
+ pattern = re.compile(r"INSTALLED_APPS\s*=\s*\[(.*?)\]", re.DOTALL)
14
+ match = pattern.search(content)
15
+
16
+ if not match:
17
+ return content
18
+
19
+ apps_block = match.group(1)
20
+
21
+ if f"'{app_name}'" in apps_block or f'"{app_name}"' in apps_block:
22
+ return content
23
+
24
+ updated_apps = apps_block.rstrip() + f"\n '{app_name}',"
25
+ return content[:match.start(1)] + updated_apps + content[match.end(1):]
26
+
27
+
28
+ def update_settings_py(settings_path, db_spec, app_names=None):
29
+ settings_path = Path(settings_path)
30
+ content = settings_path.read_text(encoding="utf-8")
31
+
32
+ # --------------------------------------------------
33
+ # 1. Ensure BASE_DIR
34
+ # --------------------------------------------------
35
+ if "BASE_DIR" not in content:
36
+ content = (
37
+ "from pathlib import Path\n\n"
38
+ "BASE_DIR = Path(__file__).resolve().parent.parent\n\n"
39
+ + content
40
+ )
41
+
42
+ # --------------------------------------------------
43
+ # 2. Ensure rest_framework + project apps
44
+ # --------------------------------------------------
45
+ content = _add_to_installed_apps(content, "rest_framework")
46
+
47
+ if app_names:
48
+ for app in app_names:
49
+ content = _add_to_installed_apps(content, app)
50
+
51
+ # --------------------------------------------------
52
+ # 3. Database config (ONLY if NOT sqlite)
53
+ # --------------------------------------------------
54
+ engine = (db_spec or {}).get("engine", "").lower()
55
+
56
+ if engine and engine != "sqlite":
57
+ engine_map = {
58
+ "postgresql": "django.db.backends.postgresql",
59
+ "postgres": "django.db.backends.postgresql",
60
+ "mysql": "django.db.backends.mysql",
61
+ "mariadb": "django.db.backends.mysql",
62
+ }
63
+
64
+ django_engine = engine_map.get(engine)
65
+ if not django_engine:
66
+ raise ValueError(f"Unsupported DB engine: {engine}")
67
+
68
+ database_block = f"""
69
+ DATABASES = {{
70
+ 'default': {{
71
+ 'ENGINE': '{django_engine}',
72
+ 'NAME': '{db_spec.get("name")}',
73
+ 'USER': '{db_spec.get("user", "")}',
74
+ 'PASSWORD': '{db_spec.get("password", "")}',
75
+ 'HOST': '{db_spec.get("host", "")}',
76
+ 'PORT': '{db_spec.get("port", "")}',
77
+ }}
78
+ }}
79
+ """.strip()
80
+
81
+ content = re.sub(
82
+ r"DATABASES\s*=\s*\{.*?\}\s*\}\s*",
83
+ database_block + "\n\n",
84
+ content,
85
+ flags=re.DOTALL
86
+ )
87
+
88
+ # --------------------------------------------------
89
+ # 4. Static & Media (idempotent)
90
+ # --------------------------------------------------
91
+ if "STATIC_ROOT" not in content:
92
+ content += """
93
+
94
+ # Static files
95
+ STATIC_URL = "/static/"
96
+ STATIC_ROOT = BASE_DIR / "staticfiles"
97
+ STATICFILES_DIRS = [BASE_DIR / "static"]
98
+
99
+ # Media files
100
+ MEDIA_URL = "/media/"
101
+ MEDIA_ROOT = BASE_DIR / "media"
102
+ """
103
+
104
+ # --------------------------------------------------
105
+ # 5. Write file
106
+ # --------------------------------------------------
107
+ settings_path.write_text(content, encoding="utf-8")
108
+
109
+ # --------------------------------------------------
110
+ # 6. Create folders
111
+ # --------------------------------------------------
112
+ project_root = settings_path.parents[2]
113
+
114
+ ensure_dir(project_root / "static")
115
+ ensure_dir(project_root / "media")
116
+ ensure_dir(project_root / "staticfiles")
117
+
118
+
119
+ #print("✅ settings.py updated correctly")