'Get the next batch.
The batch returned by this function cannot be used to fetch the next batch
(through Batch.next_batch()). Instead this function will always return None.
To retrieve the next batch use .next() or .next_batch(N).
This function may return a batch larger than min_to_fetch, but will never
return smaller unless there are no more results.
Special values can be used for min_batch_size:
ASYNC_ONLY - Do not perform any synchrounous fetches from the datastore
even if the this produces a batch with no results.
AT_LEAST_OFFSET - Only pull enough results to satifiy the offset.
AT_LEAST_ONE - Pull batches until at least one result is returned.
Args:
min_batch_size: The minimum number of results to retrieve or one of
(ASYNC_ONLY, AT_LEAST_OFFSET, AT_LEAST_ONE)
Returns:
The next Batch of results.'

def next_batch(self, min_batch_size):

    if (min_batch_size in (Batcher.ASYNC_ONLY, Batcher.AT_LEAST_OFFSET, Batcher.AT_LEAST_ONE)):
        exact = False
    else:
        exact = True
        datastore_types.ValidateInteger(min_batch_size, 'min_batch_size', datastore_errors.BadArgumentError)
    if (not self.__next_batch):
        raise StopIteration
    batch = self.__next_batch.get_result()
    self.__next_batch = None
    self.__skipped_results += batch.skipped_results
    if (min_batch_size is not Batcher.ASYNC_ONLY):
        if (min_batch_size is Batcher.AT_LEAST_ONE):
            min_batch_size = 1
        needed_results = (min_batch_size - len(batch.results))
        while (batch.more_results and ((self.__skipped_results < self.__initial_offset) or (needed_results > 0))):
            if batch.query_options.batch_size:
                batch_size = max(batch.query_options.batch_size, needed_results)
            elif exact:
                batch_size = needed_results
            else:
                batch_size = None
            self.__next_batch = batch.next_batch_async(FetchOptions(offset=max(0, (self.__initial_offset - self.__skipped_results)), batch_size=batch_size))
            next_batch = self.__next_batch.get_result()
            self.__next_batch = None
            self.__skipped_results += next_batch.skipped_results
            needed_results = max(0, (needed_results - len(next_batch.results)))
            batch._extend(next_batch)
    self.__next_batch = batch.next_batch_async()
    return batch
