Skip to content
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

Add unhide_file method to Bucket #505

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion b2sdk/_internal/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
from .exception import (
BucketIdNotFound,
CopySourceTooBig,
FileDeleted,
FileNotHidden,
FileNotPresent,
FileOrBucketNotFound,
UnexpectedCloudBehaviour,
UnexpectedFileVersionAction,
UnrecognizedBucketType,
)
from .file_lock import (
Expand All @@ -34,7 +37,7 @@
FileRetentionSetting,
LegalHold,
)
from .file_version import DownloadVersion, FileVersion
from .file_version import DownloadVersion, FileIdAndName, FileVersion
from .filter import Filter, FilterMatcher
from .http_constants import LIST_FILE_NAMES_MAX_LIMIT
from .progress import AbstractProgressListener, DoNothingProgressListener
Expand Down Expand Up @@ -1351,6 +1354,27 @@ def hide_file(self, file_name):
response = self.api.session.hide_file(self.id_, file_name)
return self.api.file_version_factory.from_api_response(response)

def unhide_file(self, file_name: str, bypass_governance: bool = False) -> FileIdAndName:
"""
Unhide a file by deleting the "hide marker".
"""

# get the latest file version
file_versions = self.list_file_versions(file_name=file_name, fetch_count=1)
latest_file_version = next(file_versions, None)
if latest_file_version is None:
raise FileNotPresent(bucket_name=self.name, file_id_or_name=file_name)

action = latest_file_version.action
if action == "upload":
raise FileNotHidden(file_name)
elif action == "delete":
raise FileDeleted(file_name)
elif action != "hide":
raise UnexpectedFileVersionAction(action)

return self.delete_file_version(latest_file_version.id_, file_name, bypass_governance)

def copy(
self,
file_id,
Expand Down
12 changes: 12 additions & 0 deletions b2sdk/_internal/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ class FileAlreadyHidden(B2SimpleError):
pass


class FileNotHidden(B2SimpleError):
prefix = 'File not hidden'


class FileDeleted(B2SimpleError):
prefix = 'File deleted'


class UnexpectedFileVersionAction(B2SimpleError):
prefix = 'Unexpected file version action returned by the server'


class FileNameNotAllowed(NotAllowedByAppKeyError):
pass

Expand Down
1 change: 1 addition & 0 deletions changelog.d/+bucket_unhide_file.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unhide_file method to Bucket.
8 changes: 8 additions & 0 deletions test/unit/bucket/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,14 @@ def test_hidden_file(self):
expected = [('hello.txt', 0, 'hide', None), ('hello.txt', 11, 'upload', None)]
self.assertBucketContents(expected, '', show_versions=True)

def test_unhidden_file(self):
data = b'hello world'
self.bucket.upload_bytes(data, 'hello.txt')
self.bucket.hide_file('hello.txt')
self.bucket.unhide_file('hello.txt')
expected = [('hello.txt', 11, 'upload', None)]
self.assertBucketContents(expected, '', show_versions=True)

def test_delete_file_version(self):
data = b'hello world'

Expand Down