From d297b54c45271dbb6fd1aa0f3e6fd209220e1038 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 16 Nov 2016 19:32:17 -0300 Subject: [refactor] improve readability of stream producer --- .../src/leap/soledad/client/http_target/send_protocol.py | 14 ++++++-------- client/src/leap/soledad/client/http_target/support.py | 13 ++++++++----- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'client/src') 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): -- cgit v1.2.3