StrictDoc Documentation
strictdoc/features/diff_and_changelog/diff_screen_results_view_object.py
Source file coverage
Path:
strictdoc/features/diff_and_changelog/diff_screen_results_view_object.py
Lines:
127
Non-empty lines:
109
Non-empty lines covered with requirements:
109 / 109 (100.0%)
Functions:
10
Functions covered by requirements:
10 / 10 (100.0%)
1
"""
2
@relation(SDOC-SRS-111, scope=file)
3
"""
4
 
5
import urllib
6
from dataclasses import dataclass
7
from typing import List, Optional, Set
8
 
9
from markupsafe import Markup
10
 
11
from strictdoc import __version__
12
from strictdoc.backend.sdoc.models.document import SDocDocument
13
from strictdoc.backend.sdoc.models.document_grammar import DocumentGrammar
14
from strictdoc.core.document_tree import DocumentTree
15
from strictdoc.core.document_tree_iterator import DocumentTreeIterator
16
from strictdoc.core.project_config import ProjectConfig
17
from strictdoc.core.traceability_index import TraceabilityIndex
18
from strictdoc.export.html.html_templates import JinjaEnvironment
19
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
20
from strictdoc.features.diff_and_changelog.change_container import (
21
    ChangeContainer,
22
)
23
from strictdoc.features.diff_and_changelog.project_diff_analyzer import (
24
    ChangeStats,
25
    ProjectTreeDiffStats,
26
)
27
from strictdoc.helpers.cast import assert_cast
28
 
29
 
30
@dataclass
31
class DiffScreenResultsViewObject:
32
    def __init__(
33
        self,
34
        *,
35
        project_config: ProjectConfig,
36
        change_container: ChangeContainer,
37
        document_tree_lhs: DocumentTree,
38
        document_tree_rhs: DocumentTree,
39
        documents_iterator_lhs: DocumentTreeIterator,
40
        documents_iterator_rhs: DocumentTreeIterator,
41
        left_revision: Optional[str],
42
        right_revision: Optional[str],
43
        lhs_stats: ProjectTreeDiffStats,
44
        rhs_stats: ProjectTreeDiffStats,
45
        change_stats: ChangeStats,
46
        traceability_index_lhs: TraceabilityIndex,
47
        traceability_index_rhs: TraceabilityIndex,
48
        tab: str,
49
        left_revision_tags: Optional[List[str]] = None,
50
        right_revision_tags: Optional[List[str]] = None,
51
    ):
52
        self.project_config: ProjectConfig = project_config
53
        self.change_container: ChangeContainer = change_container
54
        self.document_tree_lhs: DocumentTree = document_tree_lhs
55
        self.document_tree_rhs: DocumentTree = document_tree_rhs
56
        self.documents_iterator_lhs = documents_iterator_lhs
57
        self.documents_iterator_rhs = documents_iterator_rhs
58
        self.left_revision: Optional[str] = left_revision
59
        self.right_revision: Optional[str] = right_revision
60
        self.left_revision_tags: List[str] = left_revision_tags or []
61
        self.right_revision_tags: List[str] = right_revision_tags or []
62
        self.lhs_stats = lhs_stats
63
        self.rhs_stats = rhs_stats
64
        self.change_stats = change_stats
65
        self.traceability_index_lhs: TraceabilityIndex = traceability_index_lhs
66
        self.traceability_index_rhs: TraceabilityIndex = traceability_index_rhs
67
        self.tab: str = tab
68
        self.results: bool = True
69
        link_renderer = LinkRenderer(
70
            root_path="", static_path=project_config.dir_for_sdoc_assets
71
        )
72
        self.link_renderer: LinkRenderer = link_renderer
73
        self.is_running_on_server: bool = project_config.is_running_on_server
74
        self.strictdoc_version = __version__
75
        self.error_message: Optional[str] = None
76
 
77
    @property
78
    def left_revision_urlencoded(self) -> str:
79
        return (
80
            urllib.parse.quote(self.left_revision)
81
            if self.left_revision is not None
82
            else ""
83
        )
84
 
85
    @property
86
    def right_revision_urlencoded(self) -> str:
87
        return (
88
            urllib.parse.quote(self.right_revision)
89
            if self.right_revision is not None
90
            else ""
91
        )
92
 
93
    def get_document_level(self) -> int:
94
        return 0
95
 
96
    def render_screen(self, jinja_environment: JinjaEnvironment) -> Markup:
97
        return jinja_environment.render_template_as_markup(
98
            "features/diff_and_changelog/index.jinja", view_object=self
99
        )
100
 
101
    def render_url(self, url: str) -> Markup:
102
        return Markup(self.link_renderer.render_url(url))
103
 
104
    def render_static_url(self, url: str) -> Markup:
105
        return Markup(self.link_renderer.render_static_url(url))
106
 
107
    def render_static_url_with_prefix(self, url: str) -> str:
108
        return self.link_renderer.render_static_url_with_prefix(url)
109
 
110
    def get_node_types(self) -> List[str]:
111
        node_types: Set[str] = set()
112
 
113
        document_: SDocDocument
114
        for document_ in self.document_tree_lhs.document_list:
115
            document_grammar = assert_cast(document_.grammar, DocumentGrammar)
116
 
117
            for node_type_ in document_grammar.elements_by_type.keys():
118
                node_types.add(node_type_)
119
 
120
        for document_ in self.document_tree_rhs.document_list:
121
            document_grammar = assert_cast(document_.grammar, DocumentGrammar)
122
 
123
            for node_type_ in document_grammar.elements_by_type.keys():
124
                node_types.add(node_type_)
125
 
126
        priority_order = {"SECTION": 0, "TEXT": 1, "REQUIREMENT": 2}
127
        return sorted(node_types, key=lambda x: (priority_order.get(x, 100), x))