Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added utils.get_datatype_class() #804

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion ontopy/factpluspluswrapper/sync_factpp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""# `ontopy.factpluspluswrapper.syncfatpp`"""
"""Interface FaCT++ reasoner."""

# pylint: disable=protected-access
from collections import defaultdict
Expand Down
9 changes: 8 additions & 1 deletion ontopy/ontodoc_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,14 @@ def write_conf_template(
license = md.graph.value(URIRef(iri), DCTERMS.license, default=None)
release = md.graph.value(URIRef(iri), OWL.versionInfo, default="1.0")

author = ", ".join(a.value for a in authors) if authors else "<AUTHOR>"
# FIXME: If authors are URIs, extract their names from the URI
author = (
", ".join(
a.value if hasattr(a, "value") else str(a) for a in authors
)
if authors
else "<AUTHOR>"
)
copyright = license if license else f"{time.strftime('%Y')}, {author}"

content = f"""
Expand Down
2 changes: 1 addition & 1 deletion ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from owlready2 import AnnotationPropertyClass

from ontopy.factpluspluswrapper.sync_factpp import sync_reasoner_factpp
from ontopy.utils import (
from ontopy.utils import ( # pylint: disable=cyclic-import
english,
asstring,
read_catalog,
Expand Down
6 changes: 4 additions & 2 deletions ontopy/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import owlready2
from owlready2 import AnnotationPropertyClass, ThingClass, PropertyClass
from owlready2 import Metadata, Thing, Restriction, Namespace
from ontopy.utils import EMMOntoPyException
from ontopy.ontology import Ontology as OntopyOntology
from ontopy.utils import EMMOntoPyException # pylint: disable=cyclic-import
from ontopy.ontology import ( # pylint: disable=cyclic-import
Ontology as OntopyOntology,
)


def render_func(entity):
Expand Down
56 changes: 54 additions & 2 deletions ontopy/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Some generic utility functions.
"""

# pylint: disable=protected-access,invalid-name
# pylint: disable=protected-access,invalid-name,redefined-outer-name
# pylint: disable=import-outside-toplevel
import os
import sys
import re
import datetime
import inspect
import tempfile
import textwrap
from pathlib import Path
from typing import TYPE_CHECKING
import urllib.request
Expand All @@ -22,7 +24,6 @@

import owlready2


if TYPE_CHECKING:
from typing import Optional, Union

Expand Down Expand Up @@ -265,6 +266,11 @@
f" {string!r}" if isinstance(expr.value, str) else f" {string}"
)
return res

Datatype = get_datatype_class()
if isinstance(expr, Datatype):
return str(expr).rsplit(".", 1)[-1]

Check warning on line 272 in ontopy/utils.py

View check run for this annotation

Codecov / codecov/patch

ontopy/utils.py#L272

Added line #L272 was not covered by tests

if isinstance(expr, owlready2.Or):
res = " or ".join(
[
Expand Down Expand Up @@ -915,3 +921,49 @@
new = getattr(e, src.name).first()
if new and new not in getattr(e, dst.name):
getattr(e, dst.name).append(new)


def get_datatype_class():
"""Return a class representing rdfs:Datatype."""
# This is a hack, but I find no other way to access rdfs:Datatype
# with Owlready2...

# These cannot be imported at module initialisation time...
from ontopy import get_ontology
from ontopy import utils # pylint: disable=import-self

# Check is Datatype is cached in module __dict__
if hasattr(utils, "_Datatype"):
return utils._Datatype

# Use try-finally clause instead of delete=True to avoid problems
# with file-locking on Windows
filename = None
try:
with tempfile.NamedTemporaryFile(
suffix=".ttl", mode="wt", delete=False
) as f:
filename = f.name
f.write(
textwrap.dedent(
"""
@prefix : <http://example.com/onto#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

<http://example.com/onto> rdf:type owl:Ontology .

:new_datatype rdf:type rdfs:Datatype .
"""
)
)

onto = get_ontology(filename).load()
Datatype = onto.new_datatype.__class__
utils._Datatype = Datatype # cache Datatype in module __dict__
return Datatype

finally:
if filename:
os.unlink(filename)
7 changes: 7 additions & 0 deletions tests/ontopy_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ def test_preferred_language():
assert get_preferred_language(pl, "en") == "Vertebrate"
assert get_preferred_language(pl, "no") == "Virveldyr"
assert get_preferred_language(pl, "it") == "Vertebrate"


def test_datatype_class():
from ontopy.utils import get_datatype_class

Datatype = get_datatype_class()
assert "Datatype" in repr(Datatype)
Loading