From d7af4f6ec115f9d587e88df9ba8acf167bfb9258 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Tue, 23 Jun 2015 12:22:55 -0700 Subject: [PATCH] Moving storage iterator into core modules. Fixes #90. --- gcloud/{storage => }/iterator.py | 30 +++++++++++++-------------- gcloud/storage/api.py | 2 +- gcloud/storage/bucket.py | 2 +- gcloud/{storage => }/test_iterator.py | 4 ++-- 4 files changed, 19 insertions(+), 19 deletions(-) rename gcloud/{storage => }/iterator.py (86%) rename gcloud/{storage => }/test_iterator.py (98%) diff --git a/gcloud/storage/iterator.py b/gcloud/iterator.py similarity index 86% rename from gcloud/storage/iterator.py rename to gcloud/iterator.py index ef65882988f2..0ec3082cfbf3 100644 --- a/gcloud/storage/iterator.py +++ b/gcloud/iterator.py @@ -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. @@ -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 diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index 1fdd6ad2e484..6155145499e3 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -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): diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 68dcad9032d7..be5dede07542 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -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 diff --git a/gcloud/storage/test_iterator.py b/gcloud/test_iterator.py similarity index 98% rename from gcloud/storage/test_iterator.py rename to gcloud/test_iterator.py index afe5d219ac08..83be77266703 100644 --- a/gcloud/storage/test_iterator.py +++ b/gcloud/test_iterator.py @@ -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. @@ -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):