From 50da3a3cd288c9ce98568e8204b6bbcf4a994035 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 10 Apr 2015 15:54:48 -0700 Subject: [PATCH] Preparing to use futures in storage. Wraps setting/getting of object _properties in custom methods. This will allow centralized detection of a future in a response and will also allow replacing with the value on access if it is ready. Towards #775 --- gcloud/storage/_helpers.py | 20 ++++++++++++++------ gcloud/storage/api.py | 2 +- gcloud/storage/blob.py | 2 +- gcloud/storage/bucket.py | 9 +++++---- gcloud/storage/iterator.py | 2 +- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/gcloud/storage/_helpers.py b/gcloud/storage/_helpers.py index 0f36e7f04214..ad0fd43d7f2a 100644 --- a/gcloud/storage/_helpers.py +++ b/gcloud/storage/_helpers.py @@ -54,10 +54,9 @@ def reload(self): # Pass only '?projection=noAcl' here because 'acl' and related # are handled via custom endpoints. query_params = {'projection': 'noAcl'} - self._properties = self.connection.api_request( + api_response = self.connection.api_request( method='GET', path=self.path, query_params=query_params) - # If the api_request succeeded, we reset changes. - self._changes = set() + self._set_properties(api_response) def _patch_property(self, name, value): """Update field of this object's properties. @@ -77,6 +76,16 @@ def _patch_property(self, name, value): self._changes.add(name) self._properties[name] = value + def _set_properties(self, value): + """Set the properties for the current object. + + :type value: dict + :param value: The properties to be set. + """ + self._properties = value + # If the values are reset, the changes must as well. + self._changes = set() + def patch(self): """Sends all changed properties in a PATCH request. @@ -86,11 +95,10 @@ def patch(self): # to work properly w/ 'noAcl'. update_properties = dict((key, self._properties[key]) for key in self._changes) - self._properties = self.connection.api_request( + api_response = self.connection.api_request( method='PATCH', path=self.path, data=update_properties, query_params={'projection': 'full'}) - # If the api_request succeeded, we reset changes. - self._changes = set() + self._set_properties(api_response) def _scalar_property(fieldname): diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index ae3243a6ed52..2ac270102870 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -227,7 +227,7 @@ def get_items_from_response(self, response): for item in response.get('items', []): name = item.get('name') bucket = Bucket(name, connection=self.connection) - bucket._properties = item + bucket._set_properties(item) yield bucket diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 52cc890c7a6b..dc30d21d5f7e 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -386,7 +386,7 @@ def upload_from_file(self, file_obj, rewind=False, size=None, if not isinstance(response_content, six.string_types): # pragma: NO COVER Python3 response_content = response_content.decode('utf-8') - self._properties = json.loads(response_content) + self._set_properties(json.loads(response_content)) def upload_from_filename(self, filename, content_type=None): """Upload this blob's contents from the content of a named file. diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 83291b986346..023c7440a3c4 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -75,7 +75,7 @@ def get_items_from_response(self, response): for item in response.get('items', []): name = item.get('name') blob = Blob(name, bucket=self.bucket) - blob._properties = item + blob._set_properties(item) yield blob @@ -152,9 +152,10 @@ def create(self, project=None): 'from environment.') query_params = {'project': project} - self._properties = self.connection.api_request( + api_response = self.connection.api_request( method='POST', path='/b', query_params=query_params, data={'name': self.name}) + self._set_properties(api_response) @property def acl(self): @@ -220,7 +221,7 @@ def get_blob(self, blob_name): path=blob.path) name = response.get('name') # Expect this to be blob_name blob = Blob(name, bucket=self) - blob._properties = response + blob._set_properties(response) return blob except NotFound: return None @@ -408,7 +409,7 @@ def copy_blob(self, blob, destination_bucket, new_name=None): new_blob = Blob(bucket=destination_bucket, name=new_name) api_path = blob.path + '/copyTo' + new_blob.path copy_result = self.connection.api_request(method='POST', path=api_path) - new_blob._properties = copy_result + new_blob._set_properties(copy_result) return new_blob def upload_file(self, filename, blob_name=None): diff --git a/gcloud/storage/iterator.py b/gcloud/storage/iterator.py index 284e9b5392e4..ef65882988f2 100644 --- a/gcloud/storage/iterator.py +++ b/gcloud/storage/iterator.py @@ -26,7 +26,7 @@ def get_items_from_response(self, response): items = response.get('items', []) for item in items: my_item = MyItemClass(other_arg=True) - my_item._properties = item + my_item._set_properties(item) yield my_item You then can use this to get **all** the results from a resource::