-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
cat
command and basic B2 URI support
- Loading branch information
1 parent
9b8737b
commit f11821e
Showing
8 changed files
with
262 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
import pathlib | ||
import urllib | ||
from pathlib import Path | ||
|
||
|
||
class B2URIBase: | ||
pass | ||
|
||
|
||
@dataclasses.dataclass | ||
class B2URI(B2URIBase): | ||
bucket: str | ||
path: str | ||
|
||
def __str__(self) -> str: | ||
return f"b2://{self.bucket}{self.path}" | ||
|
||
def is_dir(self) -> bool: | ||
""" | ||
Return if the path is a directory. | ||
Please note this is symbolical. | ||
It is possible for file to have a trailing slash, but it is HIGHLY discouraged, and not supported by B2 CLI. | ||
:return: True if the path is a file, False if it's a directory | ||
""" | ||
return self.path.endswith("/") | ||
|
||
|
||
@dataclasses.dataclass | ||
class B2FileIdURI(B2URIBase): | ||
file_id: str | ||
|
||
def __str__(self) -> str: | ||
return f"b2id://{self.file_id}" | ||
|
||
|
||
def parse_uri(uri: str) -> Path | B2URI | B2FileIdURI: | ||
parsed = urllib.parse.urlparse(uri) | ||
if parsed.scheme == "": | ||
return pathlib.Path(uri) | ||
return _parse_b2_uri(uri, parsed) | ||
|
||
|
||
def parse_b2_uri(uri: str) -> B2URI | B2FileIdURI: | ||
parsed = urllib.parse.urlparse(uri) | ||
return _parse_b2_uri(uri, parsed) | ||
|
||
|
||
def _parse_b2_uri(uri, parsed: urllib.parse.ParseResult) -> B2URI | B2FileIdURI: | ||
if parsed.scheme in ("b2", "b2id"): | ||
if not parsed.netloc: | ||
raise ValueError(f"Invalid B2 URI: {uri!r}") | ||
elif parsed.password or parsed.username: | ||
raise ValueError( | ||
"Invalid B2 URI: credentials passed using `user@password:` syntax are not supported in URI" | ||
) | ||
|
||
if parsed.scheme == "b2": | ||
return B2URI(bucket=parsed.netloc, path=parsed.path[1:]) | ||
elif parsed.scheme == "b2id": | ||
return B2FileIdURI(file_id=parsed.netloc) | ||
else: | ||
raise ValueError(f"Unsupported URI scheme: {parsed.scheme!r}") |
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
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,63 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from b2._utils.uri import B2URI, B2FileIdURI, parse_uri | ||
|
||
|
||
def test_b2pathuri_str(): | ||
uri = B2URI(bucket="testbucket", path="/path/to/file") | ||
assert str(uri) == "b2://testbucket/path/to/file" | ||
|
||
|
||
def test_b2pathuri_is_dir_true(): | ||
uri = B2URI(bucket="testbucket", path="/path/to/directory/") | ||
assert uri.is_dir() is True | ||
|
||
|
||
def test_b2pathuri_is_dir_false(): | ||
uri = B2URI(bucket="testbucket", path="/path/to/file") | ||
assert uri.is_dir() is False | ||
|
||
|
||
def test_b2fileuri_str(): | ||
uri = B2FileIdURI(file_id="file123") | ||
assert str(uri) == "b2id://file123" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"uri,expected", | ||
[ | ||
("some/local/path", Path("some/local/path")), | ||
("./some/local/path", Path("some/local/path")), | ||
("b2://bucket/path/to/dir/", B2URI(bucket="bucket", path="path/to/dir/")), | ||
("b2id://file123", B2FileIdURI(file_id="file123")), | ||
], | ||
) | ||
def test_parse_uri(uri, expected): | ||
assert parse_uri(uri) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"uri, expected_exception_message", | ||
[ | ||
# Test cases for invalid B2 URIs (missing netloc part) | ||
("b2://", "Invalid B2 URI: 'b2://'"), | ||
("b2id://", "Invalid B2 URI: 'b2id://'"), | ||
# Test cases for B2 URIs with credentials | ||
( | ||
"b2://user@password:bucket/path", | ||
"Invalid B2 URI: credentials passed using `user@password:` syntax are not supported in URI", | ||
), | ||
( | ||
"b2id://user@password:file123", | ||
"Invalid B2 URI: credentials passed using `user@password:` syntax are not supported in URI", | ||
), | ||
# Test cases for unsupported URI schemes | ||
("unknown://bucket/path", "Unsupported URI scheme: 'unknown'"), | ||
], | ||
) | ||
def test_parse_uri_exceptions(uri, expected_exception_message): | ||
with pytest.raises(ValueError) as exc_info: | ||
parse_uri(uri) | ||
assert expected_exception_message in str(exc_info.value) |
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