forked from Backblaze/B2_Command_Line_Tool
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Backblaze#959 from reef-technologies/b2_uri_cmds
B2 URI commands
- Loading branch information
Showing
26 changed files
with
843 additions
and
263 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
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,44 @@ | ||
###################################################################### | ||
# | ||
# File: b2/_cli/b2args.py | ||
# | ||
# Copyright 2023 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
""" | ||
Utility functions for adding b2-specific arguments to an argparse parser. | ||
""" | ||
import argparse | ||
|
||
from b2._cli.argcompleters import b2uri_file_completer | ||
from b2._utils.uri import B2URI, B2URIBase, parse_b2_uri | ||
from b2.arg_parser import wrap_with_argument_type_error | ||
|
||
|
||
def b2_file_uri(value: str) -> B2URIBase: | ||
b2_uri = parse_b2_uri(value) | ||
if isinstance(b2_uri, B2URI): | ||
if b2_uri.is_dir(): | ||
raise ValueError( | ||
f"B2 URI pointing to a file-like object is required, but {value} was provided" | ||
) | ||
return b2_uri | ||
|
||
return b2_uri | ||
|
||
|
||
B2_URI_ARG_TYPE = wrap_with_argument_type_error(parse_b2_uri) | ||
B2_URI_FILE_ARG_TYPE = wrap_with_argument_type_error(b2_file_uri) | ||
|
||
|
||
def add_b2_file_argument(parser: argparse.ArgumentParser, name="B2_URI"): | ||
""" | ||
Add a B2 URI pointing to a file as an argument to the parser. | ||
""" | ||
parser.add_argument( | ||
name, | ||
type=B2_URI_FILE_ARG_TYPE, | ||
help="B2 URI pointing to a file, e.g. b2://yourBucket/file.txt or b2id://fileId", | ||
).completer = b2uri_file_completer |
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,49 @@ | ||
###################################################################### | ||
# | ||
# File: b2/_utils/python_compat.py | ||
# | ||
# Copyright 2023 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
""" | ||
Utilities for compatibility with older Python versions. | ||
""" | ||
import functools | ||
import sys | ||
|
||
if sys.version_info < (3, 9): | ||
|
||
def removeprefix(s: str, prefix: str) -> str: | ||
return s[len(prefix):] if s.startswith(prefix) else s | ||
|
||
else: | ||
removeprefix = str.removeprefix | ||
|
||
if sys.version_info < (3, 8): | ||
|
||
class singledispatchmethod: | ||
""" | ||
singledispatchmethod backport for Python 3.7. | ||
There are no guarantees for its completeness. | ||
""" | ||
|
||
def __init__(self, method): | ||
self.dispatcher = functools.singledispatch(method) | ||
self.method = method | ||
|
||
def register(self, cls, method=None): | ||
return self.dispatcher.register(cls, func=method) | ||
|
||
def __get__(self, obj, cls): | ||
@functools.wraps(self.method) | ||
def method_wrapper(arg, *args, **kwargs): | ||
method_desc = self.dispatcher.dispatch(arg.__class__) | ||
return method_desc.__get__(obj, cls)(arg, *args, **kwargs) | ||
|
||
method_wrapper.register = self.register | ||
return method_wrapper | ||
else: | ||
singledispatchmethod = functools.singledispatchmethod |
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
Oops, something went wrong.