diff --git a/namespace.py b/namespace.py index 723c925..3ad3e18 100644 --- a/namespace.py +++ b/namespace.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . -import importlib, urllib.request, urllib.parse +import importlib, os, urllib.request, urllib.parse from owlready2.base import * from owlready2.base import _universal_abbrev_2_iri, _universal_iri_2_abbrev, _universal_abbrev_2_datatype, _universal_datatype_2_abbrev @@ -481,7 +481,7 @@ def new_blank_node(self): return self.graph.new_blank_node() def save(self, file = None, format = "rdfxml", **kargs): if file is None: self.graph.commit() - elif isinstance(file, str): + elif isinstance(file, (str, os.PathLike)): if _LOG_LEVEL: print("* Owlready2 * Saving world %s to %s..." % (self, file), file = sys.stderr) file = open(file, "wb") self.graph.save(file, format, **kargs) @@ -870,7 +870,7 @@ def save(self, file = None, format = "rdfxml", **kargs): if _LOG_LEVEL: print("* Owlready2 * Saving ontology %s to %s..." % (self.name, getattr(file, "name", "???")), file = sys.stderr) self.graph.save(file, format, **kargs) file.close() - elif isinstance(file, str): + elif isinstance(file, (str, os.PathLike)): if _LOG_LEVEL: print("* Owlready2 * Saving ontology %s to %s..." % (self.name, file), file = sys.stderr) file = open(file, "wb") self.graph.save(file, format, **kargs) diff --git a/test/regtest.py b/test/regtest.py index 74c0301..952ece7 100644 --- a/test/regtest.py +++ b/test/regtest.py @@ -323,6 +323,33 @@ def test_world_10(self): sql = db.cursor() sql.execute("""SELECT * from ontologies;""") assert sql.fetchall() == [] + + def test_world_save_pathlike(self): + import pathlib + + filename = pathlib.Path(self.new_tmp_file()) + world = self.new_world() + onto = world.get_ontology("http://test.org/pathlib_world.owl") + + with onto: + class C(Thing): pass + + world.save(file = filename, format = "ntriples") + + assert filename.stat().st_size > 0 + + def test_ontology_save_pathlike(self): + import pathlib + + filename = pathlib.Path(self.new_tmp_file()) + onto = self.new_ontology() + + with onto: + class C(Thing): pass + + onto.save(file = filename, format = "ntriples") + + assert filename.stat().st_size > 0 def test_ontology_1(self):