-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fs: introduce fs.find() #5879
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
fs: introduce fs.find() #5879
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,15 +33,13 @@ def _strip_bucket(self, entry): | |
|
|
||
| return path or bucket | ||
|
|
||
| def _strip_buckets(self, entries, detail, prefix=None): | ||
| def _strip_buckets(self, entries, detail=False): | ||
| for entry in entries: | ||
| if detail: | ||
| entry = self._entry_hook(entry.copy()) | ||
| entry["name"] = self._strip_bucket(entry["name"]) | ||
| else: | ||
| entry = self._strip_bucket( | ||
| f"{prefix}/{entry}" if prefix else entry | ||
| ) | ||
| entry = self._strip_bucket(entry) | ||
| yield entry | ||
|
|
||
| def _entry_hook(self, entry): | ||
|
|
@@ -93,24 +91,29 @@ def copy(self, from_info, to_info): | |
| def exists(self, path_info, use_dvcignore=False): | ||
| return self.fs.exists(self._with_bucket(path_info)) | ||
|
|
||
| def ls( | ||
| self, path_info, detail=False, recursive=False | ||
| ): # pylint: disable=arguments-differ | ||
| if self.isdir(path_info) and self.is_empty(path_info): | ||
| return None | ||
| def ls(self, path_info, detail=False): | ||
| path = self._with_bucket(path_info) | ||
| files = self.fs.ls(path, detail=detail) | ||
| yield from self._strip_buckets(files, detail=detail) | ||
|
|
||
| def find(self, path_info, detail=False): | ||
| path = self._with_bucket(path_info) | ||
| if recursive: | ||
| for root, _, files in self.fs.walk(path, detail=detail): | ||
| if detail: | ||
| files = files.values() | ||
| yield from self._strip_buckets(files, detail, prefix=root) | ||
| files = self.fs.find(path, detail=detail) | ||
| if detail: | ||
| files = files.values() | ||
|
|
||
| # When calling find() on a file, it returns the same file in a list. | ||
| # For object-based storages, the same behavior applies to empty | ||
| # directories since they are represented as files. This condition | ||
| # checks whether we should yield an empty list (if it is an empty | ||
| # directory) or just yield the file itself. | ||
| if len(files) == 1 and files[0] == path and self.isdir(path_info): | ||
| return None | ||
|
|
||
| yield from self._strip_buckets(self.ls(path, detail=detail), detail) | ||
| yield from self._strip_buckets(files, detail=detail) | ||
|
|
||
| def walk_files(self, path_info, **kwargs): | ||
| for file in self.ls(path_info, recursive=True): | ||
| for file in self.find(path_info): | ||
| yield path_info.replace(path=file) | ||
|
Comment on lines
115
to
117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like getting rid of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, |
||
|
|
||
| def remove(self, path_info): | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would calling full
isdirbe wasteful? Or does fsspec caching compensate for that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, not really. We only call the
isdir()if it is a file and see whether the file is actually a file or just a directory. So unless we start callingwalk_files()on normal files, then this would cost nothing.The last check will simply ensure that the this is indeed a directory, and would only cost stuff when
import-urlin empty directories etc. Which I guess shouldn't be much of a big deal.