125 project_root: Path, path: str, force: bool,
126 dry_run: bool =
False, test: bool =
False,
127 verbose: bool =
False,
130 if not target.is_absolute():
131 target = project_root / target
133 if target.suffix !=
".py":
134 raise click.ClickException(
135 f
"oct new only creates Python (.py) files. Got: '{target.suffix or '(no extension)'}'"
140 target.resolve().relative_to(project_root.resolve())
142 raise click.ClickException(
143 f
"Target path {target} is outside the project root ({project_root}). "
144 "Use a relative path or a path inside the project."
147 if target.exists()
and not force:
148 raise click.ClickException(f
"File already exists: {target}")
153 test_target = project_root /
"tests" / f
"test_{target.stem}.py"
154 if test_target.exists()
and not force:
155 raise click.ClickException(f
"Test file already exists: {test_target}")
158 rel = target.relative_to(project_root).as_posix()
160 main_content = header + IMPORT_BLOCK + DOCSTRING_TEMPLATE
164 test_rel = test_target.relative_to(project_root).as_posix()
167 test_docstring = TEST_DOCSTRING_TEMPLATE.format(module_name=target.stem)
168 test_body = TEST_BODY_TEMPLATE.format(
169 import_line=f
"from {import_path} import *",
172 test_content = test_header + test_docstring + test_body
175 if not target.parent.exists():
176 print(f
"[DRY RUN] Would create directory: {target.parent}")
178 current = target.parent
179 while current != project_root
and current != current.parent:
181 current.relative_to(project_root)
184 init_file = current /
"__init__.py"
185 if not init_file.exists():
186 print(f
"[DRY RUN] Would create package marker: {init_file.relative_to(project_root)}")
187 current = current.parent
188 print(f
"[DRY RUN] Would write file: {target}")
190 print(f
"[DRY RUN] Would write test file: {test_target}")
197 target.parent.mkdir(parents=
True, exist_ok=
True)
200 current = target.parent
201 while current != project_root
and current != current.parent:
203 current.relative_to(project_root)
206 init_file = current /
"__init__.py"
207 if not init_file.exists():
208 init_file.write_text(
"", encoding=
"utf-8")
209 print(f
" Created package marker: {init_file.relative_to(project_root)}")
210 current = current.parent
212 target.write_text(main_content, encoding=
"utf-8")
213 print(f
"Created Option C-compliant file: {target}")
218 if test_target
and test_content:
219 test_target.parent.mkdir(parents=
True, exist_ok=
True)
220 test_target.write_text(test_content, encoding=
"utf-8")
221 print(f
"Created companion test file: {test_target}")