Skip to content

Commit

Permalink
Removing _implicit_environ from storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jul 14, 2015
1 parent a928c86 commit e627eaa
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 904 deletions.
9 changes: 0 additions & 9 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,6 @@ def set_default_project(project=None):
raise EnvironmentError('No project could be inferred.')


def get_default_project():
"""Get default project.
:rtype: string or ``NoneType``
:returns: The default project if one has been set.
"""
return _DEFAULTS.project


class _DefaultsContainer(object):

This comment has been minimized.

Copy link
@tseaver

tseaver Jul 14, 2015

Shouldn't _DefaultsContainer go away too?

This comment has been minimized.

Copy link
@dhermes

dhermes Jul 14, 2015

Author Owner

Does in googleapis#991

"""Container for defaults.
Expand Down
5 changes: 0 additions & 5 deletions gcloud/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
setattr(self.module, key, value)


def _monkey_defaults(*args, **kwargs):
mock_defaults = _DefaultsContainer(*args, **kwargs)
return _Monkey(_helpers, _DEFAULTS=mock_defaults)


def _setup_defaults(test_case, *args, **kwargs):

This comment has been minimized.

Copy link
@tseaver

tseaver Jul 14, 2015

Likewise the whole _testing module, which exists only to support the _implicit_environ pattern.

This comment has been minimized.

Copy link
@dhermes

dhermes Jul 14, 2015

Author Owner

It goes in googleapis#988.

test_case._replaced_defaults = _helpers._DEFAULTS
_helpers._DEFAULTS = _DefaultsContainer(*args, **kwargs)
Expand Down
68 changes: 21 additions & 47 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
from Crypto.Hash import MD5
import base64

from gcloud.storage._implicit_environ import get_default_connection
from gcloud.storage.batch import Batch


class _PropertyMixin(object):
"""Abstract mixin for cloud storage classes with associated propertties.
Expand All @@ -35,33 +32,34 @@ class _PropertyMixin(object):
:param name: The name of the object.
"""

@property
def path(self):
"""Abstract getter for the object path."""
raise NotImplementedError

def __init__(self, name=None):
self.name = name
self._properties = {}
self._changes = set()

@staticmethod
def _client_or_connection(client):
"""Temporary method to get a connection from a client.
@property
def path(self):
"""Abstract getter for the object path."""
raise NotImplementedError

If the client is null, gets the connection from the environment.
@property
def client(self):
"""Abstract getter for the object client."""
raise NotImplementedError

def _require_client(self, client):
"""Check client or verify over-ride.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current object.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: The connection determined from the ``client`` or environment.
:rtype: :class:`gcloud.storage.client.Client`
:returns: The client passed in or the currently bound client.
"""
if client is None:
return _require_connection()
else:
return client.connection
client = self.client
return client

def reload(self, client=None):
"""Reload properties from Cloud Storage.
Expand All @@ -70,11 +68,11 @@ def reload(self, client=None):
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
connection = self._client_or_connection(client)
client = self._require_client(client)
# Pass only '?projection=noAcl' here because 'acl' and related
# are handled via custom endpoints.
query_params = {'projection': 'noAcl'}
api_response = connection.api_request(
api_response = client.connection.api_request(
method='GET', path=self.path, query_params=query_params,
_target_object=self)
self._set_properties(api_response)
Expand Down Expand Up @@ -116,41 +114,17 @@ def patch(self, client=None):
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
connection = self._client_or_connection(client)
client = self._require_client(client)
# Pass '?projection=full' here because 'PATCH' documented not
# to work properly w/ 'noAcl'.
update_properties = dict((key, self._properties[key])
for key in self._changes)
api_response = connection.api_request(
api_response = client.connection.api_request(
method='PATCH', path=self.path, data=update_properties,
query_params={'projection': 'full'}, _target_object=self)
self._set_properties(api_response)


def _require_connection(connection=None):
"""Infer a connection from the environment, if not passed explicitly.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Optional.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: A connection based on the current environment.
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
cannot be inferred from the environment.
"""
# NOTE: We use current Batch directly since it inherits from Connection.
if connection is None:
connection = Batch.current()

if connection is None:
connection = get_default_connection()

if connection is None:
raise EnvironmentError('Connection could not be inferred.')

return connection


def _scalar_property(fieldname):
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
"""
Expand Down
97 changes: 0 additions & 97 deletions gcloud/storage/_implicit_environ.py

This file was deleted.

33 changes: 0 additions & 33 deletions gcloud/storage/_testing.py

This file was deleted.

43 changes: 26 additions & 17 deletions gcloud/storage/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
when sending metadata for ACLs to the API.
"""

from gcloud.storage._helpers import _require_connection


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

@staticmethod
def _client_or_connection(client):
"""Temporary method to get a connection from a client.
@property
def client(self):
"""Abstract getter for the object client."""
raise NotImplementedError

If the client is null, gets the connection from the environment.
def _require_client(self, client):
"""Check client or verify over-ride.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current object.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: The connection determined from the ``client`` or environment.
:rtype: :class:`gcloud.storage.client.Client`
:returns: The client passed in or the currently bound client.
"""
if client is None:
return _require_connection()
else:
return client.connection
client = self.client
return client

def reload(self, client=None):
"""Reload the ACL data from Cloud Storage.
Expand All @@ -377,11 +376,11 @@ def reload(self, client=None):
to default connection.
"""
path = self.reload_path
connection = self._client_or_connection(client)
client = self._require_client(client)

self.entities.clear()

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

if save_to_backend:
path = self.save_path
connection = self._client_or_connection(client)
result = connection.api_request(
client = self._require_client(client)
result = client.connection.api_request(
method='PATCH',
path=path,
data={self._URL_PATH_ELEM: list(acl)},
Expand Down Expand Up @@ -442,6 +441,11 @@ def __init__(self, bucket):
super(BucketACL, self).__init__()
self.bucket = bucket

@property
def client(self):
"""The client bound to this ACL's bucket."""
return self.bucket.client

@property
def reload_path(self):
"""Compute the path for GET API requests for this ACL."""
Expand Down Expand Up @@ -470,6 +474,11 @@ def __init__(self, blob):
super(ObjectACL, self).__init__()
self.blob = blob

@property
def client(self):
"""The client bound to this ACL's blob."""
return self.blob.client

@property
def reload_path(self):
"""Compute the path for GET API requests for this ACL."""
Expand Down
Loading

0 comments on commit e627eaa

Please sign in to comment.