-
Notifications
You must be signed in to change notification settings - Fork 62
add globbing support in makeReader and CreateDataSource
#729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6ff81e3
add globbing support in `Reader` and `DataSource`
m-fila 76f3d09
add docstrings, error handling, throwing on unsupported platforms
m-fila fe89fa3
exclude test from sanitizers since otherwise its dependencies are not…
m-fila 5a0c370
remove obsolete include
m-fila 3fef10b
add reading multiple files and glob patterns with `get_reader`
m-fila a4242a3
fix broken syntax in branch for no-glob platforms
m-fila 7a240c7
move glob to utils
m-fila add1917
Revert "add reading multiple files and glob patterns with `get_reader`"
m-fila 79a4876
test standalone glob utility
m-fila 7f25518
add reading multiple files in `get_reader`, glob utilities accessible in
m-fila cef13be
import whole root in utils
m-fila 75b64f4
fix typos, comments, loading glob header in python
m-fila b64a765
fix glob namespace in selection.xml
m-fila 44f4638
define PODIO_HAS_GLOB_SUPPOR
m-fila 33de362
Harmonize comment style
tmadlener File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #ifndef PODIO_UTILITIES_GLOB_H | ||
| #define PODIO_UTILITIES_GLOB_H | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| // Support for glob expansion. | ||
| #if __has_include(<glob.h>) | ||
| #define PODIO_HAS_GLOB_SUPPORT 1 | ||
| #else | ||
| #define PODIO_HAS_GLOB_SUPPORT 0 | ||
| #endif | ||
|
|
||
| namespace podio::utils { | ||
| /// @brief Expands a given glob pattern into a list of matching file paths. | ||
| /// | ||
| /// This function takes a glob pattern as input and returns a vector of strings | ||
| /// containing the paths that match the pattern. It supports standard glob rules | ||
| /// extended with tilde expansion and brace expansion. If the pattern doesn't | ||
| /// contain any wildcards then it is placed in the returned vector as is. Paths | ||
| /// that cannot be accessed are displayed on std::cerr, but the expansion process | ||
| /// is not aborted. On platforms without <glob.h> no expansion is done and vector | ||
| /// containing the original pattern is returned | ||
| /// | ||
| /// @param pattern The glob pattern to expand. | ||
| /// @return A vector of strings containing the matching file paths. | ||
| /// | ||
| /// @throws std::runtime_error If no matches are found or if there is an error | ||
| /// during glob expansion. | ||
| std::vector<std::string> expand_glob(const std::string& pattern); | ||
|
|
||
| /// @brief Checks if a given pattern is a glob pattern. | ||
| /// | ||
| /// This function determines whether the provided pattern contains any standard | ||
| /// glob or brace expansion wildcards. | ||
| /// | ||
| /// @param pattern The pattern to check. | ||
| /// @return true if the pattern is a glob pattern, false otherwise. | ||
| bool is_glob_pattern(const std::string& pattern); | ||
|
|
||
| } // namespace podio::utils | ||
|
|
||
| #endif // PODIO_UTILITIES_GLOB_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #include "podio/utilities/Glob.h" | ||
| #include <iostream> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <vector> | ||
| #if __has_include(<glob.h>) | ||
| #include <glob.h> | ||
| #else | ||
| #include <system_error> | ||
| #endif | ||
m-fila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| namespace podio::utils { | ||
|
|
||
| bool is_glob_pattern(const std::string& pattern) { | ||
| bool escape = false; | ||
| for (auto c : pattern) { | ||
| if (escape) { | ||
| escape = false; | ||
| } else if (c == '\\') { | ||
| escape = true; | ||
| } else if (c == '*' || c == '?' || c == '[' || c == '{') { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| #if __has_include(<glob.h>) | ||
|
|
||
| int glob_err_handler(const char* epath, int eerrno) { | ||
| std::cerr << "Glob expansion error accessing path: " << epath << " (error code: " << eerrno << ")\n"; | ||
| return 0; | ||
| } | ||
|
|
||
| std::vector<std::string> expand_glob(const std::string& pattern) { | ||
| glob_t glob_result; | ||
| auto retv = glob(pattern.c_str(), GLOB_TILDE | GLOB_BRACE | GLOB_NOMAGIC, glob_err_handler, &glob_result); | ||
| if (retv == GLOB_NOMATCH) { | ||
| throw std::runtime_error("Glob expansion found no matches for pattern: " + pattern); | ||
| } else if (retv != 0) { | ||
| globfree(&glob_result); | ||
| throw std::runtime_error("Glob expansion error"); | ||
| } | ||
| std::vector<std::string> results; | ||
| results.reserve(glob_result.gl_pathc); | ||
| for (size_t i = 0; i < glob_result.gl_pathc; ++i) { | ||
| results.emplace_back(glob_result.gl_pathv[i]); | ||
| } | ||
| globfree(&glob_result); | ||
| return results; | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| std::vector<std::string> expand_glob(const std::string& pattern) { | ||
| if (is_glob_pattern(pattern)) { | ||
| throw std::system_error("Glob expansion is not supported on this platform") | ||
| } | ||
| return {pattern}; | ||
| } | ||
|
|
||
| #endif // __has_include(<glob.h>) | ||
|
|
||
| } // namespace podio::utils | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include "podio/Reader.h" | ||
| #include "podio/utilities/Glob.h" | ||
| #if PODIO_ENABLE_DATASOURCE | ||
| #include "podio/DataSource.h" | ||
| #endif | ||
|
|
||
| #define ASSERT(condition, msg) \ | ||
| if (!(condition)) { \ | ||
| throw std::runtime_error(msg); \ | ||
| } | ||
|
|
||
| int main() { | ||
| const auto pattern = "example_frame_?.root"; | ||
| const auto expected_events = 20; | ||
| // standalone globbing | ||
|
|
||
| ASSERT(podio::utils::is_glob_pattern(pattern), "Failed to recognize glob pattern"); | ||
| auto files = podio::utils::expand_glob(pattern); | ||
| ASSERT(files.size() == 2, "Glob expanded to a wrong number of files"); | ||
| ASSERT(files[0] == "example_frame_0.root", "Glob expanded to an unexpected file"); | ||
| ASSERT(files[1] == "example_frame_1.root", "Glob expanded to an unexpected file"); | ||
| { | ||
| // externally resolved glob | ||
| const auto reader = podio::makeReader(files); | ||
| ASSERT((reader.getEvents() == expected_events), "Reader read invalid number of events"); | ||
| #if PODIO_ENABLE_DATASOURCE | ||
| auto rdf = podio::CreateDataFrame(files); | ||
| ASSERT(rdf.Count().GetValue() == expected_events, "DataSource read invalid number of events"); | ||
| #endif // PODIO_ENABLE_DATASOURCE | ||
| } | ||
| { | ||
| // implicit globbing | ||
| const auto reader = podio::makeReader(pattern); | ||
| ASSERT((reader.getEvents() == expected_events), "Reader read invalid number of events"); | ||
| #if PODIO_ENABLE_DATASOURCE | ||
| auto rdf = podio::CreateDataFrame(pattern); | ||
| ASSERT(rdf.Count().GetValue() == expected_events, "DataSource read invalid number of events"); | ||
| #endif // PODIO_ENABLE_DATASOURCE | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """Small test case for checking get_reader working with | ||
| a single file, list of files, and a glob pattern""" | ||
|
|
||
| import podio | ||
|
|
||
| assert podio.utils.is_glob_pattern("example_frame_?.root") | ||
| files = podio.utils.expand_glob("example_frame_?.root") | ||
| assert files == ["example_frame_0.root", "example_frame_1.root"] | ||
|
|
||
| reader = podio.reading.get_reader("example_frame.root") | ||
| assert len(reader.get("events")) == 10 | ||
| reader = podio.reading.get_reader(files) | ||
| assert len(reader.get("events")) == 20 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.