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

Fix #161: Make 'Bucket.delete_keys' take an 'on_error' callback #312

Merged
merged 3 commits into from
Nov 3, 2014
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
17 changes: 13 additions & 4 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,26 @@ def delete_key(self, key):
self.connection.api_request(method='DELETE', path=key.path)
return key

def delete_keys(self, keys):
def delete_keys(self, keys, on_error=None):
"""Deletes a list of keys from the current bucket.

Uses :func:`Bucket.delete_key` to delete each individual key.

:type keys: list of string or :class:`gcloud.storage.key.Key`
:param key: A list of key names or Key objects to delete.
:param keys: A list of key names or Key objects to delete.

:type on_error: a callable taking (key)
:param on_error: If not ``None``, called once for each key which
raises a ``NotFoundError``.
"""
# NOTE: boto returns a MultiDeleteResult instance.
for key in keys:
self.delete_key(key)
try:

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

self.delete_key(key)
except exceptions.NotFoundError:
if on_error is not None:
on_error(key)

This comment was marked as spam.

This comment was marked as spam.

else:
raise

def copy_key(self, key, destination_bucket, new_name=None):
"""Copy the given key to the given bucket, optionally with a new name.
Expand Down
18 changes: 17 additions & 1 deletion gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_delete_keys_hit(self):
self.assertEqual(kw[0]['method'], 'DELETE')
self.assertEqual(kw[0]['path'], '/b/%s/o/%s' % (NAME, KEY))

def test_delete_keys_miss(self):
def test_delete_keys_miss_no_on_error(self):
from gcloud.storage.exceptions import NotFoundError
NAME = 'name'
KEY = 'key'
Expand All @@ -247,6 +247,22 @@ def test_delete_keys_miss(self):
self.assertEqual(kw[1]['method'], 'DELETE')
self.assertEqual(kw[1]['path'], '/b/%s/o/%s' % (NAME, NONESUCH))

def test_delete_keys_miss_w_on_error(self):
NAME = 'name'
KEY = 'key'
NONESUCH = 'nonesuch'
connection = _Connection({})
bucket = self._makeOne(connection, NAME)
errors = []
bucket.delete_keys([KEY, NONESUCH], errors.append)
self.assertEqual(errors, [NONESUCH])
kw = connection._requested
self.assertEqual(len(kw), 2)
self.assertEqual(kw[0]['method'], 'DELETE')
self.assertEqual(kw[0]['path'], '/b/%s/o/%s' % (NAME, KEY))
self.assertEqual(kw[1]['method'], 'DELETE')
self.assertEqual(kw[1]['path'], '/b/%s/o/%s' % (NAME, NONESUCH))

def test_copy_keys_wo_name(self):
SOURCE = 'source'
DEST = 'dest'
Expand Down