Skip to content
Open
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
6 changes: 3 additions & 3 deletions namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions test/regtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down