Skip to content

Commit

Permalink
Better handling for file extensions (#1473)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
franzpoeschel committed Jul 18, 2023
1 parent 198c3f9 commit 9365031
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
8 changes: 6 additions & 2 deletions include/openPMD/IO/AbstractIOHandlerHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace openPMD
* @param comm MPI communicator used for IO.
* @param options JSON-formatted option string, to be interpreted by
* the backend.
* @param pathAsItWasSpecifiedInTheConstructor For error messages.
* @tparam JSON Substitute for nlohmann::json. Templated to avoid
including nlohmann::json in a .hpp file.
* @return Smart pointer to created IOHandler.
Expand All @@ -51,7 +52,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler(
Format format,
std::string originalExtension,
MPI_Comm comm,
JSON options);
JSON options,
std::string const &pathAsItWasSpecifiedInTheConstructor);
#endif

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

// version without configuration to use in AuxiliaryTest
std::unique_ptr<AbstractIOHandler> createIOHandler(
Expand Down
22 changes: 16 additions & 6 deletions src/IO/AbstractIOHandlerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
std::string originalExtension,

MPI_Comm comm,
json::TracingJSON options)
json::TracingJSON options,
std::string const &pathAsItWasSpecifiedInTheConstructor)
{
(void)options;
switch (format)
Expand Down Expand Up @@ -124,9 +125,14 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
std::move(options),
"ssc",
std::move(originalExtension));
case Format::JSON:
throw error::WrongAPIUsage(
"JSON backend not available in parallel openPMD.");
default:
throw std::runtime_error(
"Unknown file format! Did you specify a file ending?");
throw error::WrongAPIUsage(
"Unknown file format! Did you specify a file ending? Specified "
"file name was '" +
pathAsItWasSpecifiedInTheConstructor + "'.");
}
}
#endif
Expand All @@ -137,7 +143,8 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
Access access,
Format format,
std::string originalExtension,
json::TracingJSON options)
json::TracingJSON options,
std::string const &pathAsItWasSpecifiedInTheConstructor)
{
(void)options;
switch (format)
Expand Down Expand Up @@ -190,7 +197,9 @@ std::unique_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
"JSON", path, access);
default:
throw std::runtime_error(
"Unknown file format! Did you specify a file ending?");
"Unknown file format! Did you specify a file ending? Specified "
"file name was '" +
pathAsItWasSpecifiedInTheConstructor + "'.");
}
}

Expand All @@ -205,6 +214,7 @@ std::unique_ptr<AbstractIOHandler> createIOHandler(
access,
format,
std::move(originalExtension),
json::TracingJSON(json::ParsedConfig{}));
json::TracingJSON(json::ParsedConfig{}),
"");
}
} // namespace openPMD
15 changes: 13 additions & 2 deletions src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ std::unique_ptr<Series::ParsedInput> Series::parseInput(std::string filepath)
filepath = auxiliary::replace_all(filepath, "\\", "/");
}
#endif
if (auxiliary::ends_with(filepath, auxiliary::directory_separator))
{
filepath = auxiliary::replace_last(
filepath, std::string(&auxiliary::directory_separator, 1), "");
}
auto const pos = filepath.find_last_of(auxiliary::directory_separator);
if (std::string::npos == pos)
{
Expand Down Expand Up @@ -2308,7 +2313,8 @@ Series::Series(
input->format,
input->filenameExtension,
comm,
optionsJson);
optionsJson,
filepath);
init(std::move(handler), std::move(input));
json::warnGlobalUnusedOptions(optionsJson);
}
Expand All @@ -2325,7 +2331,12 @@ Series::Series(
auto input = parseInput(filepath);
parseJsonOptions(optionsJson, *input);
auto handler = createIOHandler(
input->path, at, input->format, input->filenameExtension, optionsJson);
input->path,
at,
input->format,
input->filenameExtension,
optionsJson,
filepath);
init(std::move(handler), std::move(input));
json::warnGlobalUnusedOptions(optionsJson);
}
Expand Down
9 changes: 6 additions & 3 deletions test/CoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,13 +1035,16 @@ TEST_CASE("no_file_ending", "[core]")
{
REQUIRE_THROWS_WITH(
Series("./new_openpmd_output", Access::CREATE),
Catch::Equals("Unknown file format! Did you specify a file ending?"));
Catch::Equals("Unknown file format! Did you specify a file ending? "
"Specified file name was './new_openpmd_output'."));
REQUIRE_THROWS_WITH(
Series("./new_openpmd_output_%T", Access::CREATE),
Catch::Equals("Unknown file format! Did you specify a file ending?"));
Catch::Equals("Unknown file format! Did you specify a file ending? "
"Specified file name was './new_openpmd_output_%T'."));
REQUIRE_THROWS_WITH(
Series("./new_openpmd_output_%05T", Access::CREATE),
Catch::Equals("Unknown file format! Did you specify a file ending?"));
Catch::Equals("Unknown file format! Did you specify a file ending? "
"Specified file name was './new_openpmd_output_%05T'."));
{
Series(
"../samples/no_extension_specified",
Expand Down

0 comments on commit 9365031

Please sign in to comment.