Repository: SINTEF/dlite (language: python)

Issue:
Python-plugin to accept Posix paths and not only strings
have to convert path to string with Instance.create_from_url(path)


Discussion / hints:

Fix patch:
diff --git a/bindings/python/dlite-collection-python.i b/bindings/python/dlite-collection-python.i
index 21da3b7ab..c45f62612 100644
--- a/bindings/python/dlite-collection-python.i
+++ b/bindings/python/dlite-collection-python.i
@@ -101,19 +101,20 @@ class Collection(Instance):
                 Whether to also save all instances referred to by the
                 collection.
         """
+        loc = str(location) if location else None
         if include_instances:
             c = self._coll
             if isinstance(dst, Storage):
                 _collection_save(c, dst)
             elif location:
-                with Storage(dst, location, options) as s:
+                with Storage(dst, loc, options) as s:
                     _collection_save(c, s)
             else:
                 _collection_save_url(c, dst)
         elif isinstance(dst, Storage):
             Instance.save(self, storage=dst)
         else:
-            Instance.save(self, dst, location, options)
+            Instance.save(self, dst, loc, options)
 
     _coll = property(
         lambda self: _get_collection(id=self.uuid),
diff --git a/bindings/python/dlite-entity-python.i b/bindings/python/dlite-entity-python.i
index 883132543..d8e80f505 100644
--- a/bindings/python/dlite-entity-python.i
+++ b/bindings/python/dlite-entity-python.i
@@ -290,7 +290,7 @@ def get_instance(id: "str", metaid: "str"=None, check_storages: "bool"=True) ->
         required if the storage only contains more one instance).
         """
         return Instance(
-            driver=driver, location=location, options=options, id=id,
+            driver=driver, location=str(location), options=options, id=id,
             dims=(), dimensions=(), properties=()  # arrays
         )
 
@@ -375,10 +375,28 @@ def get_instance(id: "str", metaid: "str"=None, check_storages: "bool"=True) ->
             "create_from_location() is deprecated, use from_location() "
             "instead.", DeprecationWarning, stacklevel=2)
         return Instance(
-            driver=driver, location=location, options=options, id=id,
+            driver=driver, location=str(location), options=options, id=id,
             dims=(), dimensions=(), properties=()  # arrays
         )
 
+    def save(self, dest, location=None, options=None):
+        """Saves this instance to url or storage.
+
+        Call signatures:
+          - save(url)
+          - save(driver, location, options=None)
+          - save(storage)
+        """
+        if isinstance(dest, Storage):
+            self.save_to_storage(storage=dest)
+        elif location:
+            with Storage(dest, str(location), options) as storage:
+                storage.save(self)
+        elif isinstance(dest, str):
+            self.save_to_url(dest)
+        else:
+            raise DLiteError('Arguments do not match any call signature')
+
     def __getitem__(self, ind):
         if isinstance(ind, int):
             return self.get_property_by_index(ind)
diff --git a/bindings/python/dlite-storage-python.i b/bindings/python/dlite-storage-python.i
index f829d519c..255198556 100644
--- a/bindings/python/dlite-storage-python.i
+++ b/bindings/python/dlite-storage-python.i
@@ -1,10 +1,16 @@
-/* -*- c -*-  (not really, but good for syntax highlighting) */
+/* -*- Python -*-  (not really, but good for syntax highlighting) */
 
 /* Python-spesific extensions to dlite-storage.i */
 
 %extend _DLiteStorage {
 
   %pythoncode %{
+      # Override default __init__()
+      def __init__(self, driver_or_url, location=None, options=None):
+          loc = str(location) if location else None
+          _dlite.Instance_swiginit(self, _dlite.new_Storage(
+              driver_or_url=driver_or_url, location=loc, options=options))
+
       def __enter__(self):
           return self
 


Test patch:
diff --git a/bindings/python/tests/test_collection.py b/bindings/python/tests/test_collection.py
index 9af499dd8..24b43fdb4 100755
--- a/bindings/python/tests/test_collection.py
+++ b/bindings/python/tests/test_collection.py
@@ -38,9 +38,9 @@
 assert not coll.has_id('non-existing-id')
 
 # Save
-with dlite.Storage('json', str(outdir / 'coll0.json'), 'mode=w') as s:
+with dlite.Storage('json', outdir / 'coll0.json', 'mode=w') as s:
     coll.save(s)
-coll.save('json', str(outdir / 'coll1.json'), 'mode=w')
+coll.save('json', outdir / 'coll1.json', 'mode=w')
 coll.save(f'json://{outdir}/coll2.json?mode=w')
 coll.save(f'json://{outdir}/coll3.json?mode=w', include_instances=False)
 data = []