Skip to content

Commit

Permalink
Merge pull request Backblaze#988 from emnoor-reef/ls-rm-uri
Browse files Browse the repository at this point in the history
Implement `b2://` URI in `ls` & `rm` in v4
  • Loading branch information
mjurbanski-reef authored Feb 2, 2024
2 parents f0482ca + c38ccc9 commit 8122bb6
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 159 deletions.
25 changes: 24 additions & 1 deletion b2/_internal/_cli/b2args.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,33 @@ def b2_file_uri(value: str) -> B2URIBase:
return b2_uri


B2_URI_ARG_TYPE = wrap_with_argument_type_error(parse_b2_uri)
def b2_uri(value: str) -> B2URI:
uri = parse_b2_uri(value)
if not isinstance(uri, B2URI):
raise ValueError(
f"B2 URI of the form b2://bucket/path/ is required, but {value} was provided"
)
return uri


B2_URI_ARG_TYPE = wrap_with_argument_type_error(b2_uri)
B2_URI_FILE_ARG_TYPE = wrap_with_argument_type_error(b2_file_uri)


def add_b2_uri_argument(parser: argparse.ArgumentParser, name="B2_URI"):
"""
Add a B2 URI pointing to a bucket, optionally with a directory
or a pattern as an argument to the parser.
"""
parser.add_argument(
name,
type=B2_URI_ARG_TYPE,
help="B2 URI pointing to a bucket, directory or a pattern, "
"e.g. b2://yourBucket, b2://yourBucket/file.txt, b2://yourBucket/folder/, "
"b2://yourBucket/*.txt or b2id://fileId",
)


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.
Expand Down
41 changes: 41 additions & 0 deletions b2/_internal/b2v3/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,47 @@

# ruff: noqa: F405
from b2._internal._b2v4.registry import * # noqa
from .rm import Rm


class Ls(B2URIBucketNFolderNameArgMixin, BaseLs):
"""
{BASELS}
Examples
.. note::
Note the use of quotes, to ensure that special
characters are not expanded by the shell.
List csv and tsv files (in any directory, in the whole bucket):
.. code-block::
{NAME} ls --recursive --withWildcard bucketName "*.[ct]sv"
List all info.txt files from buckets bX, where X is any character:
.. code-block::
{NAME} ls --recursive --withWildcard bucketName "b?/info.txt"
List all pdf files from buckets b0 to b9 (including sub-directories):
.. code-block::
{NAME} ls --recursive --withWildcard bucketName "b[0-9]/*.pdf"
Requires capability:
- **listFiles**
"""


B2.register_subcommand(AuthorizeAccount)
B2.register_subcommand(CancelAllUnfinishedLargeFiles)
Expand Down
59 changes: 59 additions & 0 deletions b2/_internal/b2v3/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
######################################################################
#
# File: b2/_internal/b2v3/rm.py
#
# Copyright 2023 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

from b2._internal._b2v4.registry import B2URIBucketNFolderNameArgMixin, BaseRm


# NOTE: We need to keep v3 Rm in separate file, because we need to import it in
# unit tests without registering any commands.
class Rm(B2URIBucketNFolderNameArgMixin, BaseRm):
"""
{BASERM}
Examples.
.. note::
Note the use of quotes, to ensure that special
characters are not expanded by the shell.
.. note::
Use with caution. Running examples presented below can cause data-loss.
Remove all csv and tsv files (in any directory, in the whole bucket):
.. code-block::
{NAME} rm --recursive --withWildcard bucketName "*.[ct]sv"
Remove all info.txt files from buckets bX, where X is any character:
.. code-block::
{NAME} rm --recursive --withWildcard bucketName "b?/info.txt"
Remove all pdf files from buckets b0 to b9 (including sub-directories):
.. code-block::
{NAME} rm --recursive --withWildcard bucketName "b[0-9]/*.pdf"
Requires capability:
- **listFiles**
- **deleteFiles**
"""
Loading

0 comments on commit 8122bb6

Please sign in to comment.