Coverage for fpdf2_textindex/interface/cross_reference.py: 96.00%

25 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-01 14:22 +0000

1"""Cross Reference.""" 

2 

3from __future__ import annotations 

4 

5import dataclasses 

6 

7from fpdf2_textindex import constants as const 

8from fpdf2_textindex.interface.abc import _LabelPathABC 

9from fpdf2_textindex.interface.enums import CrossReferenceType 

10from fpdf2_textindex.interface.label_path import LabelPath 

11from fpdf2_textindex.interface.link_location import LinkLocation 

12 

13 

14@dataclasses.dataclass(kw_only=True, slots=True) 

15class CrossReference(_LabelPathABC): 

16 """Cross Reference.""" 

17 

18 id: int 

19 """The id of the cross reference.""" 

20 

21 type: CrossReferenceType 

22 """The type of the cross reference.""" 

23 

24 label_path: LabelPath 

25 """The label path the cross reference points to.""" 

26 

27 location: LinkLocation | None = dataclasses.field(default=None, init=False) 

28 """The (link) location in the document the cross reference is set at.""" 

29 

30 def __post_init__(self) -> None: 

31 self.label_path = LabelPath(self.label_path) 

32 self.type = CrossReferenceType(self.type) 

33 

34 def __str__(self) -> str: 

35 return f"{self.type.capitalize():s} {self.joined_label_path:s}" 

36 

37 @property 

38 def link(self) -> str: 

39 """The link in the document that must be set in the text index to lead 

40 from the text to the text index. 

41 """ 

42 return f"{const.INDEX_ID_PREFIX:s}{self.id:d}"