From c30132b5bac49ae33fb47b96759428ca694a3bdb Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 10 Jul 2015 20:41:42 -0700 Subject: [PATCH] Updating Bucket.copy_blob and Blob.rename methods to accept a client. Towards #952, removing connection from methods / constructors. --- gcloud/storage/blob.py | 13 ++++++------- gcloud/storage/bucket.py | 14 ++++++-------- gcloud/storage/test_blob.py | 25 +++++-------------------- gcloud/storage/test_bucket.py | 6 ++++-- 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 37d6063f0abf..1b72843c1728 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -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 @@ -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 diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 37a013503679..3aa4d4a107d4 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -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` @@ -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) diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 58d9ea6b8c1a..3f8065515159 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -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) @@ -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) diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 70853e6f2378..c9c4a9564306 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -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 @@ -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