Skip to content

Commit

Permalink
BUG: fix check_folder to not raise exception for mem. streams
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrivenaes committed Oct 11, 2024
1 parent 2a144ec commit 96fb83a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/xtgeo/grid3d/_gridprop_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def to_file(
name = gridprop.name or ""

xtg_file = FileWrapper(pfile, mode="rb")
if not xtg_file.memstream:
xtg_file.check_folder(raiseerror=OSError)
xtg_file.check_folder(raiseerror=OSError)

if fformat in FileFormat.ROFF_BINARY.value + FileFormat.ROFF_ASCII.value:
_export_roff(gridprop, xtg_file.name, name, binary, append=append)
Expand Down
3 changes: 1 addition & 2 deletions src/xtgeo/grid3d/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,7 @@ def to_file(self, gfile: FileLike, fformat: str = "roff") -> None:
"""
_gfile = FileWrapper(gfile, mode="wb")

if not _gfile.memstream:
_gfile.check_folder(raiseerror=OSError)
_gfile.check_folder(raiseerror=OSError)

if fformat in FileFormat.ROFF_BINARY.value:
_grid_export.export_roff(self, _gfile.name, "binary")
Expand Down
10 changes: 6 additions & 4 deletions src/xtgeo/io/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,14 @@ def check_folder(
ValueError: If the file is a memstream
"""
# Redundant but mypy can't follow when memstream is True
if self.memstream or isinstance(self.file, (io.BytesIO, io.StringIO)):
raise ValueError("Cannot check folder status of an in-memory file")

logger.debug("Checking folder...")

if self.memstream or isinstance(self.file, (io.BytesIO, io.StringIO)):
logger.info(
"Cannot check folder status of an in-memory file, just return True"
)
return True

folder = self.file.parent
if raisetext is None:
raisetext = f"Folder {folder.name} does not exist or cannot be accessed"
Expand Down
8 changes: 3 additions & 5 deletions src/xtgeo/surface/regular_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,9 @@ def to_file(
warnings.warn(msg, UserWarning)

mfile = FileWrapper(mfile, mode="wb", obj=self)
mfile.check_folder(raiseerror=OSError)

if not mfile.memstream:
mfile.check_folder(raiseerror=OSError)
else:
if mfile.memstream:
engine = "python"

if fformat in FileFormat.IRAP_ASCII.value:
Expand Down Expand Up @@ -1076,8 +1075,7 @@ def to_hdf(
# developing, in prep and experimental!
mfile = FileWrapper(mfile, mode="wb", obj=self)

if not mfile.memstream:
mfile.check_folder(raiseerror=OSError)
mfile.check_folder(raiseerror=OSError)

_regsurf_export.export_hdf5_regsurf(self, mfile, compression=compression)
return mfile.file
Expand Down

0 comments on commit 96fb83a

Please sign in to comment.