2020from Crypto .Hash import MD5
2121import base64
2222
23- from gcloud .storage ._implicit_environ import get_default_connection
24- from gcloud .storage .batch import Batch
25-
2623
2724class _PropertyMixin (object ):
2825 """Abstract mixin for cloud storage classes with associated propertties.
@@ -35,33 +32,34 @@ class _PropertyMixin(object):
3532 :param name: The name of the object.
3633 """
3734
38- @property
39- def path (self ):
40- """Abstract getter for the object path."""
41- raise NotImplementedError
42-
4335 def __init__ (self , name = None ):
4436 self .name = name
4537 self ._properties = {}
4638 self ._changes = set ()
4739
48- @staticmethod
49- def _client_or_connection (client ):
50- """Temporary method to get a connection from a client.
40+ @property
41+ def path (self ):
42+ """Abstract getter for the object path."""
43+ raise NotImplementedError
5144
52- If the client is null, gets the connection from the environment.
45+ @property
46+ def client (self ):
47+ """Abstract getter for the object client."""
48+ raise NotImplementedError
49+
50+ def _require_client (self , client ):
51+ """Check client or verify over-ride.
5352
5453 :type client: :class:`gcloud.storage.client.Client` or ``NoneType``
55- :param client: Optional. The client to use. If not passed, falls back
56- to default connection .
54+ :param client: the client to use. If not passed, falls back to the
55+ ``client`` stored on the current object .
5756
58- :rtype: :class:`gcloud.storage.connection.Connection `
59- :returns: The connection determined from the ``client`` or environment .
57+ :rtype: :class:`gcloud.storage.client.Client `
58+ :returns: The client passed in or the currently bound client .
6059 """
6160 if client is None :
62- return _require_connection ()
63- else :
64- return client .connection
61+ client = self .client
62+ return client
6563
6664 def reload (self , client = None ):
6765 """Reload properties from Cloud Storage.
@@ -70,11 +68,11 @@ def reload(self, client=None):
7068 :param client: Optional. The client to use. If not passed, falls back
7169 to default connection.
7270 """
73- connection = self ._client_or_connection (client )
71+ client = self ._require_client (client )
7472 # Pass only '?projection=noAcl' here because 'acl' and related
7573 # are handled via custom endpoints.
7674 query_params = {'projection' : 'noAcl' }
77- api_response = connection .api_request (
75+ api_response = client . connection .api_request (
7876 method = 'GET' , path = self .path , query_params = query_params ,
7977 _target_object = self )
8078 self ._set_properties (api_response )
@@ -116,41 +114,17 @@ def patch(self, client=None):
116114 :param client: Optional. The client to use. If not passed, falls back
117115 to default connection.
118116 """
119- connection = self ._client_or_connection (client )
117+ client = self ._require_client (client )
120118 # Pass '?projection=full' here because 'PATCH' documented not
121119 # to work properly w/ 'noAcl'.
122120 update_properties = dict ((key , self ._properties [key ])
123121 for key in self ._changes )
124- api_response = connection .api_request (
122+ api_response = client . connection .api_request (
125123 method = 'PATCH' , path = self .path , data = update_properties ,
126124 query_params = {'projection' : 'full' }, _target_object = self )
127125 self ._set_properties (api_response )
128126
129127
130- def _require_connection (connection = None ):
131- """Infer a connection from the environment, if not passed explicitly.
132-
133- :type connection: :class:`gcloud.storage.connection.Connection`
134- :param connection: Optional.
135-
136- :rtype: :class:`gcloud.storage.connection.Connection`
137- :returns: A connection based on the current environment.
138- :raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
139- cannot be inferred from the environment.
140- """
141- # NOTE: We use current Batch directly since it inherits from Connection.
142- if connection is None :
143- connection = Batch .current ()
144-
145- if connection is None :
146- connection = get_default_connection ()
147-
148- if connection is None :
149- raise EnvironmentError ('Connection could not be inferred.' )
150-
151- return connection
152-
153-
154128def _scalar_property (fieldname ):
155129 """Create a property descriptor around the :class:`_PropertyMixin` helpers.
156130 """
0 commit comments