8000 Remove implicit environ from storage by dhermes · Pull Request #988 · googleapis/google-cloud-python · GitHub
[go: up one dir, main page]

Skip to content

Remove implicit environ from storage #988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/storage-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ Storage

.. include:: _components/storage-quickstart.rst

:mod:`gcloud.storage`
~~~~~~~~~~~~~~~~~~~~~

.. automodule:: gcloud.storage
:members: get_connection, get_default_connection, get_default_bucket,
set_default_connection, set_default_bucket, set_defaults

Connections
~~~~~~~~~~~

Expand Down
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):
"""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):
test_case._replaced_defaults = _helpers._DEFAULTS
_helpers._DEFAULTS = _DefaultsContainer(*args, **kwargs)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Expand Down
59 changes: 0 additions & 59 deletions gcloud/storage/__init__.py
< 8000 td class="blob-num blob-num-deletion empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -38,68 +38,9 @@
machine).
"""

import os

from gcloud import credentials
from gcloud._helpers import get_default_project
from gcloud._helpers import set_default_project
from gcloud.storage import _implicit_environ
from gcloud.storage._implicit_environ import get_connection
from gcloud.storage._implicit_environ import get_default_bucket
from gcloud.storage._implicit_environ import get_default_connection
from gcloud.storage._implicit_environ import set_default_connection
from gcloud.storage.batch import Batch
from gcloud.storage.blob import Blob
from gcloud.storage.bucket import Bucket
from gcloud.storage.client import Client
from gcloud.storage.connection import SCOPE
from gcloud.storage.connection import Connection


_BUCKET_ENV_VAR_NAME = 'GCLOUD_BUCKET_NAME'


def set_default_bucket(bucket=None):
"""Set default bucket either explicitly or implicitly as fall-back.

In implicit case, currently only supports enviroment variable but will
support App Engine, Compute Engine and other environments in the future.

In the implicit case, relies on an implicit connection in addition to the
implicit bucket name.

Local environment variable used is:
- GCLOUD_BUCKET_NAME

:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: Optional. The bucket to use as default.
"""
if bucket is None:
bucket_name = os.getenv(_BUCKET_ENV_VAR_NAME)

if bucket_name is not None:
bucket = Bucket(None, name=bucket_name)

if bucket is not None:
_implicit_environ._DEFAULTS.bucket = bucket


def set_defaults(bucket=None, project=None, connection=None):
"""Set defaults either explicitly or implicitly as fall-back.

Uses the arguments to call the individual default methods.

:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: Optional. The bucket to use as default.

:type project: string
:param project: Optional. The name of the project to connect to.

:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Optional. A connection provided to be the default.
"""
set_default_project(project=project)
set_default_connection(connection=connection)
# NOTE: `set_default_bucket` is called after `set_default_connection`
# since `set_default_bucket` falls back to implicit connection.
set_default_bucket(bucket=bucket)
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.

Loading
0