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

write catalog now writes relative paths per default #483

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,10 @@ def append(onto):
if recursive:
append(self)
write_catalog(
mappings, output=catalog_file, dir=dir, append=append_catalog
mappings,
output=catalog_file,
directory=dir,
append=append_catalog,
)

def get_imported_ontologies(self, recursive=False):
Expand Down
15 changes: 11 additions & 4 deletions ontopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ def load_uri(uri, dirname):
url = f"{baseuri}/{uri_as_str}"
else:
url = os.path.join(baseuri if baseuri else dirname, uri_as_str)
# url = normalise_url(url)

iris.setdefault(uri.attrib["name"], url)
if recursive:
Expand Down Expand Up @@ -381,7 +380,8 @@ def load_uri(uri, dirname):
def write_catalog(
mappings: dict,
output: "Union[str, Path]" = "catalog-v001.xml",
dir: "Union[str, Path]" = ".",
directory: "Union[str, Path]" = ".",
relative_paths: bool = True,
append: bool = False,
): # pylint: disable=redefined-builtin
"""Write catalog file do disk.
Expand All @@ -391,12 +391,19 @@ def write_catalog(
(URIs). It has the same format as the dict returned by
read_catalog().
output: name of catalog file.
dir: directory path to the catalog file. Only used if `output`
directory: directory path to the catalog file. Only used if `output`
is a relative path.
relative_paths: whether to write absolute or relative paths to
for file paths inside the catalog file.
append: whether to append to a possible existing catalog file.
If false, an existing file will be overwritten.
"""
filename = (Path(dir) / output).resolve()
web_protocol = "http://", "https://", "ftp://"
if relative_paths:
for key, item in mappings.items():
if not item.startswith(web_protocol):
mappings[key] = os.path.relpath(item, Path(directory).resolve())
filename = (Path(directory) / output).resolve()
if filename.exists() and append:
iris = read_catalog(filename)
iris.update(mappings)
Expand Down
7 changes: 7 additions & 0 deletions tests/catalogs_for_testing/catalog-v001.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog prefer="public" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<group id="Folder Repository, directory=, recursive=true, Auto-Update=false, version=2" prefer="public" xml:base="">
<uri name="http://emmo.info/testonto/0.1.0" uri="testonto.ttl"/>
<uri name="http://emmo.info/testonto/0.1.0/models" uri="models.ttl"/>
</group>
</catalog>
7 changes: 7 additions & 0 deletions tests/catalogs_for_testing/catalog-w-special-name.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog prefer="public" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<group id="Folder Repository, directory=, recursive=true, Auto-Update=false, version=2" prefer="public" xml:base="">
<uri name="http://emmo.info/testonto/0.1.0" uri="testonto.ttl"/>
<uri name="http://emmo.info/testonto/0.1.0/models" uri="models.ttl"/>
</group>
</catalog>
70 changes: 66 additions & 4 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from typing import TYPE_CHECKING
import defusedxml.ElementTree as ET
from ontopy.utils import read_catalog, ReadCatalogError, write_catalog

if TYPE_CHECKING:
from pathlib import Path


def test_catalog(repo_dir: "Path", tmpdir: "Path") -> None:
from ontopy.utils import read_catalog, ReadCatalogError, write_catalog

ontodir = repo_dir / "tests" / "testonto"
ontodir = repo_dir / "tests" / "catalogs_for_testing"
catalog_expected = {
"http://emmo.info/testonto/0.1.0": str(ontodir / "testonto.ttl"),
"http://emmo.info/testonto/0.1.0/models": str(ontodir / "models.ttl"),
}

catalog = read_catalog(str(ontodir / "catalog-v001.xml"))
catalog = read_catalog(str(ontodir / "catalog-w-special-name.xml"))
assert catalog == catalog_expected

catalog = read_catalog(str(ontodir))
Expand All @@ -26,6 +27,8 @@ def test_catalog(repo_dir: "Path", tmpdir: "Path") -> None:
assert catalog == catalog_expected
assert catalog_paths == set([str(ontodir)])

write_catalog(catalog, output="cat.xml")

catalog = read_catalog(
"https://raw.githubusercontent.com/emmo-repo/EMMO/master/"
"catalog-v001.xml"
Expand Down Expand Up @@ -61,6 +64,65 @@ def test_catalog(repo_dir: "Path", tmpdir: "Path") -> None:
assert "/abc/emmo.ttl" in catalog.values()

tmp_catalog_path = tmpdir / "tmp-catalog.xml"
write_catalog(catalog, output=tmp_catalog_path)
write_catalog(catalog, output=tmp_catalog_path, relative_paths=False)
tmp_catalog = read_catalog(str(tmp_catalog_path))
assert tmp_catalog == catalog


def test_write_catalog_choosing_relative_paths(
repo_dir: "Path", tmpdir: "Path"
) -> None:
ontodir = repo_dir / "tests" / "catalogs_for_testing"
catalog1 = read_catalog(str(ontodir))
write_catalog(
catalog1,
output=(tmpdir / "cat-relative-paths.xml"),
relative_paths=True,
)
catalog2 = read_catalog(str(ontodir))
write_catalog(
catalog2,
output=(tmpdir / "cat-absolute-paths.xml"),
relative_paths=False,
)

catalog_w_absolute_paths_xml = ET.parse(tmpdir / "cat-absolute-paths.xml")
catalog_w_relative_paths_xml = ET.parse(tmpdir / "cat-relative-paths.xml")

catalog_w_absolute_paths_root = catalog_w_absolute_paths_xml.getroot()
catalog_w_relative_paths_root = catalog_w_relative_paths_xml.getroot()

absolutepaths = [
uri.attrib["uri"] for uri in catalog_w_absolute_paths_root[0]
]
relativepaths = [
uri.attrib["uri"] for uri in catalog_w_relative_paths_root[0]
]

ontodir = repo_dir / "tests" / "catalogs_for_testing"

catalog_expected_relative_paths = {
str("tests/catalogs_for_testing/testonto.ttl"),
str("tests/catalogs_for_testing/models.ttl"),
}

catalog_expected_absolute_paths = {
str(ontodir / "testonto.ttl"),
str(ontodir / "models.ttl"),
}

assert set(absolutepaths) == set(catalog_expected_absolute_paths)
assert set(relativepaths) == set(catalog_expected_relative_paths)

catalog3 = read_catalog(
"https://raw.githubusercontent.com/emmo-repo/EMMO/master/"
"catalog-v001.xml"
)
write_catalog(catalog3, output=(tmpdir / "cat-with-http-paths.xml"))

catalog_w_http_paths_xml = ET.parse(tmpdir / "cat-with-http-paths.xml")
catalog_w_http_paths_root = catalog_w_http_paths_xml.getroot()

paths = [uri.attrib["uri"] for uri in catalog_w_http_paths_root[0]]

assert set(catalog3.values()) == set(paths)