summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Shyba <victor1984@riseup.net>2016-11-16 19:32:17 -0300
committerVictor Shyba <victor1984@riseup.net>2016-11-18 15:55:52 -0300
commit12ec7596730e1856099d6ce2a3dab9cde256b093 (patch)
tree7c85f665cb31b245f66ec380bc533d0c21bccb11
parent7f9e1a322e48567a03af2c9feba7ec54f4701abd (diff)
[refactor] improve readability of stream producer
-rw-r--r--client/src/leap/soledad/client/http_target/send_protocol.py14
-rw-r--r--client/src/leap/soledad/client/http_target/support.py13
2 files changed, 14 insertions, 13 deletions
diff --git a/client/src/leap/soledad/client/http_target/send_protocol.py b/client/src/leap/soledad/client/http_target/send_protocol.py
index 9980309a..63ce6b42 100644
--- a/client/src/leap/soledad/client/http_target/send_protocol.py
+++ b/client/src/leap/soledad/client/http_target/send_protocol.py
@@ -28,14 +28,14 @@ class DocStreamProducer(object):
implements(IBodyProducer)
- def __init__(self, parser_producer):
+ def __init__(self, producer):
"""
Initialize the string produer.
- :param body: The body of the request.
- :type body: str
+ :param producer: A RequestBody instance and a list of producer calls
+ :type producer: (.support.RequestBody, [(function, *args)])
"""
- self.body, self.producer = parser_producer
+ self.body, self.producer = producer
self.length = UNKNOWN_LENGTH
self.pause = False
self.stop = False
@@ -51,16 +51,14 @@ class DocStreamProducer(object):
:return: A Deferred that fires when production ends.
:rtype: twisted.internet.defer.Deferred
"""
- call = self.producer.pop(0)
- yield call[0](*call[1:])
while self.producer and not self.stop:
if self.pause:
yield self.sleep(0.001)
continue
call = self.producer.pop(0)
yield call[0](*call[1:])
- consumer.write(self.body.pop(1))
- consumer.write(self.body.pop(1))
+ consumer.write(self.body.pop(1, leave_open=True))
+ consumer.write(self.body.pop(0)) # close stream
def sleep(self, secs):
d = defer.Deferred()
diff --git a/client/src/leap/soledad/client/http_target/support.py b/client/src/leap/soledad/client/http_target/support.py
index feb306e8..19e07838 100644
--- a/client/src/leap/soledad/client/http_target/support.py
+++ b/client/src/leap/soledad/client/http_target/support.py
@@ -178,13 +178,16 @@ class RequestBody(object):
entry = json.dumps(entry_dict)
self.entries.append(entry)
- def pop(self, amount=10):
+ def pop(self, amount=10, leave_open=False):
"""
- Removes all entries and returns it formatted and ready
+ Removes entries and returns it formatted and ready
to be sent.
- :param number: number of entries to pop and format
- :type number: int
+ :param amount: number of entries to pop and format
+ :type amount: int
+
+ :param leave_open: flag to skip stream closing
+ :type amount: bool
:return: formatted body ready to be sent
:rtype: str
@@ -193,7 +196,7 @@ class RequestBody(object):
amount = min([len(self.entries), amount])
entries = [self.entries.pop(0) for i in xrange(amount)]
self.consumed += amount
- end = len(self.entries) == 0
+ end = len(self.entries) == 0 if not leave_open else False
return self.entries_to_str(entries, start, end)
def __str__(self):