@@ -702,20 +702,12 @@ def read_from_directory(self, dataset_info_dir: epath.PathLike) -> None:
702
702
be the root directory of a specific dataset version.
703
703
704
704
Raises:
705
- FileNotFoundError : If the dataset_info.json can't be found .
705
+ DatasetInfoFileError : If the dataset_info.json can't be read .
706
706
"""
707
707
logging .info ("Load dataset info from %s" , dataset_info_dir )
708
708
709
709
# Load the metadata from disk
710
- try :
711
- parsed_proto = read_from_json (dataset_info_path (dataset_info_dir ))
712
- except Exception as e :
713
- raise FileNotFoundError (
714
- "Tried to load `DatasetInfo` from a directory which does not exist or"
715
- " does not contain `dataset_info.json`. Please delete the directory "
716
- f"`{ dataset_info_dir } ` if you are trying to re-generate the "
717
- "dataset."
718
- ) from e
710
+ parsed_proto = read_from_json (dataset_info_path (dataset_info_dir ))
719
711
720
712
if str (self .version ) != parsed_proto .version :
721
713
raise AssertionError (
@@ -1128,12 +1120,14 @@ def read_from_json(path: epath.PathLike) -> dataset_info_pb2.DatasetInfo:
1128
1120
the DatasetInfo proto.
1129
1121
1130
1122
Raises:
1131
- FileNotFoundError : If the builder_dir does not exist .
1123
+ DatasetInfoFileError : If the dataset info file cannot be read .
1132
1124
"""
1133
1125
try :
1134
1126
json_str = epath .Path (path ).read_text ()
1135
1127
except OSError as e :
1136
- raise FileNotFoundError (f"Could not load dataset info from { path } " ) from e
1128
+ raise DatasetInfoFileError (
1129
+ f"Could not read dataset info from { path } "
1130
+ ) from e
1137
1131
# Parse it back into a proto.
1138
1132
parsed_proto = json_format .Parse (json_str , dataset_info_pb2 .DatasetInfo ())
1139
1133
return parsed_proto
@@ -1151,7 +1145,7 @@ def read_proto_from_builder_dir(
1151
1145
The DatasetInfo proto as read from the builder dir.
1152
1146
1153
1147
Raises:
1154
- FileNotFoundError : If the builder_dir does not exist .
1148
+ DatasetInfoFileError : If the dataset info file cannot be read .
1155
1149
"""
1156
1150
builder_dir = epath .Path (builder_dir ).expanduser ()
1157
1151
info_path = builder_dir / constants .DATASET_INFO_FILENAME
@@ -1173,8 +1167,7 @@ def read_full_proto_from_builder_dir(
1173
1167
dir.
1174
1168
1175
1169
Raises:
1176
- FileNotFoundError: If the builder_dir does not exist or it doesn't contain
1177
- dataset_info.json.
1170
+ DatasetInfoFileError: If the dataset info file cannot be read.
1178
1171
"""
1179
1172
builder_dir = epath .Path (builder_dir ).expanduser ()
1180
1173
info_path = builder_dir / constants .DATASET_INFO_FILENAME
@@ -1513,3 +1506,12 @@ def save_metadata(self, data_dir):
1513
1506
self [key ] = json .load (f )
1514
1507
self ._tempdir .rmtree ()
1515
1508
super (BeamMetadataDict , self ).save_metadata (data_dir )
1509
+
1510
+
1511
+ class DatasetInfoFileError (OSError ):
1512
+ """Raised when the dataset info file cannot be read.
1513
+
1514
+ We use a custom exception rather than native exceptions, because different
1515
+ backend of etils.epath will throw different exceptions. This exception
1516
+ catches them all.
1517
+ """
0 commit comments