generated from jCloud/repository-template
Add class for Python modules
This commit is contained in:
@@ -13,23 +13,61 @@
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
from ...utils import ExistingDirectory, assert_that_is_instance
|
||||
from .namespaces import PythonModuleNamespace, PythonPackageNamespace, PythonNamespace
|
||||
from typing import List
|
||||
from ...utils import ExistingDirectory, assert_that_is_instance, ExistingFile
|
||||
from .namespaces import PythonModuleNamespace, PythonPackageNamespace
|
||||
from .arguments import PythonASTArgumentsListParser
|
||||
from .definitions import PythonDefinition, PythonFunctionDefinition, PythonAsyncFunctionDefinition, PythonClassDefinition
|
||||
import pathlib
|
||||
import ast
|
||||
from collections.abc import Iterator
|
||||
|
||||
__all__ = [
|
||||
'PythonDocumentationGenerator'
|
||||
'PythonModuleDocumentationGenerator',
|
||||
'PythonDocumentationGenerator',
|
||||
]
|
||||
|
||||
# class PythonModuleDocumentationGenerator:
|
||||
# '''
|
||||
# A documentation generator for a Python module.
|
||||
def _collect_definitions(tree_or_node) -> Iterator[PythonDefinition]:
|
||||
'''
|
||||
Collects all definitions of the tree or node.
|
||||
|
||||
:param tree_or_node: The tree or node.
|
||||
|
||||
# '''
|
||||
:return: The definitions.
|
||||
:rtype: Iterator[PythonDefinition]
|
||||
'''
|
||||
|
||||
# def __init__(self, module_path: )
|
||||
for node in tree_or_node.body:
|
||||
if isinstance(node, ast.FunctionDef):
|
||||
yield PythonFunctionDefinition.from_node(node)
|
||||
if isinstance(node, ast.AsyncFunctionDef):
|
||||
yield PythonAsyncFunctionDefinition.from_node(node)
|
||||
if isinstance(node, ast.ClassDef):
|
||||
yield PythonClassDefinition.from_node(node)
|
||||
|
||||
class PythonModuleDocumentationGenerator:
|
||||
'''
|
||||
A documentation generator for a Python module.
|
||||
|
||||
:param module_path: The path of the module file.
|
||||
:type module_path: ExistingFile
|
||||
'''
|
||||
|
||||
def __init__(self, module_path: ExistingFile) -> None:
|
||||
assert_that_is_instance(module_path, ExistingFile)
|
||||
|
||||
self.module_path = pathlib.Path(str(module_path))
|
||||
|
||||
def collect_definitions(self) -> Iterator[PythonDefinition]:
|
||||
'''
|
||||
Collects all function and class definitions of the module.
|
||||
|
||||
:return: All definitions of the module
|
||||
:rtype: Iterator[PythonDefinition]
|
||||
'''
|
||||
|
||||
tree = ast.parse(self.module_path.read_text())
|
||||
|
||||
return _collect_definitions(tree)
|
||||
|
||||
class PythonDocumentationGenerator:
|
||||
'''
|
||||
@@ -59,12 +97,12 @@ class PythonDocumentationGenerator:
|
||||
|
||||
return namespace
|
||||
|
||||
def namespace(self) -> List[PythonPackageNamespace]:
|
||||
def namespace(self) -> list[PythonPackageNamespace]:
|
||||
'''
|
||||
Returns the project as a namespace.
|
||||
|
||||
:return: The project as a namespace.
|
||||
:rtype: PythonPackageNamespace
|
||||
:rtype: list[PythonPackageNamespace]
|
||||
'''
|
||||
|
||||
src_dir = self.project_directory / 'src'
|
||||
|
||||
Reference in New Issue
Block a user