示例#1
0
    def test_set_explicit(self):
        from gcloud.storage import _implicit_environ

        self.assertEqual(_implicit_environ.get_default_connection(), None)
        fake_cnxn = object()
        self._callFUT(connection=fake_cnxn)
        self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)
    def test_set_explicit(self):
        from gcloud.storage import _implicit_environ

        self.assertEqual(_implicit_environ.get_default_connection(), None)
        fake_cnxn = object()
        self._callFUT(connection=fake_cnxn)
        self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)
示例#3
0
    def __init__(self, connection=None):
        if connection is None:
            connection = _implicit_environ.get_default_connection()

        super(Batch, self).__init__()
        self._connection = connection
        self._requests = []
        self._responses = []
示例#4
0
    def __init__(self, connection=None):
        if connection is None:
            connection = _implicit_environ.get_default_connection()

        super(Batch, self).__init__()
        self._connection = connection
        self._requests = []
        self._target_objects = []
    def test_set_implicit(self):
        from gcloud._testing import _Monkey
        from gcloud.storage import _implicit_environ

        self.assertEqual(_implicit_environ.get_default_connection(), None)

        fake_cnxn = object()
        _called_args = []
        _called_kwargs = []

        def mock_get_connection(*args, **kwargs):
            _called_args.append(args)
            _called_kwargs.append(kwargs)
            return fake_cnxn

        with _Monkey(_implicit_environ, get_connection=mock_get_connection):
            self._callFUT()

        self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)
        self.assertEqual(_called_args, [()])
        self.assertEqual(_called_kwargs, [{}])
示例#6
0
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
示例#7
0
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
示例#8
0
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)
        connection = get_default_connection()

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

    if bucket is not None:
        _implicit_environ._DEFAULTS.bucket = bucket
示例#9
0
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)
        connection = get_default_connection()

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

    if bucket is not None:
        _implicit_environ._DEFAULTS.bucket = bucket
 def _callFUT(self):
     from gcloud.storage._implicit_environ import get_default_connection
     return get_default_connection()
 def _callFUT(self):
     from gcloud.storage._implicit_environ import get_default_connection
     return get_default_connection()