1414
1515"""Module to provide implicit behavior based on enviroment.
1616
17- Allows the storage package to infer the current project, default bucket
18- and connection from the enviroment.
17+ Allows the storage package to infer the default bucket and connection
18+ from the enviroment.
1919"""
2020
2121
22- import os
23-
24- from gcloud ._helpers import _lazy_property_deco
25-
26-
27- _PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'
28-
29-
30- def _get_production_project ():
31- """Gets the production project if it can be inferred."""
32- return os .getenv (_PROJECT_ENV_VAR_NAME )
33-
34-
35- def _determine_default_project (project = None ):
36- """Determine default project ID explicitly or implicitly as fall-back.
37-
38- In implicit case, currently only supports enviroment variable but will
39- support App Engine, Compute Engine and other environments in the future.
40-
41- Local environment variable used is:
42- - GCLOUD_PROJECT
43-
44- :type project: string
45- :param project: Optional. The project name to use as default.
46-
47- :rtype: string or ``NoneType``
48- :returns: Default project if it can be determined.
49- """
50- if project is None :
51- project = _get_production_project ()
52-
53- return project
54-
55-
56- def set_default_project (project = None ):
57- """Set default project either explicitly or implicitly as fall-back.
58-
59- :type project: string
60- :param project: Optional. The project name to use as default.
61-
62- :raises: :class:`EnvironmentError` if no project was found.
63- """
64- project = _determine_default_project (project = project )
65- if project is not None :
66- _DEFAULTS .project = project
67- else :
68- raise EnvironmentError ('No project could be inferred.' )
69-
70-
7122class _DefaultsContainer (object ):
7223 """Container for defaults.
7324
74- :type project: string
75- :param project: Persistent implied project from environment.
76-
7725 :type bucket: :class:`gcloud.storage.bucket.Bucket`
7826 :param bucket: Persistent implied default bucket from environment.
7927
8028 :type connection: :class:`gcloud.storage.connection.Connection`
8129 :param connection: Persistent implied connection from environment.
8230 """
8331
84- @_lazy_property_deco
85- @staticmethod
86- def project ():
87- """Return the implicit default project."""
88- return _determine_default_project ()
89-
90- def __init__ (self , project = None , bucket = None , connection = None ,
91- implicit = False ):
92- if project is not None or not implicit :
93- self .project = project
32+ def __init__ (self , bucket = None , connection = None ):
9433 self .bucket = bucket
9534 self .connection = connection
9635
9736
98- def get_default_project ():
99- """Get default project.
100-
101- :rtype: string or ``NoneType``
102- :returns: The default project if one has been set.
103- """
104- return _DEFAULTS .project
105-
106-
10737def get_default_bucket ():
10838 """Get default bucket.
10939
@@ -122,4 +52,4 @@ def get_default_connection():
12252 return _DEFAULTS .connection
12353
12454
125- _DEFAULTS = _DefaultsContainer (implicit = True )
55+ _DEFAULTS = _DefaultsContainer ()
0 commit comments