Coverage for lode / viewer / view_data.py: 100%
81 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-11-22 15:54 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-11-22 15:54 +0000
1"""The contract between a viewer and a template: the data a viewer hands over, and nothing else.
3A viewer returns a plain dictionary (`ViewData`); a template only reads it. The classes below
4describe that dictionary and are not used at runtime. They are the reference for whoever
5writes a template, and tests check that the viewers keep to them.
7Rules that hold everywhere:
8 - every key listed is always present: an empty list, an empty dictionary or None says "nothing",
9 never a missing key;
10 - labels are already in the requested language, with the fallback applied;
11 - lists keep a meaningful order (cards by label, relations in display order);
12 - nothing here depends on how the data will be drawn: there is no HTML, no CSS class.
13"""
14from typing import Dict, List, Optional, TypedDict
17class Namespace(TypedDict):
18 """A prefix declared by the artefact."""
20 prefix: str
21 uri: str
24class Serialization(TypedDict):
25 """A format the whole artefact, or one resource, can be exported to."""
27 label: str # "Turtle"
28 fmt: str # the rdflib format name, "turtle"
29 ext: str # file extension, "ttl"
30 mime: str # media type, "text/turtle"
31 href: Optional[str] # where to download it; None when it cannot be offered
33class ReaderWarning(TypedDict):
34 """A problem the reader found while reading the source."""
36 code: str
37 message: str # already worded with the display names of the viewer
40class NestedPair(TypedDict):
41 """One predicate and its value, inside an annotation on an annotation."""
43 predicate: str
44 value: "Value"
47class Nested(TypedDict):
48 """The annotations carried by a value that is itself a statement."""
50 main: NestedPair
51 siblings: List[NestedPair]
54class Value(TypedDict):
55 """One value shown on a card or in the header: text, possibly a link, possibly structured."""
57 text: Optional[str] # what is displayed
58 iri: Optional[str] # the resource the value stands for, if it is one
59 href: Optional[str] # where a click goes; None when the value is not a link
60 kind: Optional[str] # "anchor" (same page), "page" (a page of this documentation),
61 # "external" (outside it); None when there is no link
62 language: Optional[str] # language tag of a literal
63 type: Optional[str] # model class name: "Literal", "Concept", "PropertyChain", ...
64 deprecated: bool
65 imported: bool # defined in an imported module, not in this artefact
66 visualize: Optional[str] # header only: the address that opens LODE on this ontology; None otherwise
67 parts: List["Value"] # components of a class expression or of a property chain; [] otherwise
68 nested: Optional[Nested]
71class Provenance(TypedDict):
72 """The triples an entity was built from, serialized in one format."""
74 label: str # the format, "TriG"
75 code: str # the serialization
78class Card(TypedDict):
79 """One entity with all its data: what a card shows."""
81 iri: str
82 type: str # model class name: "Concept", "Relation", ...
83 label: Optional[str]
84 anchor: str # a stable HTML id for this entity
85 href: Optional[str] # the page of this entity; None when it has none
86 kind: Optional[str] # as in Value
87 deprecated: bool
88 relations: Dict[str, List[Value]] # display name -> values
89 statements: Dict[str, List[Value]] # predicate label -> values
90 characteristics: List[str] # for example ["is symmetric"]
91 provenance: List[Provenance] # [] when unknown
94class TocEntry(TypedDict):
95 """One entry of the navigation: an entity reduced to what a list needs, no relations, no statements."""
97 iri: str
98 type: str
99 label: Optional[str]
100 anchor: str
101 href: Optional[str]
102 kind: Optional[str]
104class TocSection(TypedDict):
105 """The entries of one class, as a section of the table of contents."""
107 id: str # a stable HTML id for the section
108 type: str # model class name
109 label: str # display name of the group, "Classes"
110 entries: List[TocEntry]
112class Model(TypedDict):
113 """The documented artefact: its header, and what concerns it as a whole."""
115 iri: Optional[str] # None when the source declares no ontology or concept scheme
116 label: Optional[str]
117 metadata: Dict[str, List[Value]] # display name -> values: title, description, version, ...
118 namespaces: List[Namespace]
119 serializations: List[Serialization]
120 warnings: List[ReaderWarning] # [] unless the artefact was read asking for them
121 href: Optional[str] # the page of the whole artefact; where "back to everything" goes
123class TypeLabel(TypedDict):
124 """How a type is called and abbreviated."""
126 singular: str # "Class"
127 plural: str # "Classes"
128 abbreviation: str # "c": the short badge shown next to a name
130class ViewData(TypedDict):
131 """What a viewer hands to a template. Every key is always present."""
133 model: Model
134 toc: List[TocSection] # the navigation of the whole artefact, for the sidebar and the contents list; [] on the page of one resource
135 types: Dict[str, TypeLabel] # how each type is called, by the class name that `type` carries
136 cards: List[Card] # what the main part shows: all the cards, one, or none
137 focus: Optional[str] # the IRI that was asked for; None for the whole artefact
138 error: Optional[str] # a message when the request cannot be answered; None otherwise
140def new_value(**fields) -> Value:
141 """Return a Value with every key present, overridden by `fields`.
143 The viewers start from this and fill in what they know, so that no Value ever lacks a key
144 a template reads (a template never has to ask whether a key exists).
145 """
146 value: Value = {"text": None, "iri": None, "href": None, "kind": None, "language": None,
147 "type": None, "deprecated": False, "imported": False, "visualize": None,
148 "parts": [], "nested": None}
149 value.update(fields)
150 return value