Skip to content

Commit

Permalink
Updating Bucket.copy_blob and Blob.rename methods to accept a client.
Browse files Browse the repository at this point in the history
Towards googleapis#952, removing connection from methods / constructors.
  • Loading branch information
dhermes committed Jul 11, 2015
1 parent d8246d0 commit bf3516f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
13 changes: 6 additions & 7 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def exists(self, client=None):
except NotFound:
return False

def rename(self, new_name, connection=None):
def rename(self, new_name, client=None):
"""Renames this blob using copy and delete operations.
Effectively, copies blob to the same bucket with a new name, then
Expand All @@ -259,17 +259,16 @@ def rename(self, new_name, connection=None):
:type new_name: string
:param new_name: The new name for this blob.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`Blob`
:returns: The newly-copied blob.
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)
new_blob = self.bucket.copy_blob(self, self.bucket, new_name,
connection=connection)
client=client)
self.delete(connection=connection)
return new_blob

Expand Down
14 changes: 6 additions & 8 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,8 @@ def delete_blobs(self, blobs, on_error=None, connection=None):
else:
raise

@staticmethod
def copy_blob(blob, destination_bucket, new_name=None,
connection=None):
def copy_blob(self, blob, destination_bucket, new_name=None,
client=None):
"""Copy the given blob to the given bucket, optionally with a new name.
:type blob: string or :class:`gcloud.storage.blob.Blob`
Expand All @@ -453,15 +452,14 @@ def copy_blob(blob, destination_bucket, new_name=None,
:type new_name: string
:param new_name: (optional) the new name for the copied file.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`gcloud.storage.blob.Blob`
:returns: The new Blob.
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)
if new_name is None:
new_name = blob.name
new_blob = Blob(bucket=destination_bucket, name=new_name)
Expand Down
25 changes: 5 additions & 20 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,30 +284,15 @@ def test_exists_hit(self):
bucket._blobs[BLOB_NAME] = 1
self.assertTrue(blob.exists(client=client))

def test_rename_w_implicit_connection(self):
from gcloud.storage._testing import _monkey_defaults
BLOB_NAME = 'blob-name'
NEW_NAME = 'new-name'
connection = _Connection()
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
with _monkey_defaults(connection=connection):
new_blob = blob.rename(NEW_NAME)
self.assertEqual(blob.name, BLOB_NAME)
self.assertEqual(new_blob.name, NEW_NAME)
self.assertFalse(BLOB_NAME in bucket._blobs)
self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])
self.assertTrue(NEW_NAME in bucket._blobs)

def test_rename_w_explicit_connection(self):
def test_rename(self):
BLOB_NAME = 'blob-name'
NEW_NAME = 'new-name'
connection = _Connection()
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
bucket._blobs[BLOB_NAME] = 1
new_blob = blob.rename(NEW_NAME, connection=connection)
new_blob = blob.rename(NEW_NAME, client=client)
self.assertEqual(blob.name, BLOB_NAME)
self.assertEqual(new_blob.name, NEW_NAME)
self.assertFalse(BLOB_NAME in bucket._blobs)
Expand Down Expand Up @@ -1157,8 +1142,8 @@ def __init__(self):
self._copied = []
self._deleted = []

def copy_blob(self, blob, destination_bucket, new_name, connection=None):
self._copied.append((blob, destination_bucket, new_name, connection))
def copy_blob(self, blob, destination_bucket, new_name, client=None):
self._copied.append((blob, destination_bucket, new_name, client))
destination_bucket._blobs[new_name] = self._blobs[blob.name]
return blob.__class__(new_name, bucket=destination_bucket)

Expand Down
6 changes: 4 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,11 @@ class _Blob(object):
path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME)

connection = _Connection({})
client = _Client(connection)
source = self._makeOne(SOURCE)
dest = self._makeOne(DEST)
blob = _Blob()
new_blob = source.copy_blob(blob, dest, connection=connection)
new_blob = source.copy_blob(blob, dest, client=client)
self.assertTrue(new_blob.bucket is dest)
self.assertEqual(new_blob.name, BLOB_NAME)
kw, = connection._requested
Expand All @@ -539,11 +540,12 @@ class _Blob(object):
path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME)

connection = _Connection({})
client = _Client(connection)
source = self._makeOne(SOURCE)
dest = self._makeOne(DEST)
blob = _Blob()
new_blob = source.copy_blob(blob, dest, NEW_NAME,
connection=connection)
client=client)
self.assertTrue(new_blob.bucket is dest)
self.assertEqual(new_blob.name, NEW_NAME)
kw, = connection._requested
Expand Down

0 comments on commit bf3516f

Please sign in to comment.