Path:
strictdoc/backend/sdoc/models/anchor.py
Lines:
66
Non-empty lines:
53
Non-empty lines covered with requirements:
53 / 53 (100.0%)
Functions:
7
Functions covered by requirements:
7 / 7 (100.0%)
1
"""2
@relation(SDOC-SRS-150, scope=file)3
"""4
5
# mypy: disable-error-code="unreachable"6
7
from typing import Optional
8
9
from strictdoc.backend.sdoc.models.model import (
10
SDocDocumentIF,
11
SDocNodeFieldIF,
12
SDocNodeIF,
13
)14
from strictdoc.helpers.auto_described import auto_described
15
from strictdoc.helpers.cast import assert_cast
16
from strictdoc.helpers.mid import MID
17
18
19
@auto_described20
class Anchor:
21
def __init__(
22
self, parent: SDocNodeFieldIF, value: str, title: Optional[str]
23
) -> None:
24
# In the grammar, the title is optional but textX passes it as an empty25
# string. Putting an assert to monitor the regressions/changes if the26
# grammar gets changed.27
assert title is not None
28
# FIXME: Cannot enable this assert because the parent can also be29
# FreeTextContainer. Refactor the types.30
# assert isinstance(parent, SDocNodeFieldIF), parent # noqa: ERA00131
32
self.parent: SDocNodeFieldIF = parent
33
self.value: str = value
34
35
has_title = len(title) > 0
36
self.title: str = title if has_title else value
37
self.has_title = has_title
38
39
# FIXME: Remove either mid or reserved_mid.40
self.mid: MID = MID.create()
41
self.reserved_mid: MID = self.mid
42
43
def get_display_title(
44
self,
45
include_toc_number: bool = True, # noqa: ARG002
46
) -> str:
47
return self.title
48
49
def get_document(self) -> SDocDocumentIF:
50
if isinstance(self.parent.parent, SDocDocumentIF):
51
return self.parent.parent
52
document: SDocDocumentIF = assert_cast(
53
self.parent_node().get_document(), SDocDocumentIF
54
)55
return document
56
57
def get_parent_or_including_document(self) -> SDocDocumentIF:
58
return self.parent_node().get_parent_or_including_document()
59
60
def get_including_document(self) -> Optional[SDocDocumentIF]:
61
return self.parent_node().get_including_document()
62
63
def parent_node(self) -> SDocNodeIF:
64
# Anchor -> SDocField -> SDocNode.65
parent_node: SDocNodeIF = assert_cast(self.parent.parent, SDocNodeIF)
66
return parent_node