Coverage for lode / profiles / skos / viewer.py: 98%

45 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-22 15:54 +0000

1"""Viewer for SKOS artefacts.""" 

2 

3from typing import List, Optional 

4 

5# internal imports 

6from lode.viewer.view_data import TocEntry, TocSection, ViewData 

7from lode.helpers.links import Links 

8from lode.viewer.base_viewer import BaseViewer 

9 

10 

11class TreeNode(TocEntry): 

12 """One node of the concept taxonomy: a concept and the concepts narrower than it.""" 

13 

14 children: List["TreeNode"] 

15 

16 

17class SkosTocSection(TocSection): 

18 """A section of the SKOS navigation: the base one plus the taxonomy of its concepts. 

19 

20 The section of the collections has an empty `tree`. 

21 """ 

22 

23 tree: List[TreeNode] 

24 

25 

26 

27class SkosViewer(BaseViewer): 

28 """SKOS viewer. 

29 

30 Display names, table of contents and card come from the viewer.yaml of this profile. 

31 Entities are formatted by BaseViewer, so SKOS produces the same data structure 

32 as OWL and the card (a separate template) reads the same keys. 

33 """ 

34 

35 def view(self, resource_uri: Optional[str] = None, language: Optional[str] = None, 

36 links: Optional[Links] = None) -> ViewData: 

37 """SKOS viewer. 

38 

39 Display names and table of contents come from viewer.yaml. Entities are 

40 formatted by BaseViewer, so SKOS produces the same data structure as OWL. It adds the 

41 taxonomy of the concepts, which its own sidebar (templates/_toc.html of this profile) draws. 

42 """ 

43 

44 data = super().view(resource_uri, language, links) 

45 for section in data['toc']: 

46 addresses = {e['iri']: (e['href'], e['kind']) for e in section['entries']} 

47 self._copy_addresses(section['tree'], addresses) 

48 return data 

49 

50 def _copy_addresses(self, nodes: List[TreeNode], addresses: dict) -> None: 

51 """Give each node, and the nodes below it, the address of the entry that has its IRI.""" 

52 for node in nodes: 

53 node['href'], node['kind'] = addresses[node['iri']] 

54 self._copy_addresses(node['children'], addresses) 

55 

56 def _build_toc(self, language: str) -> List[TocSection]: 

57 """Build the navigation, with the taxonomy of the concepts added to the sections. 

58 

59 Every section gets a `tree` (empty for the ones that have no hierarchy, such as the 

60 collections), so the SKOS sidebar can read it without checking that it exists. 

61 """ 

62 toc = super()._build_toc(language) 

63 concepts = [i for i in self.get_toc_entities() if type(i).__name__ == 'Concept'] 

64 for section in toc: 

65 section['tree'] = self._build_taxonomy(concepts, language) if section['type'] == 'Concept' else [] 

66 return toc 

67 

68 def _build_taxonomy(self, concepts: List, language: Optional[str]) -> List[TreeNode]: 

69 """Build the forest of the concepts, each one under its broader concepts. 

70 

71 A concept with several broader concepts appears under each of them, which is how a 

72 SKOS taxonomy works; one whose broader concepts are not documented here is a root. 

73 A cycle is cut where it closes, so a malformed vocabulary cannot loop forever. 

74 """ 

75 by_iri = {str(c.has_identifier): c for c in concepts if getattr(c, 'has_identifier', None)} 

76 narrower = {iri: [] for iri in by_iri} 

77 has_parent = set() 

78 for iri, concept in by_iri.items(): 

79 for broader in concept.get_is_sub_concept_of() or []: 

80 parent = str(getattr(broader, 'has_identifier', '') or '') 

81 if parent in by_iri and parent != iri: 

82 narrower[parent].append(iri) 

83 has_parent.add(iri) 

84 

85 def label_key(iri: str) -> str: 

86 """Sort key of a concept: its label, or its IRI when it has none.""" 

87 return (self._get_best_label(by_iri[iri], language) or iri).lower() 

88 

89 def node(iri: str, path: frozenset) -> Optional[TreeNode]: 

90 """Build the node of a concept with its narrower ones; None when it closes a cycle.""" 

91 if iri in path: 

92 return None 

93 entry = self._toc_entry(by_iri[iri], language) 

94 kids = (node(child, path | {iri}) for child in sorted(narrower[iri], key=label_key)) 

95 return {**entry, 'label': entry['label'] or iri, 'children': [k for k in kids if k]} 

96 

97 roots = (node(iri, frozenset()) for iri in sorted((i for i in by_iri if i not in has_parent), 

98 key=label_key)) 

99 return [r for r in roots if r]