Example #1
0
def _require_dataset_id(dataset_id=None, first_key=None):
    """Infer a dataset ID from the environment, if not passed explicitly.

    Order of precedence:

    - Passed `dataset_id` (if not None).
    - `dataset_id` of current batch / transaction (if current exists).
    - `dataset_id` of first key
    - `dataset_id` inferred from the environment (if `set_default_dataset_id`
      has been called).

    :type dataset_id: string
    :param dataset_id: Optional.

    :type first_key: :class:`gcloud.datastore.key.Key` or None
    :param first_key: Optional: first key being manipulated.

    :rtype: string
    :returns: A dataset ID based on the current environment.
    :raises: :class:`EnvironmentError` if ``dataset_id`` is ``None``,
             and cannot be inferred from the environment.
    """
    if dataset_id is not None:
        return dataset_id
    top = Batch.current()
    if top is not None:
        return top.dataset_id
    if first_key is not None:
        return first_key.dataset_id

    dataset_id = _implicit_environ.get_default_dataset_id()
    if dataset_id is None:
        raise EnvironmentError('Dataset ID could not be inferred.')
    return dataset_id
Example #2
0
def _require_dataset_id(dataset_id=None, first_key=None):
    """Infer a dataset ID from the environment, if not passed explicitly.

    Order of precedence:

    - Passed `dataset_id` (if not None).
    - `dataset_id` of current batch / transaction (if current exists).
    - `dataset_id` of first key
    - `dataset_id` inferred from the environment (if `set_default_dataset_id`
      has been called).

    :type dataset_id: string
    :param dataset_id: Optional.

    :type first_key: :class:`gcloud.datastore.key.Key` or None
    :param first_key: Optional: first key being manipulated.

    :rtype: string
    :returns: A dataset ID based on the current environment.
    :raises: :class:`EnvironmentError` if ``dataset_id`` is ``None``,
             and cannot be inferred from the environment.
    """
    if dataset_id is not None:
        return dataset_id
    top = Batch.current()
    if top is not None:
        return top.dataset_id
    if first_key is not None:
        return first_key.dataset_id

    dataset_id = _implicit_environ.get_default_dataset_id()
    if dataset_id is None:
        raise EnvironmentError('Dataset ID could not be inferred.')
    return dataset_id
Example #3
0
    def __init__(
        self,
        kind=None,
        dataset_id=None,
        namespace=None,
        ancestor=None,
        filters=(),
        projection=(),
        order=(),
        group_by=(),
    ):

        if dataset_id is None:
            dataset_id = _implicit_environ.get_default_dataset_id()

        if dataset_id is None:
            raise ValueError("No dataset ID supplied, and no default set.")

        self._dataset_id = dataset_id
        self._kind = kind
        self._namespace = namespace
        self._ancestor = ancestor
        self._filters = []
        # Verify filters passed in.
        for property_name, operator, value in filters:
            self.add_filter(property_name, operator, value)
        self._projection = _ensure_tuple_or_list("projection", projection)
        self._order = _ensure_tuple_or_list("order", order)
        self._group_by = _ensure_tuple_or_list("group_by", group_by)
Example #4
0
def _validate_dataset_id(dataset_id, parent):
    """Ensure the dataset ID is set appropriately.

    If ``parent`` is passed, skip the test (it will be checked / fixed up
    later).

    If ``dataset_id`` is unset, attempt to infer the ID from the environment.

    :type dataset_id: string
    :param dataset_id: A dataset ID.

    :type parent: :class:`gcloud.datastore.key.Key` or ``NoneType``
    :param parent: The parent of the key or ``None``.

    :rtype: string
    :returns: The ``dataset_id`` passed in, or implied from the environment.
    :raises: :class:`ValueError` if ``dataset_id`` is ``None`` and no dataset
             can be inferred.
    """
    if parent is None:

        if dataset_id is None:

            dataset_id = _implicit_environ.get_default_dataset_id()
            if dataset_id is None:
                raise ValueError("A Key must have a dataset ID set.")

    return dataset_id
    def test_ctor_missing_required(self):
        from gcloud.datastore import _implicit_environ

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

        with self.assertRaises(ValueError):
            self._makeOne()
        with self.assertRaises(ValueError):
            self._makeOne(dataset_id=object())
        with self.assertRaises(ValueError):
            self._makeOne(connection=object())
