Skip to content

Commit

Permalink
Moving storage iterator into core modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jun 23, 2015
1 parent 85c7e37 commit d7af4f6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
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

0 comments on commit d7af4f6

Please sign in to comment.