From 5791358b4143d4e7bd3bb040fdaf4d9b46890830 Mon Sep 17 00:00:00 2001 From: Jakob Scheid Date: Sun, 27 Sep 2026 14:26:26 +0200 Subject: [PATCH] feat(python-generator): add force argument to generator Added a force argument to PythonDocumentationGenerator.generate_documentation to control whether the content of the output directory should be overwritten or not, if it is not empty. If the force argument is True, the output directory content will first be deleted recursively. If it is False and the output directory is not empty, a DirectoryNotEmptyError will be raised. --- src/jcloud_docsgen/core/python/_core.py | 27 +++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/jcloud_docsgen/core/python/_core.py b/src/jcloud_docsgen/core/python/_core.py index 73b6b26..af783b3 100644 --- a/src/jcloud_docsgen/core/python/_core.py +++ b/src/jcloud_docsgen/core/python/_core.py @@ -22,7 +22,13 @@ from typing import TypeAlias import docstring_parser -from ...exceptions import InvalidNamespaceError, PythonArgumentStructureError +import shutil + +from ...exceptions import ( + DirectoryNotEmptyError, + InvalidNamespaceError, + PythonArgumentStructureError, +) from ...utils import get_relative_path_segments, non_empty_str from .arguments import PythonArgumentKind, PythonFunctionArgument from .definitions import ( @@ -1114,12 +1120,29 @@ class PythonDocumentationGenerator: return self._namespace(self.project_directory) - def generate_documentation(self) -> None: + def generate_documentation(self, force: bool = False) -> None: """ Generates the full project documentation and writes it to the documentation directory. + + :param force: Indicates whether existing contents of the output + directory shall be overwritten. If ``True``, all + contents of the output directory will be deleted + first. + :type force: bool + + :raises DirectoryNotEmptyError: If ``force`` is ``False`` and + the output directory is not + empty. """ + if self.docs_directory.exists() and list( + self.docs_directory.iterdir() + ): + if force: + shutil.rmtree(self.docs_directory) + else: + raise DirectoryNotEmptyError(str(self.docs_directory)) self.docs_directory.mkdir(parents=True, exist_ok=True) _generate_package_namespace_documentation(