def show_progress(): start = time() p = subprocess.Popen(['stty size'], shell=True, stdout=subprocess.PIPE) length, width = (int(i) for i in p.stdout.read().strip().split()) progress_bar_length = int(width * 0.5) p.stdout.close() eta = 0 speed = 0 while Tracker.downloaded < Tracker.total_size: # Get terminal dimensions. p = subprocess.Popen(['stty size'], shell=True, stdout=subprocess.PIPE) length, width = (int(i) for i in p.stdout.read().strip().split()) progress_bar_length = int(width * 0.5) p.stdout.close() speed = (Tracker.downloaded/1024.0)/(time() - start) percentage = (Tracker.downloaded / float(Tracker.total_size)) * 100.0 # Progress bar output. progress = COMPLETE_CHAR * int(percentage * progress_bar_length/100.0) # Pad progress bar. progress = progress.ljust(progress_bar_length, ' ') if speed > 0: eta = (Tracker.total_size - Tracker.downloaded) / (speed * 1024) else: speed = 0 helper.print_static(' %s%% [%s] %s KB/s %s ETA' % (format(percentage, '3.1f'), progress, format(speed, '3.1f'), helper.htime(eta)), width) sleep(1) # For the completion progress bar and statistics. if Tracker.total_size: speed = format((Tracker.downloaded / 1024.0)/(time() - start), '3.1f') speed = speed + ' ' + helper.get_size(float(speed), suffix_only=True) + '/s' helper.print_static(' %s%% [%s] %s' % ('100', COMPLETE_CHAR * progress_bar_length, speed), width) # Print out completion download info. print '\n\n%s (%s)' % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), speed), print '[%s/%s] in %s\n' % (Tracker.downloaded, Tracker.total_size, helper.htime(time() - start))
def test_htime(self): self.assertEqual(helper.htime(1234), '20m 34s') self.assertEqual(helper.htime(53), '53s') self.assertEqual(helper.htime(3720), '1h 2m 0s')