-
Notifications
You must be signed in to change notification settings - Fork 0
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
Core: Implement glob api #62
Merged
+315
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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,130 @@ | ||
# ByteDance Volcengine EMR, Copyright 2024. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""The module contains utility functions copied from fsspec package.""" | ||
|
||
import os | ||
import re | ||
|
||
|
||
def _translate(pat, STAR, QUESTION_MARK): | ||
# Copied from: https://github.com/python/cpython/pull/106703. | ||
res: list[str] = [] | ||
add = res.append | ||
i, n = 0, len(pat) | ||
while i < n: | ||
c = pat[i] | ||
i = i + 1 | ||
if c == "*": | ||
# compress consecutive `*` into one | ||
if (not res) or res[-1] is not STAR: | ||
add(STAR) | ||
elif c == "?": | ||
add(QUESTION_MARK) | ||
elif c == "[": | ||
j = i | ||
if j < n and pat[j] == "!": | ||
j = j + 1 | ||
if j < n and pat[j] == "]": | ||
j = j + 1 | ||
while j < n and pat[j] != "]": | ||
j = j + 1 | ||
if j >= n: | ||
add("\\[") | ||
else: | ||
stuff = pat[i:j] | ||
if "-" not in stuff: | ||
stuff = stuff.replace("\\", r"\\") | ||
else: | ||
chunks = [] | ||
k = i + 2 if pat[i] == "!" else i + 1 | ||
while True: | ||
k = pat.find("-", k, j) | ||
if k < 0: | ||
break | ||
chunks.append(pat[i:k]) | ||
i = k + 1 | ||
k = k + 3 | ||
chunk = pat[i:j] | ||
if chunk: | ||
chunks.append(chunk) | ||
else: | ||
chunks[-1] += "-" | ||
# Remove empty ranges -- invalid in RE. | ||
for k in range(len(chunks) - 1, 0, -1): | ||
if chunks[k - 1][-1] > chunks[k][0]: | ||
chunks[k - 1] = chunks[k - 1][:-1] + chunks[k][1:] | ||
del chunks[k] | ||
# Escape backslashes and hyphens for set difference (--). | ||
# Hyphens that create ranges shouldn't be escaped. | ||
stuff = "-".join( | ||
s.replace("\\", r"\\").replace("-", r"\-") for s in chunks | ||
) | ||
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. could you please add some explanation about these code? |
||
# Escape set operations (&&, ~~ and ||). | ||
stuff = re.sub(r"([&~|])", r"\\\1", stuff) | ||
i = j + 1 | ||
if not stuff: | ||
# Empty range: never match. | ||
add("(?!)") | ||
elif stuff == "!": | ||
# Negated empty range: match any character. | ||
add(".") | ||
else: | ||
if stuff[0] == "!": | ||
stuff = "^" + stuff[1:] | ||
elif stuff[0] in ("^", "["): | ||
stuff = "\\" + stuff | ||
add(f"[{stuff}]") | ||
else: | ||
add(re.escape(c)) | ||
assert i == n | ||
return res | ||
|
||
|
||
def glob_translate(pat: str): | ||
# Copied from: https://github.com/python/cpython/pull/106703. | ||
# The keyword parameters' values are fixed to: | ||
# recursive=True, include_hidden=True, seps=None | ||
"""Translate a pathname with shell wildcards to a regular expression.""" | ||
if os.path.altsep: | ||
seps = os.path.sep + os.path.altsep | ||
else: | ||
seps = os.path.sep | ||
escaped_seps = "".join(map(re.escape, seps)) | ||
any_sep = f"[{escaped_seps}]" if len(seps) > 1 else escaped_seps | ||
not_sep = f"[^{escaped_seps}]" | ||
one_last_segment = f"{not_sep}+" | ||
one_segment = f"{one_last_segment}{any_sep}" | ||
any_segments = f"(?:.+{any_sep})?" | ||
any_last_segments = ".*" | ||
results = [] | ||
parts = re.split(any_sep, pat) | ||
last_part_idx = len(parts) - 1 | ||
for idx, part in enumerate(parts): | ||
if part == "*": | ||
results.append(one_segment if idx < last_part_idx else one_last_segment) | ||
continue | ||
if part == "**": | ||
results.append(any_segments if idx < last_part_idx else any_last_segments) | ||
continue | ||
elif "**" in part: | ||
raise ValueError( | ||
"Invalid pattern: '**' can only be an entire path component" | ||
) | ||
if part: | ||
results.extend(_translate(part, f"{not_sep}*", not_sep)) | ||
if idx < last_part_idx: | ||
results.append(any_sep) | ||
res = "".join(results) | ||
return rf"(?s:{res})\Z" |
This file contains 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 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.
@xianyinxin I added a little description here, these code snippets are copied from fsspec's repo and utils.py. These codes do not exist in old fsspec versions(e.g.
2023.5.0
).