Skip to content
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
4 changes: 2 additions & 2 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

"""Module to provide implicit behavior based on enviroment.

Acts as a mutable namespace to allow the datastore package to
imply the current dataset ID and connection from the enviroment.
Allows the datastore package to infer the current dataset ID and
connection from the enviroment.
"""

import os
Expand Down
13 changes: 8 additions & 5 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

from gcloud import credentials
from gcloud.storage import _implicit_environ
from gcloud.storage._implicit_environ import get_default_bucket
from gcloud.storage._implicit_environ import get_default_connection
from gcloud.storage._implicit_environ import get_default_project
from gcloud.storage.blob import Blob
from gcloud.storage.bucket import Bucket
from gcloud.storage.connection import Connection
Expand Down Expand Up @@ -71,13 +74,13 @@ def set_default_bucket(bucket=None):
"""
if bucket is None:
bucket_name = os.getenv(_BUCKET_ENV_VAR_NAME)
connection = _implicit_environ.CONNECTION
connection = get_default_connection()

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

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


def set_default_project(project=None):
Expand All @@ -96,7 +99,7 @@ def set_default_project(project=None):
project = os.getenv(_PROJECT_ENV_VAR_NAME)

if project is not None:
_implicit_environ.PROJECT = project
_implicit_environ._DEFAULTS.project = project


def set_default_connection(project=None, connection=None):
Expand All @@ -109,10 +112,10 @@ def set_default_connection(project=None, connection=None):
:param connection: A connection provided to be the default.
"""
if project is None:
project = _implicit_environ.PROJECT
project = get_default_project()

connection = connection or get_connection(project)
_implicit_environ.CONNECTION = connection
_implicit_environ._DEFAULTS.connection = connection


def set_defaults(bucket=None, project=None, connection=None):
Expand Down
55 changes: 47 additions & 8 deletions gcloud/storage/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,55 @@

"""Module to provide implicit behavior based on enviroment.

Acts as a mutable namespace to allow the datastore package to
infer the current dataset ID and connection from the enviroment.
Allows the storage package to infer the current project, default bucket
and connection from the enviroment.
"""


PROJECT = None
"""Module global to allow persistent implied project from enviroment."""
class _DefaultsContainer(object):
"""Container for defaults.

BUCKET = None
"""Module global to allow persistent implied bucket from enviroment."""
:type project: string
:param project: Persistent implied project from environment.

CONNECTION = None
"""Module global to allow persistent implied connection from enviroment."""
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: Persistent implied default bucket from environment.

:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Persistent implied connection from environment.
"""

def __init__(self, project=None, bucket=None, connection=None):
self.project = project
self.bucket = bucket
self.connection = connection


def get_default_project():
"""Get default project.

:rtype: string or ``NoneType``
:returns: The default project if one has been set.
"""
return _DEFAULTS.project


def get_default_bucket():
"""Get default bucket.

:rtype: :class:`gcloud.storage.bucket.Bucket` or ``NoneType``
:returns: The default bucket if one has been set.
"""
return _DEFAULTS.bucket


def get_default_connection():
"""Get default connection.

:rtype: :class:`gcloud.storage.connection.Connection` or ``NoneType``
:returns: The default connection if one has been set.
"""
return _DEFAULTS.connection


_DEFAULTS = _DefaultsContainer()
33 changes: 33 additions & 0 deletions gcloud/storage/_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Shared storage testing utilities."""

from gcloud._testing import _Monkey
from gcloud.storage import _implicit_environ
from gcloud.storage._implicit_environ import _DefaultsContainer


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


def _setup_defaults(test_case, *args, **kwargs):
test_case._replaced_defaults = _implicit_environ._DEFAULTS
_implicit_environ._DEFAULTS = _DefaultsContainer(*args, **kwargs)


def _tear_down_defaults(test_case):
_implicit_environ._DEFAULTS = test_case._replaced_defaults
2 changes: 1 addition & 1 deletion gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(self, name, bucket=None, properties=None):
name = properties.get('name')

if bucket is None:
bucket = _implicit_environ.BUCKET
bucket = _implicit_environ.get_default_bucket()

if bucket is None:
raise ValueError('A Blob must have a bucket set.')
Expand Down
Loading