Coverage for lode / core.py: 100%

12 statements  

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

1"""Core entry points of the library for callers that name the format with a string. 

2 

3The CLI and the web service go through these functions: none of them touches the Reader, 

4the viewers or the builder directly. Library users read with the class of the format 

5(OwlOntology, SkosVocabulary) and reach the viewer through `artefact.viewer`. The code lives 

6here and not in `lode/__init__.py` so that the package root only lists the public names. 

7""" 

8from pathlib import Path 

9from typing import TYPE_CHECKING, Optional, Union 

10 

11from lode import profiles 

12 

13if TYPE_CHECKING: 

14 from lode.artefact import Artefact 

15 

16 

17# ========================================================================== 

18# 1. READ 

19# ========================================================================== 

20def read(source: str, read_as: str, *, imported: Optional[bool] = None, 

21 closure: Optional[bool] = None, partial_import: Optional[bool] = None, 

22 warnings: bool = False, source_name: Optional[str] = None) -> "Artefact": 

23 """Read a semantic artefact whose format is given by name, and return it. 

24 

25 For callers that hold the format as a string, such as the CLI and the web service: 

26 this is the only place where `read_as` becomes a class. It names an enabled profile, 

27 matched ignoring case, and the result is that profile's Artefact subclass 

28 (OwlOntology, SkosVocabulary...); an unknown or disabled format raises 

29 ArtefactValidationError listing the valid ones. `source` is a local path or a public 

30 http(s) URL. `imported`, `closure` and `partial_import` load the `owl:imports` of 

31 the artefact (depth 1, full closure, only the entities in use). `warnings` 

32 collects the reader warnings in `artefact.warnings`; the current checks are 

33 OWL-specific. `source_name` names the artefact when `source` is only a local 

34 copy of it (for example a cached download). Blocking: it may fetch remote 

35 documents. 

36 """ 

37 # The profile registry is cheap to import; the class it returns brings in rdflib 

38 # and the reader only now, so `import lode` and `lode --help` stay fast. 

39 profile = profiles.get_enabled(read_as) 

40 return profile.artefact_class()(source, imported=imported, closure=closure, 

41 partial_import=partial_import, warnings=warnings, 

42 source_name=source_name) 

43 

44 

45# ========================================================================== 

46# 2. EXPORT 

47# ========================================================================== 

48def export(artefact: "Artefact", fmt: str, resource: Optional[str] = None) -> Union[str, bytes]: 

49 """Serialize the triples of the artefact, or of one resource, in an rdflib format. 

50 

51 `fmt` is an rdflib format name ("turtle", "xml", "json-ld", "nt"); the valid 

52 ones are listed in `lode.helpers.formats.SERIALIZATION_FORMATS`. Only the 

53 triples of the loaded artefact are returned, never those of its imports. 

54 """ 

55 viewer = artefact.viewer 

56 return viewer.export_resource(resource, fmt) if resource else viewer.export_main(fmt) 

57 

58 

59# ========================================================================== 

60# 3. BUILD 

61# ========================================================================== 

62def build_site(artefact: "Artefact", out_dir: Union[str, Path], *, lang: str = "en", 

63 artefact_name: str = "index") -> None: 

64 """Write the static documentation site of an artefact into `out_dir`. 

65 

66 The viewer is the one of `artefact.profile`. Blocking and disk-heavy: existing 

67 files in `out_dir` with the same names are overwritten. `artefact_name` is the 

68 file stem of the root page and its serializations. 

69 """ 

70 # Local import for the same reason as in read(): the builder pulls in jinja2. 

71 from lode.builder import build_html 

72 

73 build_html(artefact.viewer, Path(out_dir), lang=lang, artefact_name=artefact_name)