From fbae6738c9276dc8af02b93c1cde9e20eddcd8ea Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Wed, 21 Dec 2016 00:58:18 +0200 Subject: [PATCH] utils.py: Add 'to_bytes' & 'format_bytes' methods --- tests/test_utils.py | 71 +++++++++++++++++++++++++++++++++++++++++ youtube_dl_gui/utils.py | 33 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tests/test_utils.py diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..4944090 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Contains test cases for the utils.py module.""" + +from __future__ import unicode_literals + +import sys +import os.path +import unittest + +PATH = os.path.realpath(os.path.abspath(__file__)) +sys.path.insert(0, os.path.dirname(os.path.dirname(PATH))) + +try: + from youtube_dl_gui import utils +except ImportError as error: + print error + sys.exit(1) + + +class TestToBytes(unittest.TestCase): + + """Test case for the to_bytes method.""" + + def test_to_bytes_bytes(self): + self.assertEqual(utils.to_bytes("596.00B"), 596.00) + self.assertEqual(utils.to_bytes("133.55B"), 133.55) + + def test_to_bytes_kilobytes(self): + self.assertEqual(utils.to_bytes("1.00KiB"), 1024.00) + self.assertEqual(utils.to_bytes("5.55KiB"), 5683.20) + + def test_to_bytes_megabytes(self): + self.assertEqual(utils.to_bytes("13.64MiB"), 14302576.64) + self.assertEqual(utils.to_bytes("1.00MiB"), 1048576.00) + + def test_to_bytes_gigabytes(self): + self.assertEqual(utils.to_bytes("1.00GiB"), 1073741824.00) + self.assertEqual(utils.to_bytes("1.55GiB"), 1664299827.20) + + def test_to_bytes_terabytes(self): + self.assertEqual(utils.to_bytes("1.00TiB"), 1099511627776.00) + + +class TestFormatBytes(unittest.TestCase): + + """Test case for the format_bytes method.""" + + def test_format_bytes_bytes(self): + self.assertEqual(utils.format_bytes(518.00), "518.00B") + + def test_format_bytes_kilobytes(self): + self.assertEqual(utils.format_bytes(1024.00), "1.00KiB") + + def test_format_bytes_megabytes(self): + self.assertEqual(utils.format_bytes(1048576.00), "1.00MiB") + + def test_format_bytes_gigabytes(self): + self.assertEqual(utils.format_bytes(1073741824.00), "1.00GiB") + + def test_format_bytes_terabytes(self): + self.assertEqual(utils.format_bytes(1099511627776.00), "1.00TiB") + + +def main(): + unittest.main() + + +if __name__ == "__main__": + main() diff --git a/youtube_dl_gui/utils.py b/youtube_dl_gui/utils.py index aef60da..69b5636 100644 --- a/youtube_dl_gui/utils.py +++ b/youtube_dl_gui/utils.py @@ -15,6 +15,7 @@ from __future__ import unicode_literals import os import sys import json +import math import locale import subprocess @@ -36,6 +37,11 @@ if os.name == 'nt': YOUTUBEDL_BIN += '.exe' +FILESIZE_METRICS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] + +KILO_SIZE = 1024.0 + + def get_encoding(): """Return system encoding. """ try: @@ -318,3 +324,30 @@ def get_pixmaps_dir(): return pixmaps_dir return None + + +def to_bytes(string): + """Convert given youtube-dl size string to bytes.""" + value = 0.0 + + for index, metric in enumerate(reversed(FILESIZE_METRICS)): + if metric in string: + value = float(string.split(metric)[0]) + break + + exponent = index * (-1) + (len(FILESIZE_METRICS) - 1) + + return round(value * (KILO_SIZE ** exponent), 2) + + +def format_bytes(bytes): + """Format bytes to youtube-dl size output strings.""" + if bytes == 0.0: + exponent = 0 + else: + exponent = int(math.log(bytes, KILO_SIZE)) + + suffix = FILESIZE_METRICS[exponent] + output_value = bytes / (KILO_SIZE ** exponent) + + return "%.2f%s" % (output_value, suffix)