8000 Removing _implicit_environ from storage. · dhermes/google-cloud-python@e627eaa · GitHub
[go: up one dir, main page]

Skip to content

Commit e627eaa

Browse files
committed
Removing _implicit_environ from storage.
Towards googleapis#952.
1 parent a928c86 commit e627eaa
< 10000 div class="prc-PageLayout-ContentWrapper-b-QRo Commit-module__SplitPageLayout_Content--ZY6yu" data-is-hidden="false">

16 files changed

+222
-904
lines changed

gcloud/_helpers.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,6 @@ def set_default_project(project=None):
228228
raise EnvironmentError('No project could be inferred.')
229229

230230

231-
def get_default_project():
232-
"""Get default project.
233-
234-
:rtype: string or ``NoneType``
235-
:returns: The default project if one has been set.
236-
"""
237-
return _DEFAULTS.project
238-
239-
240231
class _DefaultsContainer(object):
241232
"""Container for defaults.
242233

gcloud/_testing.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
3535
setattr(self.module, key, value)
3636

3737

38-
def _monkey_defaults(*args, **kwargs):
39-
mock_defaults = _DefaultsContainer(*args, **kwargs)
40-
return _Monkey(_helpers, _DEFAULTS=mock_defaults)
41-
42-
4338
def _setup_defaults(test_case, *args, **kwargs):
4439
test_case._replaced_defaults = _helpers._DEFAULTS
4540
_helpers._DEFAULTS = _DefaultsContainer(*args, **kwargs)

gcloud/storage/_helpers.py

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
from Crypto.Hash import MD5
2121
import base64
2222

23-
from gcloud.storage._implicit_environ import get_default_connection
24-
from gcloud.storage.batch import Batch
25-
2623

2724
class _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)
12812 10000 6

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-
154128
def _scalar_property(fieldname):
155129
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
156130
"""

gcloud/storage/_implicit_environ.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

gcloud/storage/_testing.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

gcloud/storage/acl.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@
7878
when sending metadata for ACLs to the API.
7979
"""
8080

81-
from gcloud.storage._helpers import _require_connection
82-
8381

8482
class _ACLEntity(object):
8583
"""Class representing a set of roles for an entity.
@@ -351,23 +349,24 @@ def get_entities(self):
351349
self._ensure_loaded()
352350
return list(self.entities.values())
353351

354-
@staticmethod
355-
def _client_or_connection(client):
356-
"""Temporary method to get a connection from a client.
352+
@property
353+
def client(self):
354+
"""Abstract getter for the object client."""
355+
raise NotImplementedError
357356

358-
If the client is null, gets the connection from the environment.
357+
def _require_client(self, client):
358+
"""Check client or verify over-ride.
359359
360360
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
361-
:param client: Optional. The client to use. If not passed, falls back
362-
to default connection.
361+
:param client: the client to use. If not passed, falls back to the
362+
``client`` stored on the current object.
363363
364-
:rtype: :class:`gcloud.storage.connection.Connection`
365-
:returns: The connection determined from the ``client`` or environment.
364+
:rtype: :class:`gcloud.storage.client.Client`
365+
:returns: The client passed in or the currently bound client.
366366
"""
367367
if client is None:
368-
return _require_connection()
369-
else:
370-
return client.connection
368+
client = self.client
369+
return client
371370

372371
def reload(self, client=None):
373372
"""Reload the ACL data from Cloud Storage.
@@ -377,11 +376,11 @@ def reload(self, client=None):
377376
to default connection.
378377
"""
379378
path = self.reload_path
380-
connection = self._client_or_connection(client)
379+
client = self._require_client(client)
381380

382381
self.entities.clear()
383382

384-
found = connection.api_request(method='GET', path=path)
383+
found = client.connection.api_request(method='GET', path=path)
385384
self.loaded = True
386385
for entry in found.get('items', ()):
387386
self.add_entity(self.entity_from_dict(entry))
@@ -405,8 +404,8 @@ def save(self, acl=None, client=None):
405404

406405
if save_to_backend:
407406
path = self.save_path
408-
connection = self._client_or_connection(client)
409-
result = connection.api_request(
407+
client = self._require_client(client)
408+
result = client.connection.api_request(
410409
method='PATCH',
411410
path=path,
412411
data={self._URL_PATH_ELEM: list(acl)},
@@ -442,6 +441,11 @@ def __init__(self, bucket):
442441
super(BucketACL, self).__init__()
443442
self.bucket = bucket
444443

444+
@property
445+
def client(self):
446+
"""The client bound to this ACL's bucket."""
447+
return self.bucket.client
448+
445449
@property
446450
def reload_path(self):
447451
"""Compute the path for GET API requests for this ACL."""
@@ -470,6 +474,11 @@ def __init__(self, blob):
470474
super(ObjectACL, self).__init__()
471475
self.blob = blob
472476

477+
@property
478+
def client(self):
479+
"""The client bound to this ACL's blob."""
480+
return self.blob.client
481+
473482
@property
474483
def reload_path(self):
475484
"""Compute the path for GET API requests for this ACL."""

0 commit comments

Comments
 (0)
0