Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
#741: hoist project detection/setup into 'gcloud._helpers'.#748
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -14,96 +14,26 @@ | ||
| """Module to provide implicit behavior based on enviroment. | ||
| Allows the storage package to infer the current project, default bucket | ||
| and connection from the enviroment. | ||
| Allows the storage package to infer the default bucket and connection | ||
| from the enviroment. | ||
| """ | ||
| import os | ||
| from gcloud._helpers import _lazy_property_deco | ||
| _PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT' | ||
| def _get_production_project(): | ||
| """Gets the production project if it can be inferred.""" | ||
| return os.getenv(_PROJECT_ENV_VAR_NAME) | ||
| def _determine_default_project(project=None): | ||
| """Determine default project ID 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. | ||
| :rtype: string or ``NoneType`` | ||
| :returns: Default project if it can be determined. | ||
| """ | ||
| if project is None: | ||
| project = _get_production_project() | ||
| return project | ||
| def set_default_project(project=None): | ||
| """Set default project either explicitly or implicitly as fall-back. | ||
| :type project: string | ||
| :param project: Optional. The project name to use as default. | ||
| :raises: :class:`EnvironmentError` if no project was found. | ||
| """ | ||
| project = _determine_default_project(project=project) | ||
| if project is not None: | ||
| _DEFAULTS.project = project | ||
| else: | ||
| raise EnvironmentError('No project could be inferred.') | ||
| class _DefaultsContainer(object): | ||
| """Container for defaults. | ||
| :type project: string | ||
| :param project: Persistent implied project from environment. | ||
| :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. | ||
| """ | ||
| @_lazy_property_deco | ||
| @staticmethod | ||
| def project(): | ||
| """Return the implicit default project.""" | ||
| return _determine_default_project() | ||
| def __init__(self, project=None, bucket=None, connection=None, | ||
| implicit=False): | ||
| if project is not None or not implicit: | ||
| self.project = project | ||
| def __init__(self, bucket=None, connection=None): | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| 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. | ||
| @@ -122,4 +52,4 @@ def get_default_connection(): | ||
| return _DEFAULTS.connection | ||
| _DEFAULTS = _DefaultsContainer(implicit=True) | ||
| _DEFAULTS = _DefaultsContainer() | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.