Example #6
0
    def test_ctor_missing_required(self):
        from gcloud.datastore import _implicit_environ

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

        with self.assertRaises(ValueError):
            self._makeOne()
        with self.assertRaises(ValueError):
            self._makeOne(dataset_id=object())
        with self.assertRaises(ValueError):
            self._makeOne(connection=object())
Example #7
0
    def __init__(self, dataset_id=None, connection=None):
        self._connection = (connection or
                            _implicit_environ.get_default_connection())
        self._dataset_id = (dataset_id or
                            _implicit_environ.get_default_dataset_id())

        if self._connection is None or self._dataset_id is None:
            raise ValueError('A batch must have a connection and '
                             'a dataset ID set.')

        self._mutation = datastore_pb.Mutation()
        self._auto_id_entities = []
Example #8
0
def _validate_dataset_id(dataset_id, parent):
    """Ensure the dataset ID is set appropriately.

    If ``parent`` is passed, skip the test (it will be checked / fixed up
    later).

    If ``dataset_id`` is unset, attempt to infer the ID from the environment.

    :raises: :class:`ValueError` if ``dataset_id`` is ``None`` and no dataset
             can be inferred.
    """
    if parent is None:

        if dataset_id is None:

            dataset_id = _implicit_environ.get_default_dataset_id()
            if dataset_id is None:
                raise ValueError("A Key must have a dataset ID set.")

    return dataset_id
Example #9
0
    def __init__(self, dataset_id=None, connection=None):
        """Construct a batch.

        :type dataset_id: :class:`str`.
        :param dataset_id: The ID of the dataset.

        :type connection: :class:`gcloud.datastore.connection.Connection`
        :param connection: The connection used to connect to datastore.

        :raises: :class:`ValueError` if either a connection or dataset ID
                 are not set.
        """
        self._connection = (connection or
                            _implicit_environ.get_default_connection())
        self._dataset_id = (dataset_id or
                            _implicit_environ.get_default_dataset_id())

        if self._connection is None or self._dataset_id is None:
            raise ValueError('A batch must have a connection and '
                             'a dataset ID set.')

        self._mutation = datastore_pb.Mutation()
        self._auto_id_entities = []
Example #10
0
    def __init__(self, dataset_id=None, connection=None):
        """Construct a batch.

        :type dataset_id: :class:`str`.
        :param dataset_id: The ID of the dataset.

        :type connection: :class:`gcloud.datastore.connection.Connection`
        :param connection: The connection used to connect to datastore.

        :raises: :class:`ValueError` if either a connection or dataset ID
                 are not set.
        """
        self._connection = (connection
                            or _implicit_environ.get_default_connection())
        self._dataset_id = (dataset_id
                            or _implicit_environ.get_default_dataset_id())

        if self._connection is None or self._dataset_id is None:
            raise ValueError('A batch must have a connection and '
                             'a dataset ID set.')

        self._mutation = datastore_pb.Mutation()
        self._auto_id_entities = []
Example #11
0
    def __init__(self,
                 kind=None,
                 dataset_id=None,
                 namespace=None,
                 ancestor=None,
                 filters=(),
                 projection=(),
                 order=(),
                 group_by=()):

        if dataset_id is None:
            dataset_id = _implicit_environ.get_default_dataset_id()

        if dataset_id is None:
            raise ValueError("No dataset ID supplied, and no default set.")

        self._dataset_id = dataset_id
        self._kind = kind
        self._namespace = namespace
        self._ancestor = ancestor
        self._filters = list(filters)
        self._projection = list(projection)
        self._order = list(order)
        self._group_by = list(group_by)
Example #12
0
    def __init__(self,
                 kind=None,
                 dataset_id=None,
                 namespace=None,
                 ancestor=None,
                 filters=(),
                 projection=(),
                 order=(),
                 group_by=()):

        if dataset_id is None:
            dataset_id = _implicit_environ.get_default_dataset_id()

        if dataset_id is None:
            raise ValueError("No dataset ID supplied, and no default set.")

        self._dataset_id = dataset_id
        self._kind = kind
        self._namespace = namespace
        self._ancestor = ancestor
        self._filters = list(filters)
        self._projection = list(projection)
        self._order = list(order)
        self._group_by = list(group_by)
 def _callFUT(self):
     from gcloud.datastore._implicit_environ import get_default_dataset_id
     return get_default_dataset_id()
 def _callFUT(self):
     from gcloud.datastore._implicit_environ import get_default_dataset_id
     return get_default_dataset_id()