[downloader, cleanup] Refactor report_progress

Closes #3790
This commit is contained in:
pukkandan 2022-05-22 21:52:26 +05:30
parent 3a85e9cee9
commit 11233f2afd
No known key found for this signature in database
GPG key ID: 7EEE9E1E817D0A39

View file

@ -21,10 +21,12 @@
error_to_compat_str, error_to_compat_str,
float_or_none, float_or_none,
format_bytes, format_bytes,
join_nonempty,
sanitize_open, sanitize_open,
shell_quote, shell_quote,
timeconvert, timeconvert,
timetuple_from_msec, timetuple_from_msec,
try_call,
) )
@ -110,6 +112,8 @@ def FD_NAME(cls):
@staticmethod @staticmethod
def format_seconds(seconds): def format_seconds(seconds):
if seconds is None:
return ' Unknown'
time = timetuple_from_msec(seconds * 1000) time = timetuple_from_msec(seconds * 1000)
if time.hours > 99: if time.hours > 99:
return '--:--:--' return '--:--:--'
@ -117,6 +121,8 @@ def format_seconds(seconds):
return '%02d:%02d' % time[1:-1] return '%02d:%02d' % time[1:-1]
return '%02d:%02d:%02d' % time[:-1] return '%02d:%02d:%02d' % time[:-1]
format_eta = format_seconds
@staticmethod @staticmethod
def calc_percent(byte_counter, data_len): def calc_percent(byte_counter, data_len):
if data_len is None: if data_len is None:
@ -125,11 +131,7 @@ def calc_percent(byte_counter, data_len):
@staticmethod @staticmethod
def format_percent(percent): def format_percent(percent):
if percent is None: return ' N/A%' if percent is None else f'{percent:>5.1f}%'
return '---.-%'
elif percent == 100:
return '100%'
return '%6s' % ('%3.1f%%' % percent)
@staticmethod @staticmethod
def calc_eta(start, now, total, current): def calc_eta(start, now, total, current):
@ -143,12 +145,6 @@ def calc_eta(start, now, total, current):
rate = float(current) / dif rate = float(current) / dif
return int((float(total) - float(current)) / rate) return int((float(total) - float(current)) / rate)
@staticmethod
def format_eta(eta):
if eta is None:
return '--:--'
return FileDownloader.format_seconds(eta)
@staticmethod @staticmethod
def calc_speed(start, now, bytes): def calc_speed(start, now, bytes):
dif = now - start dif = now - start
@ -158,13 +154,11 @@ def calc_speed(start, now, bytes):
@staticmethod @staticmethod
def format_speed(speed): def format_speed(speed):
if speed is None: return ' Unknown B/s' if speed is None else f'{format_bytes(speed):>10s}/s'
return '%10s' % '---b/s'
return '%10s' % ('%s/s' % format_bytes(speed))
@staticmethod @staticmethod
def format_retries(retries): def format_retries(retries):
return 'inf' if retries == float('inf') else '%.0f' % retries return 'inf' if retries == float('inf') else int(retries)
@staticmethod @staticmethod
def best_block_size(elapsed_time, bytes): def best_block_size(elapsed_time, bytes):
@ -332,63 +326,52 @@ def _format_progress(self, *args, **kwargs):
self._multiline.stream, self._multiline.allow_colors, *args, **kwargs) self._multiline.stream, self._multiline.allow_colors, *args, **kwargs)
def report_progress(self, s): def report_progress(self, s):
def with_fields(*tups, default=''):
for *fields, tmpl in tups:
if all(s.get(f) is not None for f in fields):
return tmpl
return default
if s['status'] == 'finished': if s['status'] == 'finished':
if self.params.get('noprogress'): if self.params.get('noprogress'):
self.to_screen('[download] Download completed') self.to_screen('[download] Download completed')
msg_template = '100%%' s.update({
if s.get('total_bytes') is not None: '_total_bytes_str': format_bytes(s.get('total_bytes')),
s['_total_bytes_str'] = format_bytes(s['total_bytes']) '_elapsed_str': self.format_seconds(s.get('elapsed')),
msg_template += ' of %(_total_bytes_str)s' '_percent_str': self.format_percent(100),
if s.get('elapsed') is not None: })
s['_elapsed_str'] = self.format_seconds(s['elapsed']) self._report_progress_status(s, join_nonempty(
msg_template += ' in %(_elapsed_str)s' '100%%',
s['_percent_str'] = self.format_percent(100) with_fields(('total_bytes', 'of %(_total_bytes_str)s')),
self._report_progress_status(s, msg_template) with_fields(('elapsed', 'in %(_elapsed_str)s')),
return delim=' '))
if s['status'] != 'downloading': if s['status'] != 'downloading':
return return
if s.get('eta') is not None: s.update({
s['_eta_str'] = self.format_eta(s['eta']) '_eta_str': self.format_eta(s.get('eta')),
else: '_speed_str': self.format_speed(s.get('speed')),
s['_eta_str'] = 'Unknown' '_percent_str': self.format_percent(try_call(
lambda: 100 * s['downloaded_bytes'] / s['total_bytes'],
lambda: 100 * s['downloaded_bytes'] / s['total_bytes_estimate'],
lambda: s['downloaded_bytes'] == 0 and 0)),
'_total_bytes_str': format_bytes(s.get('total_bytes')),
'_total_bytes_estimate_str': format_bytes(s.get('total_bytes_estimate')),
'_downloaded_bytes_str': format_bytes(s.get('downloaded_bytes')),
'_elapsed_str': self.format_seconds(s.get('elapsed')),
})
if s.get('total_bytes') and s.get('downloaded_bytes') is not None: msg_template = with_fields(
s['_percent_str'] = self.format_percent(100 * s['downloaded_bytes'] / s['total_bytes']) ('total_bytes', '%(_percent_str)s of %(_total_bytes_str)s at %(_speed_str)s ETA %(_eta_str)s'),
elif s.get('total_bytes_estimate') and s.get('downloaded_bytes') is not None: ('total_bytes_estimate', '%(_percent_str)s of ~%(_total_bytes_estimate_str)s at %(_speed_str)s ETA %(_eta_str)s'),
s['_percent_str'] = self.format_percent(100 * s['downloaded_bytes'] / s['total_bytes_estimate']) ('downloaded_bytes', 'elapsed', '%(_downloaded_bytes_str)s at %(_speed_str)s (%(_elapsed_str)s)'),
else: ('downloaded_bytes', '%(_downloaded_bytes_str)s at %(_speed_str)s'),
if s.get('downloaded_bytes') == 0: default='%(_percent_str)s at %(_speed_str)s ETA %(_eta_str)s')
s['_percent_str'] = self.format_percent(0)
else:
s['_percent_str'] = 'Unknown %'
if s.get('speed') is not None: msg_template += with_fields(
s['_speed_str'] = self.format_speed(s['speed']) ('fragment_index', 'fragment_count', ' (frag %(fragment_index)s/%(fragment_count)s)'),
else: ('fragment_index', ' (frag %(fragment_index)s)'))
s['_speed_str'] = 'Unknown speed'
if s.get('total_bytes') is not None:
s['_total_bytes_str'] = format_bytes(s['total_bytes'])
msg_template = '%(_percent_str)s of %(_total_bytes_str)s at %(_speed_str)s ETA %(_eta_str)s'
elif s.get('total_bytes_estimate') is not None:
s['_total_bytes_estimate_str'] = format_bytes(s['total_bytes_estimate'])
msg_template = '%(_percent_str)s of ~%(_total_bytes_estimate_str)s at %(_speed_str)s ETA %(_eta_str)s'
else:
if s.get('downloaded_bytes') is not None:
s['_downloaded_bytes_str'] = format_bytes(s['downloaded_bytes'])
if s.get('elapsed'):
s['_elapsed_str'] = self.format_seconds(s['elapsed'])
msg_template = '%(_downloaded_bytes_str)s at %(_speed_str)s (%(_elapsed_str)s)'
else:
msg_template = '%(_downloaded_bytes_str)s at %(_speed_str)s'
else:
msg_template = '%(_percent_str)s at %(_speed_str)s ETA %(_eta_str)s'
if s.get('fragment_index') and s.get('fragment_count'):
msg_template += ' (frag %(fragment_index)s/%(fragment_count)s)'
elif s.get('fragment_index'):
msg_template += ' (frag %(fragment_index)s)'
self._report_progress_status(s, msg_template) self._report_progress_status(s, msg_template)
def report_resuming_byte(self, resume_len): def report_resuming_byte(self, resume_len):