Skip to content

Commit

Permalink
Fixed BUFR, GRIB and HDF5 stub saving
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 19, 2022
1 parent af34535 commit 20acd02
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
37 changes: 37 additions & 0 deletions Tests/test_file_bufrstub.py
Expand Up @@ -45,3 +45,40 @@ 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 Handler:
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 = Handler()
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
Image.register_open(
BufrStubImagePlugin.BufrStubImageFile.format,
BufrStubImagePlugin.BufrStubImageFile,
BufrStubImagePlugin._accept,
)
37 changes: 37 additions & 0 deletions Tests/test_file_gribstub.py
Expand Up @@ -45,3 +45,40 @@ 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 Handler:
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 = Handler()
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
Image.register_open(
GribStubImagePlugin.GribStubImageFile.format,
GribStubImagePlugin.GribStubImageFile,
GribStubImagePlugin._accept,
)
37 changes: 37 additions & 0 deletions Tests/test_file_hdf5stub.py
Expand Up @@ -46,3 +46,40 @@ def test_save():
im.save(dummy_filename)
with pytest.raises(OSError):
Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename)


def test_handler(tmp_path):
class Handler:
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 = Handler()
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
Image.register_open(
Hdf5StubImagePlugin.HDF5StubImageFile.format,
Hdf5StubImagePlugin.HDF5StubImageFile,
Hdf5StubImagePlugin._accept,
)
2 changes: 1 addition & 1 deletion src/PIL/BufrStubImagePlugin.py
Expand Up @@ -59,7 +59,7 @@ def _load(self):


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)

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GribStubImagePlugin.py
Expand Up @@ -59,7 +59,7 @@ def _load(self):


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)

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Hdf5StubImagePlugin.py
Expand Up @@ -59,7 +59,7 @@ def _load(self):


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)

Expand Down

0 comments on commit 20acd02

Please sign in to comment.