2017-09-13 21:46:39 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-09-26 06:13:47 +02:00
|
|
|
class HashBlobReader(object):
|
2017-09-27 22:56:29 +02:00
|
|
|
"""
|
|
|
|
This is a file like reader class that supports
|
|
|
|
read(size) and close()
|
|
|
|
"""
|
2017-10-03 00:07:07 +02:00
|
|
|
def __init__(self, read_handle, finished_cb):
|
2017-09-26 06:13:47 +02:00
|
|
|
self.finished_cb = finished_cb
|
|
|
|
self.finished_cb_d = None
|
2017-10-03 00:07:07 +02:00
|
|
|
self.read_handle = read_handle
|
2017-09-26 06:13:47 +02:00
|
|
|
|
|
|
|
def __del__(self):
|
2017-09-29 20:29:35 +02:00
|
|
|
if self.finished_cb_d is None:
|
|
|
|
log.warn("Garbage collection was called, but reader for %s was not closed yet",
|
2017-10-03 00:07:07 +02:00
|
|
|
self.read_handle.name)
|
2017-09-26 06:13:47 +02:00
|
|
|
self.close()
|
|
|
|
|
|
|
|
def read(self, size=-1):
|
|
|
|
return self.read_handle.read(size)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
# if we've already closed and called finished_cb, do nothing
|
|
|
|
if self.finished_cb_d is not None:
|
|
|
|
return
|
|
|
|
self.read_handle.close()
|
|
|
|
self.finished_cb_d = self.finished_cb(self)
|
|
|
|
|
|
|
|
|