Skip to content

Commit 9365031

Browse files
Better handling for file extensions (#1473)
* Deal with trailing slashes in file paths Happens in bash completion on BP files since they are folders. * Throw better error messages when selecting a bad backend * Adapt CoreTest to new error message
1 parent 198c3f9 commit 9365031

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

include/openPMD/IO/AbstractIOHandlerHelper.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace openPMD
4040
* @param comm MPI communicator used for IO.
4141
* @param options JSON-formatted option string, to be interpreted by
4242
* the backend.
43+
* @param pathAsItWasSpecifiedInTheConstructor For error messages.
4344
* @tparam JSON Substitute for nlohmann::json. Templated to avoid
4445
including nlohmann::json in a .hpp file.
4546
* @return Smart pointer to created IOHandler.
@@ -51,7 +52,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler(
5152
Format format,
5253
std::string originalExtension,
5354
MPI_Comm comm,
54-
JSON options);
55+
JSON options,
56+
std::string const &pathAsItWasSpecifiedInTheConstructor);
5557
#endif
5658

5759
/** Construct an appropriate specific IOHandler for the desired IO mode.
@@ -65,6 +67,7 @@ std::unique_ptr<AbstractIOHandler> createIOHandler(
6567
* specified by the user.
6668
* @param options JSON-formatted option string, to be interpreted by
6769
* the backend.
70+
* @param pathAsItWasSpecifiedInTheConstructor For error messages.
6871
* @tparam JSON Substitute for nlohmann::json. Templated to avoid
6972
including nlohmann::json in a .hpp file.
7073
* @return Smart pointer to created IOHandler.
@@ -75,7 +78,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler(
7578
Access access,
7679
Format format,
7780
std::string originalExtension,
78-
JSON options = JSON());
81+
JSON options,
82+
std::string const &pathAsItWasSpecifiedInTheConstructor);
7983

8084
// version without configuration to use in AuxiliaryTest
8185
std::unique_ptr<AbstractIOHandler> createIOHandler(

src/IO/AbstractIOHandlerHelper.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
7171
std::string originalExtension,
7272

7373
MPI_Comm comm,
74-
json::TracingJSON options)
74+
json::TracingJSON options,
75+
std::string const &pathAsItWasSpecifiedInTheConstructor)
7576
{
7677
(void)options;
7778
switch (format)
@@ -124,9 +125,14 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
124125
std::move(options),
125126
"ssc",
126127
std::move(originalExtension));
128+
case Format::JSON:
129+
throw error::WrongAPIUsage(
130+
"JSON backend not available in parallel openPMD.");
127131
default:
128-
throw std::runtime_error(
129-
"Unknown file format! Did you specify a file ending?");
132+
throw error::WrongAPIUsage(
133+
"Unknown file format! Did you specify a file ending? Specified "
134+
"file name was '" +
135+
pathAsItWasSpecifiedInTheConstructor + "'.");
130136
}
131137
}
132138
#endif
@@ -137,7 +143,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
137143
Access access,
138144
Format format,
139145
std::string originalExtension,
140-
json::TracingJSON options)
146+
json::TracingJSON options,
147+
std::string const &pathAsItWasSpecifiedInTheConstructor)
141148
{
142149
(void)options;
143150
switch (format)
@@ -190,7 +197,9 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
190197
"JSON", path, access);
191198
default:
192199
throw std::runtime_error(
193-
"Unknown file format! Did you specify a file ending?");
200+
"Unknown file format! Did you specify a file ending? Specified "
201+
"file name was '" +
202+
pathAsItWasSpecifiedInTheConstructor + "'.");
194203
}
195204
}
196205

@@ -205,6 +214,7 @@ std::unique_ptr<AbstractIOHandler> createIOHandler(
205214
access,
206215
format,
207216
std::move(originalExtension),
208-
json::TracingJSON(json::ParsedConfig{}));
217+
json::TracingJSON(json::ParsedConfig{}),
218+
"");
209219
}
210220
} // namespace openPMD

src/Series.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ std::unique_ptr<Series::ParsedInput> Series::parseInput(std::string filepath)
424424
filepath = auxiliary::replace_all(filepath, "\\", "/");
425425
}
426426
#endif
427+
if (auxiliary::ends_with(filepath, auxiliary::directory_separator))
428+
{
429+
filepath = auxiliary::replace_last(
430+
filepath, std::string(&auxiliary::directory_separator, 1), "");
431+
}
427432
auto const pos = filepath.find_last_of(auxiliary::directory_separator);
428433
if (std::string::npos == pos)
429434
{
@@ -2308,7 +2313,8 @@ Series::Series(
23082313
input->format,
23092314
input->filenameExtension,
23102315
comm,
2311-
optionsJson);
2316+
optionsJson,
2317+
filepath);
23122318
init(std::move(handler), std::move(input));
23132319
json::warnGlobalUnusedOptions(optionsJson);
23142320
}
@@ -2325,7 +2331,12 @@ Series::Series(
23252331
auto input = parseInput(filepath);
23262332
parseJsonOptions(optionsJson, *input);
23272333
auto handler = createIOHandler(
2328-
input->path, at, input->format, input->filenameExtension, optionsJson);
2334+
input->path,
2335+
at,
2336+
input->format,
2337+
input->filenameExtension,
2338+
optionsJson,
2339+
filepath);
23292340
init(std::move(handler), std::move(input));
23302341
json::warnGlobalUnusedOptions(optionsJson);
23312342
}

test/CoreTest.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,13 +1035,16 @@ TEST_CASE("no_file_ending", "[core]")
10351035
{
10361036
REQUIRE_THROWS_WITH(
10371037
Series("./new_openpmd_output", Access::CREATE),
1038-
Catch::Equals("Unknown file format! Did you specify a file ending?"));
1038+
Catch::Equals("Unknown file format! Did you specify a file ending? "
1039+
"Specified file name was './new_openpmd_output'."));
10391040
REQUIRE_THROWS_WITH(
10401041
Series("./new_openpmd_output_%T", Access::CREATE),
1041-
Catch::Equals("Unknown file format! Did you specify a file ending?"));
1042+
Catch::Equals("Unknown file format! Did you specify a file ending? "
1043+
"Specified file name was './new_openpmd_output_%T'."));
10421044
REQUIRE_THROWS_WITH(
10431045
Series("./new_openpmd_output_%05T", Access::CREATE),
1044-
Catch::Equals("Unknown file format! Did you specify a file ending?"));
1046+
Catch::Equals("Unknown file format! Did you specify a file ending? "
1047+
"Specified file name was './new_openpmd_output_%05T'."));
10451048
{
10461049
Series(
10471050
"../samples/no_extension_specified",

0 commit comments

Comments
 (0)