X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b13876638cf57b400bd59513a0a1811b3d2993a1..26b510d785df7f548f41a11445c07df34b60328c:/tools/crunchstat-summary/crunchstat_summary/reader.py diff --git a/tools/crunchstat-summary/crunchstat_summary/reader.py b/tools/crunchstat-summary/crunchstat_summary/reader.py index a0a838d6c9..e8f0861be4 100644 --- a/tools/crunchstat-summary/crunchstat_summary/reader.py +++ b/tools/crunchstat-summary/crunchstat_summary/reader.py @@ -14,21 +14,32 @@ from crunchstat_summary import logger class CollectionReader(object): def __init__(self, collection_id): - logger.debug('load collection %s', collection_id) - collection = arvados.collection.CollectionReader(collection_id) + self._collection_id = collection_id + self._label = collection_id + self._reader = None + + def __str__(self): + return self._label + + def __iter__(self): + logger.debug('load collection %s', self._collection_id) + collection = arvados.collection.CollectionReader(self._collection_id) filenames = [filename for filename in collection] if len(filenames) == 1: filename = filenames[0] else: filename = 'crunchstat.txt' + self._label = "{}/{}".format(self._collection_id, filename) self._reader = collection.open(filename) - self._label = "{}/{}".format(collection_id, filename) + return iter(self._reader) - def __str__(self): - return self._label + def __enter__(self): + return self - def __iter__(self): - return iter(self._reader) + def __exit__(self, exc_type, exc_val, exc_tb): + if self._reader: + self._reader.close() + self._reader = None class LiveLogReader(object): @@ -83,3 +94,9 @@ class LiveLogReader(object): self._thread.join() raise StopIteration return line + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + pass