Make some things in downloads.py private
This commit is contained in:
parent
4a2f0eea2f
commit
36b7485262
@ -48,9 +48,10 @@ ModelRole = usertypes.enum('ModelRole', ['item'], start=Qt.UserRole,
|
|||||||
is_int=True)
|
is_int=True)
|
||||||
|
|
||||||
|
|
||||||
RetryInfo = collections.namedtuple('RetryInfo', ['request', 'manager'])
|
_RetryInfo = collections.namedtuple('_RetryInfo', ['request', 'manager'])
|
||||||
|
|
||||||
DownloadPath = collections.namedtuple('DownloadPath', ['filename', 'question'])
|
_DownloadPath = collections.namedtuple('_DownloadPath', ['filename',
|
||||||
|
'question'])
|
||||||
|
|
||||||
# Remember the last used directory
|
# Remember the last used directory
|
||||||
last_used_directory = None
|
last_used_directory = None
|
||||||
@ -58,7 +59,7 @@ last_used_directory = None
|
|||||||
|
|
||||||
# All REFRESH_INTERVAL milliseconds, speeds will be recalculated and downloads
|
# All REFRESH_INTERVAL milliseconds, speeds will be recalculated and downloads
|
||||||
# redrawn.
|
# redrawn.
|
||||||
REFRESH_INTERVAL = 500
|
_REFRESH_INTERVAL = 500
|
||||||
|
|
||||||
|
|
||||||
def download_dir():
|
def download_dir():
|
||||||
@ -74,7 +75,7 @@ def download_dir():
|
|||||||
return directory
|
return directory
|
||||||
|
|
||||||
|
|
||||||
def path_suggestion(filename):
|
def _path_suggestion(filename):
|
||||||
"""Get the suggested file path.
|
"""Get the suggested file path.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -143,7 +144,7 @@ def ask_for_filename(suggested_filename, win_id, *, parent=None,
|
|||||||
'prompt-download-directory')
|
'prompt-download-directory')
|
||||||
|
|
||||||
if not prompt_download_directory:
|
if not prompt_download_directory:
|
||||||
return DownloadPath(filename=download_dir(), question=None)
|
return _DownloadPath(filename=download_dir(), question=None)
|
||||||
|
|
||||||
encoding = sys.getfilesystemencoding()
|
encoding = sys.getfilesystemencoding()
|
||||||
suggested_filename = utils.force_encoding(suggested_filename, encoding)
|
suggested_filename = utils.force_encoding(suggested_filename, encoding)
|
||||||
@ -152,12 +153,12 @@ def ask_for_filename(suggested_filename, win_id, *, parent=None,
|
|||||||
q.text = "Save file to:"
|
q.text = "Save file to:"
|
||||||
q.mode = usertypes.PromptMode.text
|
q.mode = usertypes.PromptMode.text
|
||||||
q.completed.connect(q.deleteLater)
|
q.completed.connect(q.deleteLater)
|
||||||
q.default = path_suggestion(suggested_filename)
|
q.default = _path_suggestion(suggested_filename)
|
||||||
|
|
||||||
message_bridge = objreg.get('message-bridge', scope='window',
|
message_bridge = objreg.get('message-bridge', scope='window',
|
||||||
window=win_id)
|
window=win_id)
|
||||||
q.ask = lambda: message_bridge.ask(q, blocking=False)
|
q.ask = lambda: message_bridge.ask(q, blocking=False)
|
||||||
return DownloadPath(filename=None, question=q)
|
return _DownloadPath(filename=None, question=q)
|
||||||
|
|
||||||
|
|
||||||
class DownloadItemStats(QObject):
|
class DownloadItemStats(QObject):
|
||||||
@ -185,20 +186,20 @@ class DownloadItemStats(QObject):
|
|||||||
self.done = 0
|
self.done = 0
|
||||||
self.speed = 0
|
self.speed = 0
|
||||||
self._last_done = 0
|
self._last_done = 0
|
||||||
samples = int(self.SPEED_AVG_WINDOW * (1000 / REFRESH_INTERVAL))
|
samples = int(self.SPEED_AVG_WINDOW * (1000 / _REFRESH_INTERVAL))
|
||||||
self._speed_avg = collections.deque(maxlen=samples)
|
self._speed_avg = collections.deque(maxlen=samples)
|
||||||
|
|
||||||
def update_speed(self):
|
def update_speed(self):
|
||||||
"""Recalculate the current download speed.
|
"""Recalculate the current download speed.
|
||||||
|
|
||||||
The caller needs to guarantee this is called all REFRESH_INTERVAL ms.
|
The caller needs to guarantee this is called all _REFRESH_INTERVAL ms.
|
||||||
"""
|
"""
|
||||||
if self.done is None:
|
if self.done is None:
|
||||||
# this can happen for very fast downloads, e.g. when actually
|
# this can happen for very fast downloads, e.g. when actually
|
||||||
# opening a file
|
# opening a file
|
||||||
return
|
return
|
||||||
delta = self.done - self._last_done
|
delta = self.done - self._last_done
|
||||||
self.speed = delta * 1000 / REFRESH_INTERVAL
|
self.speed = delta * 1000 / _REFRESH_INTERVAL
|
||||||
self._speed_avg.append(self.speed)
|
self._speed_avg.append(self.speed)
|
||||||
self._last_done = self.done
|
self._last_done = self.done
|
||||||
|
|
||||||
@ -260,7 +261,7 @@ class DownloadItem(QObject):
|
|||||||
readyRead will write to the real file object.
|
readyRead will write to the real file object.
|
||||||
|
|
||||||
Class attributes:
|
Class attributes:
|
||||||
MAX_REDIRECTS: The maximum redirection count.
|
_MAX_REDIRECTS: The maximum redirection count.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
done: Whether the download is finished.
|
done: Whether the download is finished.
|
||||||
@ -272,7 +273,7 @@ class DownloadItem(QObject):
|
|||||||
done.
|
done.
|
||||||
fileobj: The file object to download the file to.
|
fileobj: The file object to download the file to.
|
||||||
reply: The QNetworkReply associated with this download.
|
reply: The QNetworkReply associated with this download.
|
||||||
retry_info: A RetryInfo instance.
|
retry_info: A _RetryInfo instance.
|
||||||
raw_headers: The headers sent by the server.
|
raw_headers: The headers sent by the server.
|
||||||
_filename: The filename of the download.
|
_filename: The filename of the download.
|
||||||
_redirects: How many time we were redirected already.
|
_redirects: How many time we were redirected already.
|
||||||
@ -298,7 +299,7 @@ class DownloadItem(QObject):
|
|||||||
requested.
|
requested.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
MAX_REDIRECTS = 10
|
_MAX_REDIRECTS = 10
|
||||||
data_changed = pyqtSignal()
|
data_changed = pyqtSignal()
|
||||||
finished = pyqtSignal()
|
finished = pyqtSignal()
|
||||||
error = pyqtSignal(str)
|
error = pyqtSignal(str)
|
||||||
@ -746,7 +747,7 @@ class DownloadItem(QObject):
|
|||||||
if new_url == request.url():
|
if new_url == request.url():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self._redirects > self.MAX_REDIRECTS:
|
if self._redirects > self._MAX_REDIRECTS:
|
||||||
self._die("Maximum redirection count reached!")
|
self._die("Maximum redirection count reached!")
|
||||||
self.delete()
|
self.delete()
|
||||||
return True # so on_reply_finished aborts
|
return True # so on_reply_finished aborts
|
||||||
@ -801,7 +802,7 @@ class DownloadManager(QObject):
|
|||||||
win_id, None, self)
|
win_id, None, self)
|
||||||
self._update_timer = usertypes.Timer(self, 'download-update')
|
self._update_timer = usertypes.Timer(self, 'download-update')
|
||||||
self._update_timer.timeout.connect(self.update_gui)
|
self._update_timer.timeout.connect(self.update_gui)
|
||||||
self._update_timer.setInterval(REFRESH_INTERVAL)
|
self._update_timer.setInterval(_REFRESH_INTERVAL)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return utils.get_repr(self, downloads=len(self.downloads))
|
return utils.get_repr(self, downloads=len(self.downloads))
|
||||||
|
Loading…
Reference in New Issue
Block a user