Skip to content

Commit

Permalink
Improve error when reading invalid file
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmckinsey1 committed Nov 15, 2024
1 parent 1e58bc5 commit bda0ac3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
32 changes: 32 additions & 0 deletions thicket/tests/test_reader_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
# SPDX-License-Identifier: MIT

import os
import pytest

from hatchet import GraphFrame
Expand Down Expand Up @@ -88,3 +89,34 @@ def test_valid_inputs(rajaperf_cali_1trial, data_dir):
True,
f"{data_dir}/rajaperf/lassen/clang10.0.1_nvcc10.2.89_1048576/1/",
)


def test_error_file(mpi_scaling_cali, data_dir):

# Create a temporarily empty file
empty_file_path = os.path.join(f"{data_dir}/mpi_scaling_cali", "empty.cali")
with open(empty_file_path, "w") as temp_file:
pass # This creates an empty file

# list
with pytest.raises(Exception, match="Failed to read file"):
Thicket.reader_dispatch(
GraphFrame.from_caliperreader,
False,
True,
True,
mpi_scaling_cali + [empty_file_path],
)

# directory
with pytest.raises(Exception, match="Failed to read file"):
Thicket.reader_dispatch(
GraphFrame.from_caliperreader,
False,
True,
True,
f"{data_dir}/mpi_scaling_cali/",
)

# Remove the file
os.remove(empty_file_path)
18 changes: 10 additions & 8 deletions thicket/thicket.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,22 @@ def reader_dispatch(
pbar = tqdm.tqdm(obj, disable=disable_tqdm)
for file in pbar:
pbar.set_description(pbar_desc)
ens_list.append(
Thicket.thicketize_graphframe(
func(file, *extra_args, **kwargs), file
)
)
try:
gf = func(file, *extra_args, **kwargs)
except Exception as e:
raise Exception(f"Failed to read file: {file}") from e
ens_list.append(Thicket.thicketize_graphframe(gf, file))
# if directory of files
elif os.path.isdir(obj):
pbar = tqdm.tqdm(os.listdir(obj), disable=disable_tqdm)
for file in pbar:
pbar.set_description(pbar_desc)
f = os.path.join(obj, file)
ens_list.append(
Thicket.thicketize_graphframe(func(f, *extra_args, **kwargs), f)
)
try:
gf = func(f, *extra_args, **kwargs)
except Exception as e:
raise Exception(f"Failed to read file: {f}") from e
ens_list.append(Thicket.thicketize_graphframe(gf, f))
# if single file
elif os.path.isfile(obj):
return Thicket.thicketize_graphframe(func(*args, **kwargs), args[0])
Expand Down

0 comments on commit bda0ac3

Please sign in to comment.