[common] fix: human-readable sizes

# Conflicts:
#	bauh/commons/system.py
This commit is contained in:
Vinicius Moreira
2022-03-07 16:05:42 -03:00
parent ec0d0d6853
commit 9daa995d00
3 changed files with 68 additions and 12 deletions

View File

@@ -32,7 +32,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixes
- General
- not accepting blank root passwords [#235](https://github.com/vinifmor/bauh/issues/235)
- human-readable sizes (packages, files, updates, ...)
- UI
- some package icons would not appear if there is no URL associated with them

View File

@@ -22,8 +22,6 @@ if GLOBAL_PY_LIBS not in PATH:
USE_GLOBAL_INTERPRETER = bool(os.getenv('VIRTUAL_ENV'))
SIZE_MULTIPLIERS = ((0.001, 'Kb'), (0.000001, 'Mb'), (0.000000001, 'Gb'), (0.000000000001, 'Tb'))
def gen_env(global_interpreter: bool, lang: str = DEFAULT_LANG, extra_paths: Optional[Set[str]] = None) -> dict:
custom_env = dict(os.environ)
@@ -309,18 +307,42 @@ def get_dir_size(start_path='.'):
return total_size
def get_human_size_str(size) -> str:
int_size = int(size)
def get_human_size_str(size) -> Optional[str]:
if type(size) in (int, float, str):
int_size = int(size)
if int_size == 0:
return '0'
if int_size == 0:
return '0'
for m in SIZE_MULTIPLIERS:
size_str = str(int_size * m[0])
if size < 1024:
return f'{size} B'
if len(size_str.split('.')[0]) < 4:
return '{0:.2f}'.format(float(size_str)) + ' ' + m[1]
return str(int_size)
size_unit = size / 1024
if size_unit < 1024:
return f'{size_unit:.2f} Kb'
size_unit = size / 1048576
if size_unit < 1024:
return f'{size_unit:.2f} Mb'
size_unit = size / 1073741824
if size_unit < 1024:
return f'{size_unit:.2f} Gb'
size_unit = size / 1099511627776
if size_unit < 1024:
return f'{size_unit:.2f} Tb'
size_unit = size / 1125899906842624
if size_unit < 1024:
return f'{size_unit:.2f} Pb'
return str(int_size)
def run(cmd: List[str], success_code: int = 0, custom_user: Optional[str] = None) -> Tuple[bool, str]:

View File

@@ -0,0 +1,33 @@
from unittest import TestCase
from bauh.commons import system
class GetHumanSizeStr(TestCase):
def test__must_return_1Kb_for_1024(self):
self.assertEqual('1.00 Kb', system.get_human_size_str(1024))
def test__must_return_1_5Kb_for_1536(self):
self.assertEqual('1.50 Kb', system.get_human_size_str(1536))
def test__must_return_1_75Kb_for_1792(self):
self.assertEqual('1.75 Kb', system.get_human_size_str(1792))
def test__must_return_1Mb_for_1048576(self):
self.assertEqual('1.00 Mb', system.get_human_size_str(1048576))
def test__must_return_1_2Mb_for_1258291(self):
self.assertEqual('1.20 Mb', system.get_human_size_str(1258291))
def test__must_return_1_5Mb_for_1572864(self):
self.assertEqual('1.50 Mb', system.get_human_size_str(1572864))
def test__must_return_1Gb_for_1073741824(self):
self.assertEqual('1.00 Gb', system.get_human_size_str(1073741824))
def test__must_return_1Tb_for_1099511627776(self):
self.assertEqual('1.00 Tb', system.get_human_size_str(1099511627776))
def test__must_return_1Pb_for_1125899906842624(self):
self.assertEqual('1.00 Pb', system.get_human_size_str(1125899906842624))