| 1 |
# Copyright (c) 2014 Rackspace |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
|
| 17 |
class _Iterator: |
| 18 |
"""Base Iterator |
| 19 |
|
| 20 |
This iterator is not meant to be used outside |
| 21 |
the scope of this package. The iterator gets |
| 22 |
a dictionary as returned by a listing endpoint. |
| 23 |
|
| 24 |
Subclasses of this base class determine the key |
| 25 |
to iterate over, as well as the means of creating |
| 26 |
the objects contained within. |
| 27 |
|
| 28 |
If there are no objects left to return, the iterator |
| 29 |
will try to load more by following the `next` rel link |
| 30 |
type. |
| 31 |
|
| 32 |
The iterator raises a StopIteration exception if the server |
| 33 |
doesn't return more objects after a `next-page` call. |
| 34 |
|
| 35 |
:param client: The client instance used by the queue |
| 36 |
:type client: `v2.Client` |
| 37 |
:param listing_response: Response returned by the listing call |
| 38 |
:type listing_response: Dict |
| 39 |
""" |
| 40 |
def __init__(self, client, listing_response, iter_key, create_function): |
| 41 |
self._client = client |
| 42 |
self._iter_key = iter_key |
| 43 |
self._create_function = create_function |
| 44 |
|
| 45 |
self._links = [] |
| 46 |
self._stream = False |
| 47 |
self._listing_response = listing_response |
| 48 |
|
| 49 |
# NOTE(flaper87): Simple hack to |
| 50 |
# re-use the iterator for get_many_messages |
| 51 |
# and message listing. |
| 52 |
if isinstance(listing_response, dict): |
| 53 |
self._links = listing_response.get('links', []) |
| 54 |
self._listing_response = listing_response[self._iter_key] |
| 55 |
|
| 56 |
def __iter__(self): |
| 57 |
return self |
| 58 |
|
| 59 |
def get_iterables(self, iterables): |
| 60 |
self._links = iterables['links'] |
| 61 |
self._listing_response = iterables[self._iter_key] |
| 62 |
|
| 63 |
def stream(self, enabled=True): |
| 64 |
"""Make this `_Iterator` a stream iterator. |
| 65 |
|
| 66 |
Since `_Iterator`'s default is to *not* stream, |
| 67 |
this method's default value is to *stream* data |
| 68 |
from the server. That is, unless explicitly specified |
| 69 |
this method will enable make this iterator a stream |
| 70 |
iterator. |
| 71 |
|
| 72 |
:param enabled: Whether streaming should be |
| 73 |
enabled or not. |
| 74 |
:type enabled: bool |
| 75 |
""" |
| 76 |
self._stream = enabled |
| 77 |
return self |
| 78 |
|
| 79 |
def _next_page(self): |
| 80 |
for link in self._links: |
| 81 |
if link['rel'] == 'next': |
| 82 |
# NOTE(flaper87): We already have the |
| 83 |
# ref for the next set of messages, lets |
| 84 |
# just follow it. |
| 85 |
iterables = self._client.follow(link['href']) |
| 86 |
|
| 87 |
# NOTE(flaper87): Since we're using |
| 88 |
# `.follow`, the empty result will |
| 89 |
# be None. Consider making the API |
| 90 |
# return an empty dict for consistency. |
| 91 |
if iterables: |
| 92 |
self.get_iterables(iterables) |
| 93 |
return |
| 94 |
raise StopIteration |
| 95 |
|
| 96 |
def __next__(self): |
| 97 |
try: |
| 98 |
args = self._listing_response.pop(0) |
| 99 |
except IndexError: |
| 100 |
if not self._stream: |
| 101 |
raise StopIteration |
| 102 |
|
| 103 |
self._next_page() |
| 104 |
return self.next() |
| 105 |
|
| 106 |
return self._create_function(args) |
| 107 |
|
| 108 |
# NOTE(flaper87): Py2K support |
| 109 |
next = __next__ |