Cleanup post_reply cleanup code

CURA-4398

- Create 2 separate try-except blocks for disconnect()s
- Remove duplicated post_reply cleanup code by creating a function to do
  so
This commit is contained in:
Lipu Fei 2017-10-05 14:51:12 +02:00
parent 82805fefe4
commit 18e636f928

View file

@ -480,19 +480,9 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
# Check if we were uploading something. Abort if this is the case.
# Some operating systems handle this themselves, others give weird issues.
try:
if self._post_reply:
Logger.log("d", "Stopping post upload because the connection was lost.")
try:
self._post_reply.uploadProgress.disconnect(self._onUploadProgress)
self._post_reply.finished.disconnect(self._onUploadFinished)
except TypeError:
pass # The disconnection can fail on mac in some cases. Ignore that.
self._post_reply.abort()
self._post_reply = None
except RuntimeError:
self._post_reply = None # It can happen that the wrapped c++ object is already deleted.
if self._post_reply:
Logger.log("d", "Stopping post upload because the connection was lost.")
self._finalizePostReply()
return
else:
if not self._connection_state_before_timeout:
@ -513,19 +503,9 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
# Check if we were uploading something. Abort if this is the case.
# Some operating systems handle this themselves, others give weird issues.
try:
if self._post_reply:
Logger.log("d", "Stopping post upload because the connection was lost.")
try:
self._post_reply.uploadProgress.disconnect(self._onUploadProgress)
self._post_reply.finished.disconnect(self._onUploadFinished)
except TypeError:
pass # The disconnection can fail on mac in some cases. Ignore that.
self._post_reply.abort()
self._post_reply = None
except RuntimeError:
self._post_reply = None # It can happen that the wrapped c++ object is already deleted.
if self._post_reply:
Logger.log("d", "Stopping post upload because the connection was lost.")
self._finalizePostReply()
self.setConnectionState(ConnectionState.error)
return
@ -546,6 +526,26 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._last_request_time = time()
def _finalizePostReply(self):
if self._post_reply is None:
return
try:
try:
self._post_reply.uploadProgress.disconnect(self._onUploadProgress)
except TypeError:
pass # The disconnection can fail on mac in some cases. Ignore that.
try:
self._post_reply.finished.disconnect(self._onUploadFinished)
except TypeError:
pass # The disconnection can fail on mac in some cases. Ignore that.
self._post_reply.abort()
self._post_reply = None
except RuntimeError:
self._post_reply = None # It can happen that the wrapped c++ object is already deleted.
def _createNetworkManager(self):
if self._manager:
self._manager.finished.disconnect(self._onFinished)
@ -847,8 +847,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._progress_message.hide()
self._compressing_print = False
if self._post_reply:
self._post_reply.abort()
self._post_reply = None
self._finalizePostReply()
Application.getInstance().showPrintMonitor.emit(False)
## Attempt to start a new print.
@ -996,11 +995,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
# Check if we were uploading something. Abort if this is the case.
# Some operating systems handle this themselves, others give weird issues.
if self._post_reply:
self._post_reply.abort()
self._post_reply.uploadProgress.disconnect(self._onUploadProgress)
self._post_reply.finished.disconnect(self._onUploadFinished)
self._finalizePostReply()
Logger.log("d", "Uploading of print failed after %s", time() - self._send_gcode_start)
self._post_reply = None
self._progress_message.hide()
self.setConnectionState(ConnectionState.error)