Coverage for lode / viewer / contract.py: 100%

47 statements  

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

1"""The two steps of a page that are the same for every viewer (types in lode.viewer.view_data). 

2 

3`to_model` assembles the header of the artefact; `resolve_links` fills in the address of every 

4link of a page once the page is complete, because only then is known which cards it holds. 

5""" 

6from typing import List, Optional 

7 

8from lode.helpers.links import Links 

9from lode.viewer.view_data import Model, ViewData 

10 

11 

12def to_model(metadata: dict, export_formats: list, warnings: list) -> Model: 

13 """Build the contract Model from the header metadata, the export formats and the warnings. 

14 

15 `metadata` is the dictionary the viewer builds for the header: `uri` and `label` are the 

16 identity of the artefact, `_namespaces` its prefixes, and every other key a field to show. 

17 It is empty when the source declares no ontology or concept scheme. 

18 """ 

19 fields = dict(metadata) 

20 uri = fields.pop("uri", None) 

21 label = fields.pop("label", None) 

22 namespaces = fields.pop("_namespaces", []) 

23 

24 return { 

25 "iri": uri[0]["iri"] if uri else None, 

26 "label": label[0]["text"] if label else None, 

27 "href": None, 

28 "metadata": fields, 

29 "namespaces": [{"prefix": n["prefix"], "uri": n["uri"]} for n in namespaces], 

30 "serializations": [{"label": f["label"], "fmt": f["fmt"], "ext": f["ext"], 

31 "mime": f["mime"], "href": None} for f in export_formats], 

32 "warnings": [{"code": w["code"], "message": w["message"]} for w in warnings], 

33 } 

34 

35def resolve_links(view: ViewData, links: Links) -> None: 

36 """Fill `href` and `kind` everywhere in `view`, in place. 

37 

38 A link to a resource follows the first rule that applies: its card is on this very page, so it 

39 is an anchor; it belongs to this documentation, so it goes where `links` says; otherwise it is 

40 outside and goes to its own IRI. When `links` gives no address for a page of this documentation, 

41 the link has no `href` and no `kind`: it is plain text. 

42 """ 

43 anchors = {} 

44 for card in view["cards"]: 

45 anchors.setdefault(card["iri"], card["anchor"]) 

46 

47 def to_page(item, iri): 

48 href = links.resource(iri) 

49 item["href"], item["kind"] = href, ("page" if href else None) 

50 

51 def point(item, iri, internal): 

52 if not iri: 

53 item["href"], item["kind"] = None, None 

54 elif iri in anchors: 

55 item["href"], item["kind"] = f"#{anchors[iri]}", "anchor" 

56 elif internal: 

57 href = links.resource(iri) 

58 item["href"], item["kind"] = (href, "page") if href else (iri, "external") 

59 else: 

60 item["href"], item["kind"] = iri, "external" 

61 

62 def value(v): 

63 point(v, v["iri"], v["kind"] == "page") 

64 for part in v["parts"]: 

65 value(part) 

66 if v["nested"]: 

67 for pair in [v["nested"]["main"], *v["nested"]["siblings"]]: 

68 value(pair["value"]) 

69 

70 model = view["model"] 

71 model["href"] = links.home() 

72 for serialization in model["serializations"]: 

73 serialization["href"] = links.export(serialization["ext"], view["focus"]) 

74 for values in model["metadata"].values(): 

75 for v in values: 

76 value(v) 

77 for section in view["toc"]: 

78 for entry in section["entries"]: 

79 point(entry, entry["iri"], True) 

80 for card in view["cards"]: 

81 to_page(card, card["iri"]) # its own page, never an anchor to itself 

82 for values in (*card["relations"].values(), *card["statements"].values()): 

83 for v in values: 

84 value(v)