summaryrefslogtreecommitdiff
path: root/swiftclient/utils.py
diff options
context:
space:
mode:
authorClay Gerrard <clay.gerrard@gmail.com>2019-07-02 11:23:22 -0500
committerTim Burke <tim.burke@gmail.com>2019-07-09 13:25:52 -0700
commit936631eac617c83bec1f1c44b1adcaa51d36329c (patch)
tree1e727d61a4392ca087742bd9c9cc238cf6c97fd6 /swiftclient/utils.py
parent4b3e33b3c28892616d42cf3f1497dd8db63c783b (diff)
downloadpython-swiftclient-936631eac617c83bec1f1c44b1adcaa51d36329c.tar.gz
Optionally display listings in raw json
Symlinks have recently added some new keys to container listings. It's very convenient to be able to see and reason about the extra information in container listings. Allowing raw json output is similar with what the client already does for the info command, and it's forward compatible with any listing enhancements added by future middleware development. Change-Id: I88fb38529342ac4e4198aeccd2f10c69c7396704
Diffstat (limited to 'swiftclient/utils.py')
-rw-r--r--swiftclient/utils.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/swiftclient/utils.py b/swiftclient/utils.py
index 2b208b9..9e43237 100644
--- a/swiftclient/utils.py
+++ b/swiftclient/utils.py
@@ -403,3 +403,25 @@ def normalize_manifest_path(path):
if path.startswith('/'):
return path[1:]
return path
+
+
+class JSONableIterable(list):
+ def __init__(self, iterable):
+ self._iterable = iter(iterable)
+ try:
+ self._peeked = next(self._iterable)
+ self._has_items = True
+ except StopIteration:
+ self._peeked = None
+ self._has_items = False
+
+ def __bool__(self):
+ return self._has_items
+
+ __nonzero__ = __bool__
+
+ def __iter__(self):
+ if self._has_items:
+ yield self._peeked
+ for item in self._iterable:
+ yield item