From f96665b79a96c3e25d9b28d0d0009973d4698683 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 2 Jun 2024 15:02:17 +0200 Subject: [PATCH 1/4] Added figures to generated documentation Also, added prefLabel as anchoring points such that one can use the browser to easily get documentation of a concept one knows the prefLabel of. For example, entering https://w3id.org/emmo#Atom should bring you to the section in the html doc that documents Atom. --- ontopy/ontodoc_rst.py | 48 ++++++++++++++++++++++++++++++++++--------- ontopy/utils.py | 5 ++++- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/ontopy/ontodoc_rst.py b/ontopy/ontodoc_rst.py index 20ac86a5f..0d9d4bd84 100644 --- a/ontopy/ontodoc_rst.py +++ b/ontopy/ontodoc_rst.py @@ -60,6 +60,10 @@ def __init__( self.individuals = set() self.datatypes = set() + # All navigation IDs added by the ontology. Used to warn about + # dublicated IDs + self.navids = set() + if ontology: self.add_ontology(ontology) @@ -137,7 +141,7 @@ def get_refdoc( subsections: str = "all", header: bool = True, ) -> str: - # pylint: disable=too-many-branches,too-many-locals + # pylint: disable=too-many-branches,too-many-locals,too-many-statements """Return reference documentation of all module entities. Arguments: @@ -186,15 +190,23 @@ def add_header(name): ] ) - def add_keyvalue(key, value, escape=True, htmllink=True): + def add_keyvalue( + key, value, escape=True, htmllink=True, show_figure=True + ): """Help function for adding a key-value row to table.""" - if escape: - value = html.escape(str(value)) - if htmllink: - value = re.sub( - r"(https?://[^\s]+)", r'\1', value - ) - value = value.replace("\n", "
") + if show_figure and re.match( + r"^(http://|https://)[a-zA-Z.+?@/_-]+\.(png|jpg|jpeg|svg|gif)$", + value, + ): + value = f'' + else: + if escape: + value = html.escape(str(value)) + if htmllink: + value = re.sub( + r"(https?://[^\s]+)", r'\1', value + ) + value = value.replace("\n", "
") lines.extend( [ " ", @@ -222,11 +234,27 @@ def add_keyvalue(key, value, escape=True, htmllink=True): ) for entity in sorted(maps[subsection], key=get_label): label = get_label(entity) + navid = navid2 = "" + if entity.name in self.navids: + warnings.warn(f"duplicated entity names: {entity.name}") + else: + self.navids.add(entity.name) + navid = f'
' + if hasattr(entity, "prefLabel"): + preflabel = str(entity.prefLabel.first()) + if preflabel != entity.name: + if preflabel in self.navids: + warnings.warn(f"duplicated prefLabel: {preflabel}") + else: + self.navids.add(preflabel) + navid2 = f'
' + lines.extend( [ ".. raw:: html", "", - f'
', + navid, + navid2, "", f"{label}", "^" * len(label), diff --git a/ontopy/utils.py b/ontopy/utils.py index aba32af77..135aa05f4 100644 --- a/ontopy/utils.py +++ b/ontopy/utils.py @@ -888,8 +888,11 @@ def copy_annotation(onto, src, dst): """ if onto.world[src]: src = onto.world[src] - else: + elif src in onto: src = onto[src] + else: + warnings.warn(f"skipping copy for missing source annotation: {src}") + return if onto.world[dst]: dst = onto.world[dst] From be8ad99cc814bcc574a9be5217d5e545bba2e7b8 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 6 Nov 2024 13:20:04 +0100 Subject: [PATCH 2/4] Fixed a few minor issues --- ontopy/ontodoc_rst.py | 3 ++- ontopy/utils.py | 5 ++--- tests/testonto/mammal.ttl | 11 +++++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ontopy/ontodoc_rst.py b/ontopy/ontodoc_rst.py index 0d9d4bd84..c184996c8 100644 --- a/ontopy/ontodoc_rst.py +++ b/ontopy/ontodoc_rst.py @@ -3,6 +3,7 @@ """ # pylint: disable=fixme,too-many-lines,no-member,too-many-instance-attributes +# pylint: disable=invalid-name import html import re import time @@ -196,7 +197,7 @@ def add_keyvalue( """Help function for adding a key-value row to table.""" if show_figure and re.match( r"^(http://|https://)[a-zA-Z.+?@/_-]+\.(png|jpg|jpeg|svg|gif)$", - value, + asstring(value, ontology=self.ontology), ): value = f'' else: diff --git a/ontopy/utils.py b/ontopy/utils.py index 819a1a4cd..ddb21cce8 100644 --- a/ontopy/utils.py +++ b/ontopy/utils.py @@ -1,7 +1,7 @@ """Some generic utility functions. """ -# pylint: disable=protected-access +# pylint: disable=protected-access,invalid-name import os import sys import re @@ -167,8 +167,7 @@ def asstring( """Returns a string representation of `expr`. Arguments: - expr: The entity, restriction or a logical expression or these - to represent. + expr: The entity, restriction or logical expression to represent. link: A template for links. May contain the following variables: - {iri}: The full IRI of the concept. - {name}: Name-part of IRI. diff --git a/tests/testonto/mammal.ttl b/tests/testonto/mammal.ttl index 5bc5937d3..c79f5be93 100644 --- a/tests/testonto/mammal.ttl +++ b/tests/testonto/mammal.ttl @@ -5,13 +5,15 @@ @prefix xsd: . @prefix rdfs: . @prefix skos: . +@prefix foaf: . @prefix animal: . @base . rdf:type owl:Ontology ; - owl:versionIRI ; - owl:imports ; - owl:versionInfo "0.1" . + owl:versionIRI ; + owl:imports ; + owl:versionInfo "0.1" ; + foaf:logo "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Mammal_collage.png/450px-Mammal_collage.png" . ################################################################# # Classes @@ -20,7 +22,8 @@ ### https://w3id.org/emmo/mammal#Cat :Cat rdf:type owl:Class ; rdfs:subClassOf :Felines ; - skos:prefLabel "Cat"@en . + skos:prefLabel "Cat"@en ; + foaf:img "https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Orange_tabby_cat_sitting_on_fallen_leaves-Hisashi-01A.jpg/168px-Orange_tabby_cat_sitting_on_fallen_leaves-Hisashi-01A.jpg" . ### https://w3id.org/emmo/mammal#Felines From 7bff529dadab588cc3fd36b70fbfc17b0a94edd9 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 6 Nov 2024 13:51:25 +0100 Subject: [PATCH 3/4] Updated regex --- ontopy/ontodoc_rst.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ontopy/ontodoc_rst.py b/ontopy/ontodoc_rst.py index c184996c8..311d4482e 100644 --- a/ontopy/ontodoc_rst.py +++ b/ontopy/ontodoc_rst.py @@ -196,7 +196,7 @@ def add_keyvalue( ): """Help function for adding a key-value row to table.""" if show_figure and re.match( - r"^(http://|https://)[a-zA-Z.+?@/_-]+\.(png|jpg|jpeg|svg|gif)$", + r"^https?://[a-zA-Z0-9.+?@/_-]+\.(png|jpg|jpeg|svg|gif)$", asstring(value, ontology=self.ontology), ): value = f'' From 56d977332c0195fc584cd2601c7e775c7fe83558 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 6 Nov 2024 13:56:14 +0100 Subject: [PATCH 4/4] Added documentation of arguments --- ontopy/ontodoc_rst.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ontopy/ontodoc_rst.py b/ontopy/ontodoc_rst.py index 311d4482e..1c07c76ca 100644 --- a/ontopy/ontodoc_rst.py +++ b/ontopy/ontodoc_rst.py @@ -194,7 +194,15 @@ def add_header(name): def add_keyvalue( key, value, escape=True, htmllink=True, show_figure=True ): - """Help function for adding a key-value row to table.""" + """Help function for adding a key-value row to table. + + Arguments: + key: Key to show in the table. + value: Value to show in the table. + htmllink: Whether to add html link to value. + show_figure: Whether to show figure in value column. + + """ if show_figure and re.match( r"^https?://[a-zA-Z0-9.+?@/_-]+\.(png|jpg|jpeg|svg|gif)$", asstring(value, ontology=self.ontology),