StrictDoc Documentation
strictdoc/features/project_statistics/screen.py
Source file coverage
Path:
strictdoc/features/project_statistics/screen.py
Lines:
72
Non-empty lines:
63
Non-empty lines covered with requirements:
52 / 63 (82.5%)
Functions:
1
Functions covered by requirements:
1 / 1 (100.0%)
1
import importlib
2
import os
3
import sys
4
 
5
from strictdoc.core.project_config import ProjectConfig
6
from strictdoc.core.traceability_index import TraceabilityIndex
7
from strictdoc.export.html.html_templates import HTMLTemplates
8
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
9
from strictdoc.features.project_statistics.generator import (
10
    ProgressStatisticsGenerator,
11
)
12
from strictdoc.helpers.exception import StrictDocException
13
 
14
 
15
def render_project_statistics_screen(
16
    project_config: ProjectConfig,
17
    traceability_index: TraceabilityIndex,
18
    html_templates: HTMLTemplates,
19
) -> None:
20
    """
21
    Export project statistics to a dedicated HTML page.
22
 
23
    @relation(SDOC-SRS-97, scope=function)
24
    @relation(SDOC-SRS-154, scope=function)
25
    """
26
 
27
    link_renderer = LinkRenderer(
28
        root_path="",
29
        static_path=project_config.dir_for_sdoc_assets,
30
    )
31
 
32
    statistics_generator = ProgressStatisticsGenerator
33
 
34
    if (
35
        custom_statistics_generator_path := project_config.statistics_generator
36
    ) is not None:
37
        # It is important to add the input folder to the import path.
38
        # Otherwise, the custom statistics generator may not be found.
39
        # In fact, a more reasonable path to add would be the project config
40
        # path, but since it is not maintained by ProjectConfig yet and
41
        # usually equals the input path, add the input path for
42
        # now.
43
        input_paths = project_config.input_paths
44
        assert input_paths is not None and len(input_paths) > 0, (
45
            "Expected a valid input path."
46
        )
47
        sys.path.insert(0, input_paths[0])
48
 
49
        module_path, class_name = custom_statistics_generator_path.rsplit(
50
            ".", 1
51
        )
52
        try:
53
            module = importlib.import_module(module_path)
54
            statistics_generator = getattr(module, class_name)
55
        except ModuleNotFoundError as module_not_found_error_:
56
            raise StrictDocException(
57
                "Could not import a user-provided statistics generator: "
58
                f"{module_not_found_error_}."
59
            ) from module_not_found_error_
60
 
61
    document_content = statistics_generator.export(
62
        project_config,
63
        traceability_index,
64
        link_renderer,
65
        html_templates=html_templates,
66
    )
67
    output_html_source_coverage = os.path.join(
68
        project_config.export_output_html_root,
69
        "project_statistics.html",
70
    )
71
    with open(output_html_source_coverage, "w", encoding="utf8") as file:
72
        file.write(document_content)