def test_converting_bytes_to_human_readable(self): """ Tests that bytes are successfully converted to human readable format. :return: None """ self.assertEqual("1MB", human_readable_bytes(1000000)) self.assertEqual("1.024KB", human_readable_bytes(1024)) self.assertEqual("0.123KB", human_readable_bytes(123)) self.assertEqual("1.234GB", human_readable_bytes(1234123123))
def test_byte_conversion(self): """ Tests that byte strings can be parsed correctly and displayed in a human-readable format :return: None """ for string, count in [("1MB", 1000000), ("1.024KB", 1024), ("0.123KB", 123), ("1.234GB", 1234000000)]: self.assertEqual(string, human_readable_bytes(count)) self.assertEqual(byte_string_to_byte_count(string), count) # Test if human-readable strings are rounded correctly self.assertEqual("1.234GB", human_readable_bytes(1234123123))
def progress_printer(self): """ Prints the download progress Should run in a separate thread to avoid blocking up the IO which could lead to reduced download speeds :return: None """ speed_progress = [] while not self.downloading and not self.disconnected: pass self.logger.info("Progress Printer started") time.sleep(1) printing = self.downloading and not self.disconnected while printing: printing = self.downloading and not self.disconnected speed_progress.append({ "timestamp": time.time(), "progress": self.progress }) while len(speed_progress) > 0 \ and time.time() - speed_progress[0]["timestamp"] > 7: speed_progress.pop(0) if len(speed_progress) > 0: bytes_delta = self.progress - speed_progress[0]["progress"] time_delta = time.time() - speed_progress[0]["timestamp"] ratio = int(bytes_delta / time_delta) speed = human_readable_bytes(ratio) + "/s" else: speed = "0B/s" percentage = "%.2f" % (100 * (self.progress / self.filesize)) log_message = "[{}]: ({}%) |{}/{}| ({})".format( self.pack.filename, percentage, human_readable_bytes(self.progress, remove_trailing_zeroes=False), human_readable_bytes(self.filesize), speed) try: rows, _columns = check_output(['stty', 'size']).split() columns = int(_columns) except (ValueError, CalledProcessError): columns = 80 log_message = log_message[0:columns] pprint(log_message, end="\r", bg="lyellow", fg="black") time.sleep(0.1) self.logger.info("Progress Printer stopped")