forked from fieldOfView/Cura-OctoPrintPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkReplyTimeout.py
37 lines (28 loc) · 1.1 KB
/
NetworkReplyTimeout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright (c) 2020 Aldo Hoeben / fieldOfView
# NetworkReplyTimeout is released under the terms of the AGPLv3 or higher.
from PyQt5.QtCore import QObject, QTimer
from PyQt5.QtNetwork import QNetworkReply
from UM.Signal import Signal
from typing import Optional, Callable
#
# A timer that is started when a QNetworkRequest returns a QNetworkReply, which closes the
# QNetworkReply does not reply in a timely manner
#
class NetworkReplyTimeout(QObject):
timeout = Signal()
def __init__(self, reply: QNetworkReply, timeout: int,
callback: Optional[Callable[[QNetworkReply], None]] = None) -> None:
super().__init__()
self._reply = reply
self._callback = callback
self._timer = QTimer()
self._timer.setInterval(timeout)
self._timer.setSingleShot(True)
self._timer.timeout.connect(self._onTimeout)
self._timer.start()
def _onTimeout(self):
if self._reply.isRunning():
self._reply.abort()
if self._callback:
self._callback(self._reply)
self.timeout.emit(self._reply)