2017-09-13 21:46:39 +02:00
|
|
|
import logging
|
|
|
|
from twisted.internet import interfaces
|
|
|
|
from zope.interface import implements
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-09-26 06:13:47 +02:00
|
|
|
class HashBlobReader_v0(object):
|
2017-09-27 22:56:29 +02:00
|
|
|
"""
|
|
|
|
This is a class that is only used in StreamBlobDecryptor
|
|
|
|
and should be deprecated
|
|
|
|
"""
|
2017-09-13 21:46:39 +02:00
|
|
|
implements(interfaces.IConsumer)
|
|
|
|
|
|
|
|
def __init__(self, write_func):
|
|
|
|
self.write_func = write_func
|
|
|
|
|
|
|
|
def registerProducer(self, producer, streaming):
|
|
|
|
from twisted.internet import reactor
|
|
|
|
|
|
|
|
self.producer = producer
|
|
|
|
self.streaming = streaming
|
|
|
|
if self.streaming is False:
|
|
|
|
reactor.callLater(0, self.producer.resumeProducing)
|
|
|
|
|
|
|
|
def unregisterProducer(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
from twisted.internet import reactor
|
|
|
|
|
|
|
|
self.write_func(data)
|
|
|
|
if self.streaming is False:
|
|
|
|
reactor.callLater(0, self.producer.resumeProducing)
|
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-09-26 06:13:47 +02:00
|
|
|
def __init__(self, file_path, finished_cb):
|
|
|
|
self.finished_cb = finished_cb
|
|
|
|
self.finished_cb_d = None
|
|
|
|
self.read_handle = open(file_path, 'rb')
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
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)
|
|
|
|
|
|
|
|
|