Merge pull request #6071 from radarhere/handler

Fixed BUFR, GRIB and HDF5 stub saving
This commit is contained in:
Hugo van Kemenade 2022-02-19 11:25:42 +02:00 committed by GitHub
commit b78e601b63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
with pytest.raises(OSError):
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
with pytest.raises(OSError):
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)
with pytest.raises(OSError):
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):
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")
_handler.save(im, fp, filename)

View File

@ -59,7 +59,7 @@ class GribStubImageFile(ImageFile.StubImageFile):
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")
_handler.save(im, fp, filename)

View File

@ -59,7 +59,7 @@ class HDF5StubImageFile(ImageFile.StubImageFile):
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")
_handler.save(im, fp, filename)