oc-lode 2.1.0__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {oc_lode-2.1.0 → oc_lode-2.2.0}/.gitignore +5 -1
- {oc_lode-2.1.0 → oc_lode-2.2.0}/PKG-INFO +4 -5
- {oc_lode-2.1.0 → oc_lode-2.2.0}/README.md +2 -2
- oc_lode-2.2.0/lode/__init__.py +36 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/api.py +107 -162
- oc_lode-2.2.0/lode/artefact.py +191 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/builder.py +85 -113
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/cli.py +10 -18
- oc_lode-2.2.0/lode/core.py +73 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/exceptions.py +11 -0
- oc_lode-2.2.0/lode/helpers/formats.py +22 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/helpers/html_utils.py +7 -3
- oc_lode-2.2.0/lode/helpers/links.py +31 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/helpers/rdf.py +2 -1
- oc_lode-2.2.0/lode/helpers/site_zip.py +121 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/collection.py +5 -2
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/concept.py +11 -2
- oc_lode-2.2.0/lode/models/datatypeRestriction.py +40 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/literal.py +11 -2
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/model.py +4 -4
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/resource.py +9 -0
- oc_lode-2.2.0/lode/profiles/__init__.py +220 -0
- oc_lode-2.2.0/lode/profiles/owl/__init__.py +11 -0
- oc_lode-2.2.0/lode/profiles/owl/artefact.py +10 -0
- oc_lode-2.2.0/lode/profiles/owl/checks.py +171 -0
- oc_lode-2.1.0/lode/reader/logic/owl_logic.py → oc_lode-2.2.0/lode/profiles/owl/reader.py +375 -309
- oc_lode-2.1.0/lode/reader/config/owl.yaml → oc_lode-2.2.0/lode/profiles/owl/reader.yaml +82 -30
- oc_lode-2.1.0/lode/viewer/owl_viewer.py → oc_lode-2.2.0/lode/profiles/owl/viewer.py +44 -53
- oc_lode-2.1.0/lode/viewer/config/owl_viewer.yaml → oc_lode-2.2.0/lode/profiles/owl/viewer.yaml +4 -7
- oc_lode-2.2.0/lode/profiles/skos/__init__.py +11 -0
- oc_lode-2.2.0/lode/profiles/skos/artefact.py +10 -0
- oc_lode-2.2.0/lode/profiles/skos/reader.py +41 -0
- oc_lode-2.2.0/lode/profiles/skos/reader.yaml +189 -0
- oc_lode-2.2.0/lode/profiles/skos/templates/_toc.html +165 -0
- oc_lode-2.2.0/lode/profiles/skos/viewer.py +99 -0
- oc_lode-2.1.0/lode/viewer/config/skos_viewer.yaml → oc_lode-2.2.0/lode/profiles/skos/viewer.yaml +10 -8
- {oc_lode-2.1.0/lode/reader → oc_lode-2.2.0/lode}/provenance_graph.py +5 -5
- oc_lode-2.2.0/lode/reader/__init__.py +7 -0
- oc_lode-2.2.0/lode/reader/config/base.yaml +94 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/reader/config_manager.py +37 -123
- oc_lode-2.2.0/lode/reader/loader.py +229 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/reader/modules.py +48 -26
- oc_lode-2.2.0/lode/reader/pipeline.py +157 -0
- oc_lode-2.1.0/lode/reader/logic/base_logic.py → oc_lode-2.2.0/lode/reader/reader.py +339 -361
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/static/style.css +18 -1
- oc_lode-2.2.0/lode/templates/_card.html +147 -0
- oc_lode-2.2.0/lode/templates/_metadata.html +32 -0
- oc_lode-2.2.0/lode/templates/_scripts.html +187 -0
- oc_lode-2.1.0/lode/templates/_toc_sidebar.html → oc_lode-2.2.0/lode/templates/_toc.html +49 -17
- oc_lode-2.2.0/lode/templates/_value.html +39 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/templates/index.html +3 -4
- oc_lode-2.2.0/lode/templates/page.html +152 -0
- oc_lode-2.2.0/lode/viewer/__init__.py +13 -0
- oc_lode-2.2.0/lode/viewer/base_viewer.py +745 -0
- oc_lode-2.2.0/lode/viewer/contract.py +84 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/viewer/helpers.py +15 -80
- oc_lode-2.2.0/lode/viewer/view_data.py +150 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/pyproject.toml +2 -2
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/provenance/main.ttl +4 -3
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/integration}/test_api.py +28 -16
- oc_lode-2.2.0/tests/integration/test_facade.py +66 -0
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/integration}/test_modules.py +94 -64
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/integration}/test_reader.py +103 -123
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/integration}/test_static_builder.py +22 -21
- oc_lode-2.2.0/tests/integration/test_templates.py +199 -0
- oc_lode-2.2.0/tests/taxonomies_skos.json +17 -0
- oc_lode-2.2.0/tests/unit/core/test_architecture.py +96 -0
- oc_lode-2.2.0/tests/unit/core/test_artefact.py +65 -0
- oc_lode-2.2.0/tests/unit/core/test_config_manager.py +93 -0
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/unit/core}/test_packaging.py +13 -12
- oc_lode-2.2.0/tests/unit/core/test_profile.py +130 -0
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/unit/core}/test_security.py +38 -44
- oc_lode-2.2.0/tests/unit/core/test_view_data.py +391 -0
- oc_lode-2.2.0/tests/unit/owl/test_datatype_restrictions.py +112 -0
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/unit/owl}/test_owl_integrity.py +351 -352
- oc_lode-2.1.0/tests/test_owl_logic.py → oc_lode-2.2.0/tests/unit/owl/test_owl_reader.py +453 -438
- {oc_lode-2.1.0/tests → oc_lode-2.2.0/tests/unit/owl}/test_owl_viewer.py +93 -108
- oc_lode-2.2.0/tests/unit/skos/test_skos_integrity.py +277 -0
- oc_lode-2.2.0/tests/unit/skos/test_skos_reader.py +593 -0
- oc_lode-2.1.0/lode/__init__.py +0 -2
- oc_lode-2.1.0/lode/helpers/formats.py +0 -32
- oc_lode-2.1.0/lode/models/datatypeRestriction.py +0 -28
- oc_lode-2.1.0/lode/reader/README.md +0 -215
- oc_lode-2.1.0/lode/reader/__init__.py +0 -16
- oc_lode-2.1.0/lode/reader/config/base.yaml +0 -201
- oc_lode-2.1.0/lode/reader/config/rdf.yaml +0 -72
- oc_lode-2.1.0/lode/reader/config/rdfs.yaml +0 -69
- oc_lode-2.1.0/lode/reader/config/skos.yaml +0 -22
- oc_lode-2.1.0/lode/reader/loader.py +0 -278
- oc_lode-2.1.0/lode/reader/logic/__init__.py +0 -15
- oc_lode-2.1.0/lode/reader/logic/rdf_logic.py +0 -253
- oc_lode-2.1.0/lode/reader/logic/skos_logic.py +0 -91
- oc_lode-2.1.0/lode/reader/reader.py +0 -292
- oc_lode-2.1.0/lode/reader/warnings/__init__.py +0 -15
- oc_lode-2.1.0/lode/reader/warnings/owl_warnings.py +0 -171
- oc_lode-2.1.0/lode/templates/_card_owl.html +0 -307
- oc_lode-2.1.0/lode/templates/_card_skos.html +0 -50
- oc_lode-2.1.0/lode/templates/_value.html +0 -55
- oc_lode-2.1.0/lode/templates/viewer.html +0 -500
- oc_lode-2.1.0/lode/viewer/__init__.py +0 -28
- oc_lode-2.1.0/lode/viewer/base_viewer.py +0 -775
- oc_lode-2.1.0/lode/viewer/config/rdf_viewer.yaml +0 -39
- oc_lode-2.1.0/lode/viewer/rdf_viewer.py +0 -8
- oc_lode-2.1.0/lode/viewer/skos_viewer.py +0 -172
- oc_lode-2.1.0/tests/tests.md +0 -40
- {oc_lode-2.1.0 → oc_lode-2.2.0}/LICENSE.md +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/helpers/__init__.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/helpers/spool.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/__init__.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/annotation.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/atom.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/attribute.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/cardinality.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/container.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/datatype.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/individual.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/oneOf.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/property.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/propertyConceptRestriction.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/propertySelfRestriction.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/quantifier.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/relation.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/restriction.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/rule.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/statement.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/truthFunction.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/value.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/models/variable.py +0 -0
- {oc_lode-2.1.0/lode/reader → oc_lode-2.2.0/lode}/security.py +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/templates/error.html +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/lode/viewer/config/model_annotations_deterministic_sorting.yaml +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/cito.ttl +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/provenance/anon.ttl +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/provenance/deep.ttl +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/provenance/home.ttl +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/provenance/imp.ttl +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/data/provenance/ver.ttl +0 -0
- {oc_lode-2.1.0 → oc_lode-2.2.0}/tests/ontologies_spar.json +0 -0
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: oc-lode
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: New reengineered version of LODE, maintained by OpenCitations
|
|
5
5
|
Project-URL: Homepage, https://github.com/opencitations/lode
|
|
6
6
|
Project-URL: Repository, https://github.com/opencitations/lode
|
|
7
7
|
Project-URL: Documentation, https://opencitations.github.io/lode/
|
|
8
8
|
Project-URL: Issues, https://github.com/opencitations/lode/issues
|
|
9
|
-
Author: Ahmadreza Nazari
|
|
10
|
-
Author-email: Valentina Pasqual <valentina.pasqual2@unibo.it>, Silvio Peroni <silvio.peroni@unibo.it>
|
|
9
|
+
Author-email: Valentina Pasqual <valentina.pasqual2@unibo.it>, Silvio Peroni <silvio.peroni@unibo.it>, Ahmadreza Nazari <ahmadreza.nazari@studio.unibo.it>
|
|
11
10
|
License-Expression: ISC
|
|
12
11
|
License-File: LICENSE.md
|
|
13
12
|
Keywords: documentation,ontology,opencitations,owl,rdf,semantic-web,skos
|
|
@@ -58,8 +57,8 @@ pip install "oc-lode[server]"
|
|
|
58
57
|
|
|
59
58
|
```bash
|
|
60
59
|
# Build static documentation from an ontology URL or a local file
|
|
61
|
-
lode build --url <url> --read-as <owl|
|
|
62
|
-
lode build --file <path> --read-as <owl|
|
|
60
|
+
lode build --url <url> --read-as <owl|skos> [--out ./docs] [--lang en] [--artefact-name index] [--imported] [--closure]
|
|
61
|
+
lode build --file <path> --read-as <owl|skos> [--out ./docs]
|
|
63
62
|
|
|
64
63
|
# Run the web service locally (requires the [server] extra)
|
|
65
64
|
lode serve --port 8000
|
|
@@ -22,8 +22,8 @@ pip install "oc-lode[server]"
|
|
|
22
22
|
|
|
23
23
|
```bash
|
|
24
24
|
# Build static documentation from an ontology URL or a local file
|
|
25
|
-
lode build --url <url> --read-as <owl|
|
|
26
|
-
lode build --file <path> --read-as <owl|
|
|
25
|
+
lode build --url <url> --read-as <owl|skos> [--out ./docs] [--lang en] [--artefact-name index] [--imported] [--closure]
|
|
26
|
+
lode build --file <path> --read-as <owl|skos> [--out ./docs]
|
|
27
27
|
|
|
28
28
|
# Run the web service locally (requires the [server] extra)
|
|
29
29
|
lode serve --port 8000
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""LODE: Live OWL Documentation Environment, maintained by OpenCitations.
|
|
2
|
+
|
|
3
|
+
Typical use as a library:
|
|
4
|
+
|
|
5
|
+
import lode
|
|
6
|
+
from lode import SkosVocabulary
|
|
7
|
+
from lode.models import Concept
|
|
8
|
+
|
|
9
|
+
vocabulary = SkosVocabulary("vocabulary.ttl")
|
|
10
|
+
for concept in vocabulary.get_entities(Concept):
|
|
11
|
+
print(concept.get_has_identifier())
|
|
12
|
+
|
|
13
|
+
lode.build_site(vocabulary, "site")
|
|
14
|
+
|
|
15
|
+
The classes of the formats (OwlOntology, SkosVocabulary and those of installed plugins) are
|
|
16
|
+
found on first use, so importing `lode` does not import rdflib or the readers.
|
|
17
|
+
"""
|
|
18
|
+
from lode.core import build_site, read
|
|
19
|
+
|
|
20
|
+
__all__ = ["read", "build_site", "OwlOntology", "SkosVocabulary"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def __getattr__(name: str):
|
|
24
|
+
"""Return the Artefact class called `name`, found in the profile registry (PEP 562).
|
|
25
|
+
|
|
26
|
+
Lets `from lode import OwlOntology` work without the package root importing any profile.
|
|
27
|
+
Private and dunder names are refused at once: tools probe them (`__path__`, `__wrapped__`)
|
|
28
|
+
and must not trigger the import of every format.
|
|
29
|
+
"""
|
|
30
|
+
if not name.startswith("_"):
|
|
31
|
+
from lode.profiles import artefact_class_named
|
|
32
|
+
|
|
33
|
+
cls = artefact_class_named(name)
|
|
34
|
+
if cls is not None:
|
|
35
|
+
return cls
|
|
36
|
+
raise AttributeError(f"module 'lode' has no attribute {name!r}")
|
|
@@ -14,16 +14,12 @@ Endpoints:
|
|
|
14
14
|
# 1. IMPORTS
|
|
15
15
|
# ==========================================================================
|
|
16
16
|
from pathlib import Path
|
|
17
|
-
from typing import Optional
|
|
17
|
+
from typing import Optional, Dict, Tuple
|
|
18
18
|
from urllib.parse import urlencode
|
|
19
19
|
import hashlib
|
|
20
20
|
import logging
|
|
21
21
|
import os
|
|
22
|
-
import re
|
|
23
22
|
import shutil
|
|
24
|
-
import tempfile
|
|
25
|
-
import threading
|
|
26
|
-
import time
|
|
27
23
|
import traceback
|
|
28
24
|
from pathlib import Path
|
|
29
25
|
|
|
@@ -37,19 +33,17 @@ from starlette.middleware.gzip import GZipMiddleware
|
|
|
37
33
|
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
|
|
38
34
|
|
|
39
35
|
# Internal modules
|
|
40
|
-
from lode
|
|
41
|
-
|
|
42
|
-
check_format_enabled as _check_format_enabled,
|
|
43
|
-
)
|
|
36
|
+
from lode import profiles
|
|
37
|
+
from lode.helpers.formats import SERIALIZATION_FORMATS
|
|
44
38
|
from lode.helpers.html_utils import minify as _minify
|
|
45
39
|
import lode.helpers.spool as spool
|
|
46
|
-
|
|
47
|
-
from lode
|
|
48
|
-
from lode.exceptions import
|
|
49
|
-
from lode.
|
|
50
|
-
from lode.reader.provenance_graph import ProvenanceGraph
|
|
40
|
+
import lode.core as core
|
|
41
|
+
from lode import security
|
|
42
|
+
from lode.exceptions import ArtefactValidationError, BuildCapacityError, LODEError
|
|
43
|
+
from lode.helpers.site_zip import build_zip, slugify
|
|
51
44
|
from lode.helpers.rdf import iri_local_name
|
|
52
|
-
|
|
45
|
+
from urllib.parse import quote, urlencode
|
|
46
|
+
from lode.helpers.links import Links
|
|
53
47
|
|
|
54
48
|
# ==========================================================================
|
|
55
49
|
# 2. APP SETUP
|
|
@@ -90,52 +84,61 @@ app.add_middleware(GZipMiddleware, minimum_size=1000)
|
|
|
90
84
|
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
|
|
91
85
|
|
|
92
86
|
_PKG_DIR = Path(__file__).resolve().parent
|
|
93
|
-
templates = Jinja2Templates(directory=str(_PKG_DIR / "templates"))
|
|
94
87
|
app.mount("/static", StaticFiles(directory=str(_PKG_DIR / "static")), name="static")
|
|
95
88
|
|
|
96
|
-
# Beautify produced HTML.
|
|
97
|
-
templates.env.trim_blocks = True
|
|
98
|
-
templates.env.lstrip_blocks = True
|
|
99
89
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
# ==========================================================================
|
|
103
|
-
# Builds write hundreds of files each, so they get their own directory with an
|
|
104
|
-
# explicit budget instead of filling up /tmp.
|
|
105
|
-
BUILD_DIR = os.path.realpath(
|
|
106
|
-
os.getenv("LODE_BUILD_DIR", os.path.join(tempfile.gettempdir(), "lode-builds"))
|
|
107
|
-
)
|
|
108
|
-
os.makedirs(BUILD_DIR, exist_ok=True)
|
|
109
|
-
_BUILD_TTL = 30 * 60 # orphans (aborted downloads) age out
|
|
110
|
-
_BUILD_MAX_BYTES = 2 * 1024 ** 3 # 2 GB for all pending builds
|
|
111
|
-
_BUILD_SLOTS = threading.BoundedSemaphore(2) # concurrent builds, per worker process
|
|
112
|
-
_BUILD_WAIT = 5 # seconds a request waits for a slot
|
|
90
|
+
def _jinja_templates(template_dirs) -> Jinja2Templates:
|
|
91
|
+
"""Build a Jinja environment over a search path of template folders; the first match wins.
|
|
113
92
|
|
|
93
|
+
The produced HTML is beautified (trim_blocks, lstrip_blocks) before it is minified.
|
|
94
|
+
"""
|
|
95
|
+
built = Jinja2Templates(directory=[str(d) for d in template_dirs])
|
|
96
|
+
built.env.trim_blocks = True
|
|
97
|
+
built.env.lstrip_blocks = True
|
|
98
|
+
return built
|
|
114
99
|
|
|
115
|
-
|
|
116
|
-
|
|
100
|
+
# The base templates: they also hold index.html and error.html, which no format replaces.
|
|
101
|
+
templates = _jinja_templates([_PKG_DIR / "templates"])
|
|
117
102
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
103
|
+
# One environment per search path (that is, per format), built on first use: compiling the
|
|
104
|
+
# templates on every request would be slow. Two threads may build the same one; harmless.
|
|
105
|
+
_format_templates: Dict[Tuple[str, ...], Jinja2Templates] = {}
|
|
106
|
+
|
|
107
|
+
def _templates_for(template_dirs) -> Jinja2Templates:
|
|
108
|
+
"""Return the Jinja environment of a search path, built on first use and then reused."""
|
|
109
|
+
key = tuple(str(d) for d in template_dirs)
|
|
110
|
+
if key not in _format_templates:
|
|
111
|
+
_format_templates[key] = _jinja_templates(template_dirs)
|
|
112
|
+
return _format_templates[key]
|
|
113
|
+
|
|
114
|
+
class _WebLinks(Links):
|
|
115
|
+
"""The addresses of the web service: every page is a request to /extract that keeps the view parameters."""
|
|
116
|
+
|
|
117
|
+
def __init__(self, nav_qs: str, read_as: Optional[str] = None, lang: Optional[str] = None):
|
|
118
|
+
"""`nav_qs` is the query string of the current view (see _nav_qs); `read_as` and `lang` its format and language."""
|
|
119
|
+
self._qs = nav_qs
|
|
120
|
+
self._read_as = read_as
|
|
121
|
+
self._lang = lang
|
|
122
|
+
|
|
123
|
+
def visualize(self, iri: str) -> Optional[str]:
|
|
124
|
+
"""Return the request that documents another ontology, read as the same format."""
|
|
125
|
+
if not self._read_as:
|
|
126
|
+
return None
|
|
127
|
+
return "/extract?" + urlencode({"read_as": self._read_as, "url": iri, "lang": self._lang or "en"})
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def resource(self, iri: str) -> str:
|
|
131
|
+
"""Return the request for the page of a resource."""
|
|
132
|
+
return f"/extract?{self._qs}&resource={quote(iri, safe='/')}"
|
|
133
|
+
|
|
134
|
+
def export(self, ext: str, iri: Optional[str] = None) -> str:
|
|
135
|
+
"""Return the request that serializes the artefact, or a resource, in the format with extension `ext`."""
|
|
136
|
+
resource = f"&resource={quote(iri, safe='/')}" if iri else ""
|
|
137
|
+
return f"/extract?{self._qs}{resource}&format={ext}"
|
|
138
|
+
|
|
139
|
+
def home(self) -> str:
|
|
140
|
+
"""Return the request for the page of the whole artefact."""
|
|
141
|
+
return f"/extract?{self._qs}"
|
|
139
142
|
|
|
140
143
|
|
|
141
144
|
# ==========================================================================
|
|
@@ -159,22 +162,19 @@ def _nav_qs(read_as: str, url, upload_id, lang, imported, closure, partial_impor
|
|
|
159
162
|
|
|
160
163
|
return urlencode(p)
|
|
161
164
|
|
|
162
|
-
def _render_view(request,
|
|
165
|
+
def _render_view(request, artefact, *, resource, lang, source_url, upload_id, read_as,
|
|
163
166
|
imported=None, closure=None, partial_import=None):
|
|
164
167
|
"""Render one resource (or the whole artefact) as the minified viewer page.
|
|
165
168
|
|
|
169
|
+
The page is rendered from the viewer contract alone: every address in it comes from
|
|
170
|
+
_WebLinks, which keeps the current source and options in the query string.
|
|
166
171
|
The body is minified after rendering, so Content-Length is recomputed:
|
|
167
172
|
keeping the old value would truncate the response.
|
|
168
173
|
"""
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
data
|
|
172
|
-
resp =
|
|
173
|
-
"source_url": source_url,
|
|
174
|
-
"upload_id": upload_id,
|
|
175
|
-
"nav_qs": _nav_qs(read_as, source_url, upload_id, lang, imported, closure, partial_import),
|
|
176
|
-
**data,
|
|
177
|
-
})
|
|
174
|
+
links = _WebLinks(_nav_qs(read_as, source_url, upload_id, lang, imported, closure, partial_import),
|
|
175
|
+
read_as, lang)
|
|
176
|
+
data = artefact.viewer.view(resource_uri=resource, language=lang, links=links)
|
|
177
|
+
resp = _templates_for(artefact.viewer.template_dirs()).TemplateResponse(request, "page.html", dict(data))
|
|
178
178
|
resp.body = _minify(resp.body.decode("utf-8")).encode("utf-8")
|
|
179
179
|
resp.headers["content-length"] = str(len(resp.body))
|
|
180
180
|
return resp
|
|
@@ -208,10 +208,8 @@ def _load_url(url, read_as, imported, closure, partial_import, warnings, use_cac
|
|
|
208
208
|
path = spool.get_path(token)
|
|
209
209
|
if use_cache and os.path.exists(path):
|
|
210
210
|
# cache hit: rebuild from the saved Turtle
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
warnings=warnings, source_name=url)
|
|
214
|
-
return reader
|
|
211
|
+
return core.read(path, read_as, imported=imported, closure=closure,
|
|
212
|
+
partial_import=partial_import, warnings=warnings, source_name=url)
|
|
215
213
|
if not use_cache:
|
|
216
214
|
# cache=false: drop the stale copy so the fresh fetch replaces it
|
|
217
215
|
try:
|
|
@@ -219,22 +217,20 @@ def _load_url(url, read_as, imported, closure, partial_import, warnings, use_cac
|
|
|
219
217
|
except OSError:
|
|
220
218
|
pass
|
|
221
219
|
# cache miss (or forced refresh): fetch and process the URL
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
warnings=warnings)
|
|
220
|
+
artefact = core.read(url, read_as, imported=imported, closure=closure,
|
|
221
|
+
partial_import=partial_import, warnings=warnings)
|
|
225
222
|
# persist ONLY the main document: modules are re-applied on every hit.
|
|
226
223
|
try:
|
|
227
|
-
|
|
228
|
-
main = g.graph(reader._ontology_iri) if isinstance(g, ProvenanceGraph) else g
|
|
224
|
+
main = artefact.main_graph()
|
|
229
225
|
with open(path, "wb") as f:
|
|
230
226
|
f.write(main.serialize(format="turtle").encode("utf-8"))
|
|
231
227
|
except OSError:
|
|
232
228
|
pass
|
|
233
|
-
return
|
|
229
|
+
return artefact
|
|
234
230
|
|
|
235
|
-
def
|
|
231
|
+
def _resolve_artefact(read_as: str, url, upload_id, imported, closure, partial_import, warnings,
|
|
236
232
|
use_cache=True):
|
|
237
|
-
"""
|
|
233
|
+
"""Read an artefact from an uploaded file or from a URL.
|
|
238
234
|
|
|
239
235
|
Raises ArtefactValidationError when the upload has expired or when neither
|
|
240
236
|
source is given. Uploads win over URLs and are never re-fetched.
|
|
@@ -245,10 +241,8 @@ def _resolve_reader(read_as: str, url, upload_id, imported, closure, partial_imp
|
|
|
245
241
|
if not os.path.exists(path):
|
|
246
242
|
raise ArtefactValidationError("Upload expired, please re-upload",
|
|
247
243
|
context={"upload_id": upload_id})
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
warnings=warnings)
|
|
251
|
-
return reader
|
|
244
|
+
return core.read(path, read_as, imported=imported, closure=closure,
|
|
245
|
+
partial_import=partial_import, warnings=warnings)
|
|
252
246
|
if url:
|
|
253
247
|
return _load_url(url, read_as, imported, closure, partial_import, warnings, use_cache=use_cache)
|
|
254
248
|
raise ArtefactValidationError("Missing 'url' or 'upload_id'")
|
|
@@ -264,61 +258,19 @@ async def _read_validated_upload(file: UploadFile) -> bytes:
|
|
|
264
258
|
|
|
265
259
|
|
|
266
260
|
# ==========================================================================
|
|
267
|
-
# 8. ZIP
|
|
261
|
+
# 8. ZIP RESPONSE
|
|
268
262
|
# ==========================================================================
|
|
269
|
-
def
|
|
270
|
-
"""
|
|
263
|
+
def _zip_response(artefact, slug: str, lang: Optional[str] = None) -> FileResponse:
|
|
264
|
+
"""Build the static site as a ZIP and return it as a download.
|
|
271
265
|
|
|
272
|
-
|
|
273
|
-
|
|
266
|
+
Blocking and disk-heavy: callers must keep it off the event loop. The build
|
|
267
|
+
directory is removed once the response has been sent. A service at capacity
|
|
268
|
+
becomes a 503 with Retry-After, so clients know when to come back.
|
|
274
269
|
"""
|
|
275
|
-
if not name:
|
|
276
|
-
return "ontology"
|
|
277
|
-
raw = name.rstrip("/").split("#")[0].split("/")[-1]
|
|
278
|
-
raw = re.sub(r"\.(ttl|rdf|owl|n3|nt|jsonld|xml)$", "", raw, flags=re.I)
|
|
279
|
-
raw = re.sub(r"[^A-Za-z0-9._-]", "-", raw).strip("-.")
|
|
280
|
-
if not raw or raw in {".", ".."}:
|
|
281
|
-
return "ontology"
|
|
282
|
-
return raw[:64]
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
def _zip_response(reader, slug: str, lang: Optional[str] = None) -> FileResponse:
|
|
286
|
-
"""Render the whole static site and return it as a ZIP download.
|
|
287
|
-
|
|
288
|
-
Blocking and disk-heavy: callers must keep it off the event loop.
|
|
289
|
-
"""
|
|
290
|
-
from lode.builder import build_html # lazy: avoids a circular import
|
|
291
|
-
|
|
292
|
-
if not _BUILD_SLOTS.acquire(timeout=_BUILD_WAIT):
|
|
293
|
-
raise HTTPException(503, "Server busy: too many concurrent builds",
|
|
294
|
-
headers={"Retry-After": "30"})
|
|
295
|
-
workdir = None
|
|
296
270
|
try:
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
workdir = tempfile.mkdtemp(prefix="build_", dir=BUILD_DIR)
|
|
302
|
-
site_dir = Path(workdir) / slug
|
|
303
|
-
site_dir.mkdir(parents=True, exist_ok=True)
|
|
304
|
-
|
|
305
|
-
build_html(reader.get_viewer(), site_dir, lang=lang or "en", reader=reader)
|
|
306
|
-
|
|
307
|
-
zip_path = shutil.make_archive(
|
|
308
|
-
os.path.join(workdir, f"{slug}-lode"), "zip",
|
|
309
|
-
root_dir=workdir, base_dir=slug,
|
|
310
|
-
)
|
|
311
|
-
# The loose pages are redundant once archived: halve the disk peak.
|
|
312
|
-
shutil.rmtree(site_dir, ignore_errors=True)
|
|
313
|
-
except BaseException:
|
|
314
|
-
if workdir:
|
|
315
|
-
shutil.rmtree(workdir, ignore_errors=True)
|
|
316
|
-
raise
|
|
317
|
-
finally:
|
|
318
|
-
# Free the slot as soon as the build ends, not when the client is done
|
|
319
|
-
# downloading: a slow reader must not hold a build slot hostage.
|
|
320
|
-
_BUILD_SLOTS.release()
|
|
321
|
-
|
|
271
|
+
zip_path, workdir = build_zip(artefact, slug, lang)
|
|
272
|
+
except BuildCapacityError as exc:
|
|
273
|
+
raise HTTPException(503, str(exc), headers={"Retry-After": str(exc.retry_after)})
|
|
322
274
|
return FileResponse(
|
|
323
275
|
zip_path,
|
|
324
276
|
media_type="application/zip",
|
|
@@ -398,8 +350,8 @@ async def limit_upload_size(request: Request, call_next):
|
|
|
398
350
|
async def input_web_interface(request: Request):
|
|
399
351
|
"""Serve the HTML input form, the entry point of the web interface."""
|
|
400
352
|
return templates.TemplateResponse(request, "index.html", {
|
|
401
|
-
"formats":
|
|
402
|
-
"enabled_formats":
|
|
353
|
+
"formats": profiles.names(),
|
|
354
|
+
"enabled_formats": profiles.names(enabled_only=True),
|
|
403
355
|
})
|
|
404
356
|
|
|
405
357
|
|
|
@@ -415,7 +367,7 @@ async def health_check():
|
|
|
415
367
|
@app.get("/extract", tags=["extract"], summary="Render an artefact as HTML or RDF")
|
|
416
368
|
def extract_get(
|
|
417
369
|
request: Request,
|
|
418
|
-
read_as:
|
|
370
|
+
read_as: str = Query(description="Artefact type: the name of a registered format, e.g. 'owl' or 'skos'"),
|
|
419
371
|
url: Optional[str] = Query(None, description="Public http(s) URL of the artefact"),
|
|
420
372
|
upload_id: Optional[str] = Query(None, description="Token of a previously uploaded file"),
|
|
421
373
|
resource: Optional[str] = Query(None, description="IRI of a single resource; omit for the whole ontology"),
|
|
@@ -429,10 +381,8 @@ def extract_get(
|
|
|
429
381
|
):
|
|
430
382
|
"""Render an artefact. Returns HTML by default, or RDF when a serialization
|
|
431
383
|
is requested via the `format` query or the `Accept` header."""
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
reader = _resolve_reader(read_as.value, url, upload_id, imported, closure, partial_import,
|
|
435
|
-
warnings, use_cache=cache)
|
|
384
|
+
artefact = _resolve_artefact(read_as, url, upload_id, imported, closure, partial_import,
|
|
385
|
+
warnings, use_cache=cache)
|
|
436
386
|
|
|
437
387
|
# Content negotiation: triples of the loaded artefact only, quads are never negotiable
|
|
438
388
|
accept = request.headers.get("accept", "text/html")
|
|
@@ -452,18 +402,18 @@ def extract_get(
|
|
|
452
402
|
break
|
|
453
403
|
|
|
454
404
|
if serial:
|
|
405
|
+
serialized = core.export(artefact, serial["fmt"], resource)
|
|
455
406
|
if resource:
|
|
456
|
-
serialized = reader.get_viewer().export_resource(resource, serial["fmt"])
|
|
457
407
|
filename = iri_local_name(resource) or "resource"
|
|
458
408
|
else:
|
|
459
|
-
serialized = reader.get_main_graph().serialize(format=serial["fmt"])
|
|
460
409
|
filename = (iri_local_name(url) if url else "") or "graph"
|
|
410
|
+
|
|
461
411
|
return Response(content=serialized, media_type=serial["mime"],
|
|
462
412
|
headers={"Content-Disposition": f'inline; filename="{filename}.{serial["ext"]}"'})
|
|
463
413
|
|
|
464
414
|
logger.info("=== REQUEST SUCCESS ===")
|
|
465
|
-
return _render_view(request,
|
|
466
|
-
source_url=url, upload_id=upload_id, read_as=read_as
|
|
415
|
+
return _render_view(request, artefact, resource=resource, lang=lang,
|
|
416
|
+
source_url=url, upload_id=upload_id, read_as=read_as,
|
|
467
417
|
imported=imported, closure=closure, partial_import=partial_import)
|
|
468
418
|
|
|
469
419
|
|
|
@@ -471,7 +421,7 @@ def extract_get(
|
|
|
471
421
|
summary="Render an uploaded artefact as HTML")
|
|
472
422
|
async def extract_post(
|
|
473
423
|
request: Request,
|
|
474
|
-
read_as:
|
|
424
|
+
read_as: str = Form(..., description="Artefact type of the uploaded file: a registered format, e.g. 'owl' or 'skos'"),
|
|
475
425
|
file: UploadFile = File(..., description="The artefact file (ttl, rdf, owl…)"),
|
|
476
426
|
resource: Optional[str] = Form(None, description="IRI of a single resource; omit for the whole ontology"),
|
|
477
427
|
lang: Optional[str] = Form(None, description="Language tag for labels, e.g. 'en', 'it'"),
|
|
@@ -483,19 +433,17 @@ async def extract_post(
|
|
|
483
433
|
"""Render an artefact from an uploaded file instead of a URL."""
|
|
484
434
|
logger.info("=== FILE UPLOAD START ===")
|
|
485
435
|
logger.info(f"Filename: {file.filename}")
|
|
486
|
-
logger.info(f"Format: {read_as
|
|
436
|
+
logger.info(f"Format: {read_as}")
|
|
487
437
|
|
|
488
|
-
_check_format_enabled(read_as)
|
|
489
438
|
content = await _read_validated_upload(file)
|
|
490
439
|
token = spool.save(content)
|
|
491
440
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
reader.load_instances, spool.get_path(token), read_as.value,
|
|
441
|
+
artefact = await run_in_threadpool(
|
|
442
|
+
core.read, spool.get_path(token), read_as,
|
|
495
443
|
imported=imported, closure=closure, partial_import=partial_import, warnings=warnings,
|
|
496
444
|
)
|
|
497
|
-
return _render_view(request,
|
|
498
|
-
source_url=None, upload_id=token, read_as=read_as
|
|
445
|
+
return _render_view(request, artefact, resource=resource, lang=lang,
|
|
446
|
+
source_url=None, upload_id=token, read_as=read_as,
|
|
499
447
|
imported=imported, closure=closure, partial_import=partial_import)
|
|
500
448
|
|
|
501
449
|
|
|
@@ -504,7 +452,7 @@ async def extract_post(
|
|
|
504
452
|
# ==========================================================================
|
|
505
453
|
@app.get("/build", tags=["build"], summary="Build a static site (ZIP) from a URL")
|
|
506
454
|
def build_get(
|
|
507
|
-
read_as:
|
|
455
|
+
read_as: str = Query(description="Artefact type: the name of a registered format, e.g. 'owl' or 'skos'"),
|
|
508
456
|
url: Optional[str] = Query(None, description="Public http(s) URL of the artefact"),
|
|
509
457
|
upload_id: Optional[str] = Query(None, description="Token of a previously uploaded file"),
|
|
510
458
|
lang: Optional[str] = Query(None, description="Language tag for labels, e.g. 'en', 'it'"),
|
|
@@ -516,15 +464,14 @@ def build_get(
|
|
|
516
464
|
):
|
|
517
465
|
"""Static documentation site as a ZIP. Sync on purpose: FastAPI runs
|
|
518
466
|
non-async endpoints in the threadpool, keeping the event loop free."""
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
return _zip_response(reader, _slug(url), lang)
|
|
467
|
+
artefact = _resolve_artefact(read_as, url, upload_id, imported, closure, partial_import,
|
|
468
|
+
warnings, use_cache=cache)
|
|
469
|
+
return _zip_response(artefact, slugify(url), lang)
|
|
523
470
|
|
|
524
471
|
|
|
525
472
|
@app.post("/build", tags=["build"], summary="Build a static site (ZIP) from an upload")
|
|
526
473
|
async def build_post(
|
|
527
|
-
read_as:
|
|
474
|
+
read_as: str = Form(..., description="Artefact type of the uploaded file: a registered format, e.g. 'owl' or 'skos'"),
|
|
528
475
|
file: UploadFile = File(..., description="The artefact file (ttl, rdf, owl…)"),
|
|
529
476
|
lang: Optional[str] = Form(None, description="Language tag for labels, e.g. 'en', 'it'"),
|
|
530
477
|
imported: Optional[bool] = Form(None, description="Also load owl:imports (depth 1)"),
|
|
@@ -533,16 +480,14 @@ async def build_post(
|
|
|
533
480
|
warnings: bool = Form(False, description="Collect and show reader warnings"),
|
|
534
481
|
):
|
|
535
482
|
"""Build the static site from an uploaded file instead of a URL."""
|
|
536
|
-
_check_format_enabled(read_as)
|
|
537
483
|
content = await _read_validated_upload(file)
|
|
538
484
|
token = spool.save(content)
|
|
539
485
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
reader.load_instances, spool.get_path(token), read_as.value,
|
|
486
|
+
artefact = await run_in_threadpool(
|
|
487
|
+
core.read, spool.get_path(token), read_as,
|
|
543
488
|
imported=imported, closure=closure, partial_import=partial_import, warnings=warnings,
|
|
544
489
|
)
|
|
545
|
-
return await run_in_threadpool(_zip_response,
|
|
490
|
+
return await run_in_threadpool(_zip_response, artefact, slugify(file.filename), lang)
|
|
546
491
|
|
|
547
492
|
|
|
548
493
|
# ==========================================================================
|