Add PythonDocumentationGenerator method to collect all namespaces

This commit is contained in:
2026-04-08 14:24:44 +02:00
parent 54e3a30ef0
commit d0e2855b58
6 changed files with 40 additions and 3 deletions
+1
View File
@@ -14,3 +14,4 @@
- Add feature to get the names of all sub-namespaces of a `core.python.namespaces.PythonPackageNamespace`
- Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances
- Add string representation for python namespace objects
- Add `PythonDocumentationGenerator` method to collect all namespaces
+23
View File
@@ -14,6 +14,8 @@
from __future__ import annotations
from ...utils import ExistingDirectory, assert_that_is_instance
from .namespaces import PythonModuleNamespace, PythonPackageNamespace, PythonNamespace
import pathlib
__all__ = [
'PythonDocumentationGenerator'
@@ -35,3 +37,24 @@ class PythonDocumentationGenerator:
self.project_directory = project_directory
self.docs_directory = docs_directory
def _namespace(self, directory: pathlib.Path) -> dict[str, PythonNamespace]:
namespaces = dict()
for entry in directory.iterdir():
if entry.is_dir():
namespaces[entry.name] = PythonPackageNamespace(entry.name, self._namespace(entry))
elif entry.suffix == '.py':
namespaces[entry.stem] = PythonModuleNamespace(entry.stem)
return namespaces
def namespace(self) -> PythonPackageNamespace:
'''
Returns the project as a namespace.
:return: The project as a namespace.
:rtype: PythonPackageNamespace
'''
return PythonPackageNamespace('src', self._namespace(pathlib.Path((self.project_directory / 'src').as_posix())))
@@ -1,4 +1,8 @@
import sys
sys.path.append('.')
from src.jcloud_docsgen.core.python import PythonDocumentationGenerator
from src.jcloud_docsgen.core.python.namespaces import PythonModuleNamespace, PythonPackageNamespace
import pytest
from src.jcloud_docsgen.utils import ExistingDirectory
@@ -15,3 +19,12 @@ from src.jcloud_docsgen.utils import ExistingDirectory
def test_PythonDocumentationGenerator_type_exceptions(project_directory, docs_directory):
with pytest.raises(TypeError):
PythonDocumentationGenerator(project_directory, docs_directory)
@pytest.mark.parametrize('python_documentation_generator,expected', [
(PythonDocumentationGenerator(ExistingDirectory('tests/unit/core/python/_core/test_project_dirs/pdir_1'), ExistingDirectory('tests/unit/core/python/_core/test_project_dirs/pdir_1/docs')), PythonPackageNamespace('src', {})),
(PythonDocumentationGenerator(ExistingDirectory('tests/unit/core/python/_core/test_project_dirs/pdir_2'), ExistingDirectory('tests/unit/core/python/_core/test_project_dirs/pdir_2/docs')), PythonPackageNamespace('src', {'module': PythonModuleNamespace('module')})),
(PythonDocumentationGenerator(ExistingDirectory('tests/unit/core/python/_core/test_project_dirs/pdir_3'), ExistingDirectory('tests/unit/core/python/_core/test_project_dirs/pdir_3/docs')), PythonPackageNamespace('src', {'module': PythonModuleNamespace('module'), 'pkg': PythonPackageNamespace('pkg', {'module2': PythonModuleNamespace('module2')})})),
])
def test_PythonDocumentationGenerator_collect_modules(python_documentation_generator: PythonDocumentationGenerator, expected):
print('MODS:', python_documentation_generator.namespace())
assert python_documentation_generator.namespace() == expected