Skip to content

Commit

Permalink
Merge pull request RDFLib#217 from JaeTLDR/patch-1
Browse files Browse the repository at this point in the history
fix utils.py to not load contents as file paths
  • Loading branch information
edmondchuc committed Apr 6, 2024
2 parents 3f81c2f + f5894ba commit c1ad9f3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
11 changes: 6 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ Release Schedule
:header: "Version", "Date", "Description"
:widths: 15, 10, 30

**3.1.3**, **18 March 2024**, "Relax rdflib version constraint"
**3.1.2**, **18 March 2024**, "Relax httpx version constraint"
**3.1.1**, **19 February 2024**, "Fix release"
**3.1.0**, **19 February 2024**, "Add supermodel mode - supports documenting profiles and modules"
**3.0.5**, **27 April 2023**, "Minor patching"
3.1.4, 6 April 2024, "Fix load_ontology function's detection of data input"
3.1.3, 18 March 2024, "Relax rdflib version constraint"
3.1.2, 18 March 2024, "Relax httpx version constraint"
3.1.1, 19 February 2024, "Fix release"
3.1.0, 19 February 2024, "Add supermodel mode - supports documenting profiles and modules"
3.0.5, 27 April 2023, "Minor patching"
3.0.4, 24 May 2022, "Use of Poetry"
3.0.2, 24 May 2022, "Support for preformatted skos:example literals"
3.0.1, 6 Jan 2022, "Direct HTML generation using dominate; easier to maintain and extend"
Expand Down
12 changes: 10 additions & 2 deletions pylode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ def _get_prop_label(prop_iri: URIRef, back_onts: Graph) -> dict:
return pl


def _is_file(filepath: str) -> bool:
try:
if Path(filepath).is_file():
return True
return False
except:
return False


def load_ontology(ontology: Union[Graph, Path, str]) -> Graph:
"""Loads and ontology into an RDFLib Graph.
Expand All @@ -251,8 +260,7 @@ def load_ontology(ontology: Union[Graph, Path, str]) -> Graph:
if isinstance(ontology, str) and ontology.startswith("http"):
return Graph().parse(location=ontology)
elif isinstance(ontology, str):
# see if it's a file path
if Path(ontology).is_file():
if _is_file(ontology):
return Graph().parse(ontology)
else: # it's data
if ontology.startswith("[") or ontology.startswith("{"):
Expand Down
2 changes: 1 addition & 1 deletion pylode/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.1.3"
__version__ = "3.1.4"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pylode"
version = "3.1.3a"
version = "3.1.4a"
description = "An OWL ontology documentation tool using Python, based on LODE."
authors = ["Nicholas Car <nick@kurrawong.net>"]
readme = "README.rst"
Expand Down
31 changes: 31 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from rdflib.namespace import DCTERMS, OWL, RDF, RDFS, XSD
import pytest

current_dir = Path(__file__).parent


# scope="session" so that this is reused without regeneration in this testing session
@pytest.fixture(scope="session")
Expand Down Expand Up @@ -170,3 +172,32 @@ def test_prop_obj_pair_html(fix_ont, fix_load_background_onts, fix_get_ns):
actual = de_space_html(pp.render(pretty=False))

assert actual == expected, f"Object HTML '{actual}' != '{expected}'"


def test_load_ontology_data():
data = """
@prefix : <http://www.w3.org/ns/dx/prof/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sdo: <https://schema.org/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@base <http://www.w3.org/ns/dx/prof> .
<http://www.w3.org/ns/dx/prof> rdf:type owl:Ontology .
"""
graph = load_ontology(data)
assert len(graph)


def test_load_ontology_file():
filepath = current_dir / "prof.ttl"

graph = load_ontology(filepath)
assert len(graph)

graph = load_ontology(str(filepath))
assert len(graph)

0 comments on commit c1ad9f3

Please sign in to comment.