192 project_root: Path, path: str, force: bool,
193 dry_run: bool =
False, test: bool =
False,
194 verbose: bool =
False,
196 func =
"create_new_file"
197 _dbg(
"OCT-GENERATOR", func,
"creating a new Option C file", 3)
199 if not target.is_absolute():
200 target = project_root / target
202 if target.suffix !=
".py":
203 raise click.ClickException(
204 f
"oct new only creates Python (.py) files. Got: '{target.suffix or '(no extension)'}'"
209 target.resolve().relative_to(project_root.resolve())
211 raise click.ClickException(
212 f
"Target path {target} is outside the project root ({project_root}). "
213 "Use a relative path or a path inside the project."
216 if target.exists()
and not force:
217 raise click.ClickException(f
"File already exists: {target}")
222 test_target = project_root /
"tests" / f
"test_{target.stem}.py"
223 if test_target.exists()
and not force:
224 raise click.ClickException(f
"Test file already exists: {test_target}")
227 rel = target.relative_to(project_root).as_posix()
229 main_content = header + DOCSTRING_TEMPLATE.format(domain=
_derive_domain(rel)) + IMPORT_BLOCK
233 test_rel = test_target.relative_to(project_root).as_posix()
236 test_docstring = TEST_DOCSTRING_TEMPLATE.format(module_name=target.stem)
237 test_body = TEST_BODY_TEMPLATE.format(
238 import_line=f
"from {import_path} import *",
241 test_content = test_header + test_docstring + test_body
244 if not target.parent.exists():
245 print(f
"[DRY RUN] Would create directory: {target.parent}")
247 current = target.parent
248 while current != project_root
and current != current.parent:
250 current.relative_to(project_root)
253 init_file = current /
"__init__.py"
254 if not init_file.exists():
255 print(f
"[DRY RUN] Would create package marker: {init_file.relative_to(project_root)}")
256 current = current.parent
257 print(f
"[DRY RUN] Would write file: {target}")
259 print(f
"[DRY RUN] Would write test file: {test_target}")
266 target.parent.mkdir(parents=
True, exist_ok=
True)
269 current = target.parent
270 while current != project_root
and current != current.parent:
272 current.relative_to(project_root)
275 init_file = current /
"__init__.py"
276 if not init_file.exists():
277 init_rel = init_file.relative_to(project_root).as_posix()
278 init_file.write_text(
281 print(f
" Created package marker: {init_file.relative_to(project_root)}")
282 current = current.parent
284 target.write_text(main_content, encoding=
"utf-8")
285 print(f
"Created Option C-compliant file: {target}")
290 if test_target
and test_content:
291 test_target.parent.mkdir(parents=
True, exist_ok=
True)
292 test_target.write_text(test_content, encoding=
"utf-8")
293 print(f
"Created companion test file: {test_target}")