Fixed BUFR, GRIB and HDF5 stub saving

This commit is contained in:
Andrew Murray 2022-02-19 14:29:03 +11:00
parent af345358e3
commit 3f5fad3a27
6 changed files with 99 additions and 3 deletions

View File

@ -45,3 +45,35 @@ def test_save(tmp_path):
# Act / Assert: stub cannot save without an implemented handler # Act / Assert: stub cannot save without an implemented handler
with pytest.raises(OSError): with pytest.raises(OSError):
im.save(tmpfile) im.save(tmpfile)
def test_handler(tmp_path):
class TestHandler:
opened = False
loaded = False
saved = False
def open(self, im):
self.opened = True
def load(self, im):
self.loaded = True
return Image.new("RGB", (1, 1))
def save(self, im, fp, filename):
self.saved = True
handler = TestHandler()
BufrStubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im:
assert handler.opened
assert not handler.loaded
im.load()
assert handler.loaded
temp_file = str(tmp_path / "temp.bufr")
im.save(temp_file)
assert handler.saved
BufrStubImagePlugin._handler = None

View File

@ -45,3 +45,35 @@ def test_save(tmp_path):
# Act / Assert: stub cannot save without an implemented handler # Act / Assert: stub cannot save without an implemented handler
with pytest.raises(OSError): with pytest.raises(OSError):
im.save(tmpfile) im.save(tmpfile)
def test_handler(tmp_path):
class TestHandler:
opened = False
loaded = False
saved = False
def open(self, im):
self.opened = True
def load(self, im):
self.loaded = True
return Image.new("RGB", (1, 1))
def save(self, im, fp, filename):
self.saved = True
handler = TestHandler()
GribStubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im:
assert handler.opened
assert not handler.loaded
im.load()
assert handler.loaded
temp_file = str(tmp_path / "temp.grib")
im.save(temp_file)
assert handler.saved
GribStubImagePlugin._handler = None

View File

@ -46,3 +46,35 @@ def test_save():
im.save(dummy_filename) im.save(dummy_filename)
with pytest.raises(OSError): with pytest.raises(OSError):
Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename) Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename)
def test_handler(tmp_path):
class TestHandler:
opened = False
loaded = False
saved = False
def open(self, im):
self.opened = True
def load(self, im):
self.loaded = True
return Image.new("RGB", (1, 1))
def save(self, im, fp, filename):
self.saved = True
handler = TestHandler()
Hdf5StubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im:
assert handler.opened
assert not handler.loaded
im.load()
assert handler.loaded
temp_file = str(tmp_path / "temp.h5")
im.save(temp_file)
assert handler.saved
Hdf5StubImagePlugin._handler = None

View File

@ -59,7 +59,7 @@ class BufrStubImageFile(ImageFile.StubImageFile):
def _save(im, fp, filename): def _save(im, fp, filename):
if _handler is None or not hasattr("_handler", "save"): if _handler is None or not hasattr(_handler, "save"):
raise OSError("BUFR save handler not installed") raise OSError("BUFR save handler not installed")
_handler.save(im, fp, filename) _handler.save(im, fp, filename)

View File

@ -59,7 +59,7 @@ class GribStubImageFile(ImageFile.StubImageFile):
def _save(im, fp, filename): def _save(im, fp, filename):
if _handler is None or not hasattr("_handler", "save"): if _handler is None or not hasattr(_handler, "save"):
raise OSError("GRIB save handler not installed") raise OSError("GRIB save handler not installed")
_handler.save(im, fp, filename) _handler.save(im, fp, filename)

View File

@ -59,7 +59,7 @@ class HDF5StubImageFile(ImageFile.StubImageFile):
def _save(im, fp, filename): def _save(im, fp, filename):
if _handler is None or not hasattr("_handler", "save"): if _handler is None or not hasattr(_handler, "save"):
raise OSError("HDF5 save handler not installed") raise OSError("HDF5 save handler not installed")
_handler.save(im, fp, filename) _handler.save(im, fp, filename)