Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving storage iterator into core modules. #941

Merged
merged 1 commit into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions gcloud/storage/iterator.py → gcloud/iterator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 Google Inc. All rights reserved.
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -22,33 +22,33 @@
those results into an iterable of the actual objects you want::

class MyIterator(Iterator):
def get_items_from_response(self, response):
items = response.get('items', [])
for item in items:
my_item = MyItemClass(other_arg=True)
my_item._set_properties(item)
yield my_item
def get_items_from_response(self, response):
items = response.get('items', [])
for item in items:
my_item = MyItemClass(other_arg=True)
my_item._set_properties(item)
yield my_item

You then can use this to get **all** the results from a resource::

>>> iterator = MyIterator(...)
>>> list(iterator) # Convert to a list (consumes all values).
>>> iterator = MyIterator(...)
>>> list(iterator) # Convert to a list (consumes all values).

Or you can walk your way through items and call off the search early if
you find what you're looking for (resulting in possibly fewer
requests)::

>>> for item in MyIterator(...):
>>> print item.name
>>> if not item.is_valid:
>>> break
>>> for item in MyIterator(...):
>>> print item.name
>>> if not item.is_valid:
>>> break
"""


class Iterator(object):
"""A generic class for iterating through Cloud Storage list responses.
"""A generic class for iterating through Cloud JSON APIs list responses.

:type connection: :class:`gcloud.storage.connection.Connection`
:type connection: :class:`gcloud.connection.Connection`
:param connection: The connection to use to make requests.

:type path: string
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

from gcloud.exceptions import NotFound
from gcloud._helpers import get_default_project
from gcloud.iterator import Iterator
from gcloud.storage._helpers import _require_connection
from gcloud.storage.bucket import Bucket
from gcloud.storage.iterator import Iterator


def lookup_bucket(bucket_name, connection=None):
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

from gcloud._helpers import get_default_project
from gcloud.exceptions import NotFound
from gcloud.iterator import Iterator
from gcloud.storage._helpers import _PropertyMixin
from gcloud.storage._helpers import _require_connection
from gcloud.storage._helpers import _scalar_property
from gcloud.storage.acl import BucketACL
from gcloud.storage.acl import DefaultObjectACL
from gcloud.storage.iterator import Iterator
from gcloud.storage.blob import Blob
from gcloud._helpers import _RFC3339_MICROS

Expand Down
4 changes: 2 additions & 2 deletions gcloud/storage/test_iterator.py → gcloud/test_iterator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 Google Inc. All rights reserved.
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@
class TestIterator(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.storage.iterator import Iterator
from gcloud.iterator import Iterator
return Iterator

def _makeOne(self, *args, **kw):
Expand Down