Skip to content

Commit

Permalink
Adding methods for default / implicit behavior in storage.
Browse files Browse the repository at this point in the history
Addresses part of googleapis#337.
  • Loading branch information
dhermes committed Jan 27, 2015
1 parent 86acc4a commit 2d87f34
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# 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.

"""Module to provide implicit behavior based on enviroment.
Acts as a mutable namespace to allow the datastore package to
Expand Down
81 changes: 81 additions & 0 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,95 @@
machine).
"""

import os

from gcloud import credentials
from gcloud.storage import _implicit_environ
from gcloud.storage.connection import Connection


SCOPE = ('https://www.googleapis.com/auth/devstorage.full_control',
'https://www.googleapis.com/auth/devstorage.read_only',
'https://www.googleapis.com/auth/devstorage.read_write')

_BUCKET_ENV_VAR_NAME = 'GCLOUD_BUCKET_NAME'
_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'


def set_default_bucket_name(bucket_name=None):
"""Set default bucket name 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.
Local environment variable used is:
- GCLOUD_BUCKET_NAME
:type bucket_name: string
:param bucket_name: Optional. The bucket name to use as default.
"""
if bucket_name is None:
bucket_name = os.getenv(_BUCKET_ENV_VAR_NAME)

if bucket_name is not None:
_implicit_environ.BUCKET_NAME = bucket_name


def set_default_project(project=None):
"""Set default bucket name 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.
Local environment variable used is:
- GCLOUD_PROJECT
:type project: string
:param project: Optional. The project name to use as default.
"""
if project is None:
project = os.getenv(_PROJECT_ENV_VAR_NAME)

if project is not None:
_implicit_environ.PROJECT = project


def set_default_connection(project=None, connection=None):
"""Set default connection either explicitly or implicitly as fall-back.
:type project: string
:param project: Optional. The name of the project to connect to.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: A connection provided to be the default.
"""
if project is None:
project = _implicit_environ.PROJECT

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


def set_defaults(bucket_name=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_name: string
:param bucket_name: Optional. The bucket name to use as default.
:type project: string
:param project: Optional. The name of the project to connect to.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: A connection provided to be the default.
"""
set_default_bucket_name(bucket_name=bucket_name)
# NOTE: `set_default_project` is called before `set_default_connection`
# since `set_default_connection` falls back to implicit project.
set_default_project(project=project)
set_default_connection(project=project, connection=connection)


def get_connection(project):
"""Shortcut method to establish a connection to Cloud Storage.
Expand Down
29 changes: 29 additions & 0 deletions gcloud/storage/_implicit_environ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2015 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.

"""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.
"""


PROJECT = None
"""Module global to allow persistent implied project from enviroment."""

BUCKET_NAME = None
"""Module global to allow persistent implied bucket name from enviroment."""

CONNECTION = None
"""Module global to allow persistent implied connection from enviroment."""
Loading

0 comments on commit 2d87f34

Please sign in to comment.