From e1921facf8681c2d1f555d542669e7dcf5c886ec Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Mon, 14 Mar 2022 09:46:17 -0300 Subject: [PATCH] [common] refactoring: 'get_human_size_str' moved from 'system' to 'view_utils' --- bauh/api/http.py | 3 ++- bauh/commons/system.py | 19 ------------------ bauh/commons/view_utils.py | 20 +++++++++++++++++++ bauh/gems/arch/confirmation.py | 2 +- bauh/gems/debian/controller.py | 3 ++- bauh/gems/debian/gui.py | 2 +- bauh/gems/snap/controller.py | 4 ++-- bauh/gems/web/controller.py | 3 ++- bauh/view/core/downloader.py | 3 ++- bauh/view/qt/thread.py | 3 ++- tests/common/test_system.py | 35 --------------------------------- tests/common/test_view_utils.py | 35 +++++++++++++++++++++++++++++++++ 12 files changed, 69 insertions(+), 63 deletions(-) delete mode 100644 tests/common/test_system.py create mode 100644 tests/common/test_view_utils.py diff --git a/bauh/api/http.py b/bauh/api/http.py index 89f1265b..ba4406e3 100644 --- a/bauh/api/http.py +++ b/bauh/api/http.py @@ -7,6 +7,7 @@ import requests import yaml from bauh.commons import system +from bauh.commons.view_utils import get_human_size_str class HttpClient: @@ -102,7 +103,7 @@ class HttpClient: size = self.get_content_length_in_bytes(url, session) if size: - return system.get_human_size_str(size) + return get_human_size_str(size) def exists(self, url: str, session: bool = True, timeout: int = 5) -> bool: params = {'url': url, 'allow_redirects': True, 'verify': False, 'timeout': timeout} diff --git a/bauh/commons/system.py b/bauh/commons/system.py index 7ef20b92..f9513b54 100644 --- a/bauh/commons/system.py +++ b/bauh/commons/system.py @@ -22,9 +22,6 @@ if GLOBAL_PY_LIBS not in PATH: USE_GLOBAL_INTERPRETER = bool(os.getenv('VIRTUAL_ENV')) -SIZE_UNITS = ((1, 'B'), (1024, 'Kb'), (1048576, 'Mb'), (1073741824, 'Gb'), - (1099511627776, 'Tb'), (1125899906842624, 'Pb')) - def gen_env(global_interpreter: bool, lang: str = DEFAULT_LANG, extra_paths: Optional[Set[str]] = None) -> dict: custom_env = dict(os.environ) @@ -314,22 +311,6 @@ def get_dir_size(start_path='.'): return total_size -def get_human_size_str(size) -> Optional[str]: - if type(size) in (int, float, str): - int_size = abs(int(size)) - - if int_size == 0: - return '0' - - for div, unit in SIZE_UNITS: - - size_unit = int_size / div - - if size_unit < 1024: - size_unit = size_unit if size > 0 else size_unit * -1 - return f'{int(size_unit)} {unit}' if unit == 'B' else f'{size_unit:.2f} {unit}' - - def run(cmd: List[str], success_code: int = 0, custom_user: Optional[str] = None) -> Tuple[bool, str]: final_cmd = ['runuser', '-u', custom_user, '--', *cmd] if custom_user else cmd p = subprocess.run(final_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.DEVNULL) diff --git a/bauh/commons/view_utils.py b/bauh/commons/view_utils.py index 11611bd0..1ade21ae 100644 --- a/bauh/commons/view_utils.py +++ b/bauh/commons/view_utils.py @@ -3,6 +3,10 @@ from typing import List, Tuple, Optional from bauh.api.abstract.view import SelectViewType, InputOption, SingleSelectComponent +SIZE_UNITS = ((1, 'B'), (1024, 'Kb'), (1048576, 'Mb'), (1073741824, 'Gb'), + (1099511627776, 'Tb'), (1125899906842624, 'Pb')) + + def new_select(label: str, tip: str, id_: str, opts: List[Tuple[Optional[str], object, Optional[str]]], value: object, max_width: int, type_: SelectViewType = SelectViewType.RADIO, capitalize_label: bool = True): inp_opts = [InputOption(label=o[0].capitalize(), value=o[1], tooltip=o[2]) for o in opts] @@ -16,3 +20,19 @@ def new_select(label: str, tip: str, id_: str, opts: List[Tuple[Optional[str], o type_=type_, id_=id_, capitalize_label=capitalize_label) + + +def get_human_size_str(size) -> Optional[str]: + if type(size) in (int, float, str): + int_size = abs(int(size)) + + if int_size == 0: + return '0' + + for div, unit in SIZE_UNITS: + + size_unit = int_size / div + + if size_unit < 1024: + size_unit = size_unit if size > 0 else size_unit * -1 + return f'{int(size_unit)} {unit}' if unit == 'B' else f'{size_unit:.2f} {unit}' diff --git a/bauh/gems/arch/confirmation.py b/bauh/gems/arch/confirmation.py index a8d03f08..b461f999 100644 --- a/bauh/gems/arch/confirmation.py +++ b/bauh/gems/arch/confirmation.py @@ -5,7 +5,7 @@ from bauh.api.abstract.view import MultipleSelectComponent, InputOption, FormCom SelectViewType from bauh.commons import resource from bauh.commons.html import bold -from bauh.commons.system import get_human_size_str +from bauh.commons.view_utils import get_human_size_str from bauh.gems.arch import ROOT_DIR, get_repo_icon_path, get_icon_path, pacman from bauh.view.util.translation import I18n diff --git a/bauh/gems/debian/controller.py b/bauh/gems/debian/controller.py index c2b5ded4..5c6df34d 100644 --- a/bauh/gems/debian/controller.py +++ b/bauh/gems/debian/controller.py @@ -19,8 +19,9 @@ from bauh.api.abstract.view import ViewComponent, TextInputComponent, PanelCompo SingleSelectComponent, InputOption, SelectViewType from bauh.api.paths import CONFIG_DIR from bauh.commons.html import bold -from bauh.commons.system import get_human_size_str, ProcessHandler +from bauh.commons.system import ProcessHandler from bauh.commons.util import NullLoggerFactory +from bauh.commons.view_utils import get_human_size_str from bauh.gems.debian import DEBIAN_ICON_PATH from bauh.gems.debian.aptitude import Aptitude, AptitudeOutputHandlerFactory, AptitudeAction from bauh.gems.debian.common import fill_show_data diff --git a/bauh/gems/debian/gui.py b/bauh/gems/debian/gui.py index aa279301..6c131649 100644 --- a/bauh/gems/debian/gui.py +++ b/bauh/gems/debian/gui.py @@ -4,7 +4,7 @@ from typing import Collection, Optional, Tuple, List from bauh.api.abstract.handler import ProcessWatcher from bauh.api.abstract.view import InputOption, MultipleSelectComponent, TextComponent from bauh.commons.html import bold -from bauh.commons.system import get_human_size_str +from bauh.commons.view_utils import get_human_size_str from bauh.gems.debian import DEBIAN_ICON_PATH from bauh.gems.debian.model import DebianPackage from bauh.view.util.translation import I18n diff --git a/bauh/gems/snap/controller.py b/bauh/gems/snap/controller.py index 4cd443c4..2408dbca 100644 --- a/bauh/gems/snap/controller.py +++ b/bauh/gems/snap/controller.py @@ -16,8 +16,8 @@ from bauh.api.exception import NoInternetException from bauh.commons.boot import CreateConfigFile from bauh.commons.category import CategoriesDownloader from bauh.commons.html import bold -from bauh.commons.system import SystemProcess, ProcessHandler, new_root_subprocess, get_human_size_str -from bauh.commons.view_utils import new_select +from bauh.commons.system import SystemProcess, ProcessHandler, new_root_subprocess +from bauh.commons.view_utils import new_select, get_human_size_str from bauh.gems.snap import snap, URL_CATEGORIES_FILE, CATEGORIES_FILE_PATH, SUGGESTIONS_FILE, \ get_icon_path, snapd from bauh.gems.snap.config import SnapConfigManager diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index 9d965c81..615359a6 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -30,7 +30,8 @@ from bauh.api.paths import DESKTOP_ENTRIES_DIR from bauh.commons import resource from bauh.commons.boot import CreateConfigFile from bauh.commons.html import bold -from bauh.commons.system import ProcessHandler, get_dir_size, get_human_size_str, SimpleProcess +from bauh.commons.system import ProcessHandler, get_dir_size, SimpleProcess +from bauh.commons.view_utils import get_human_size_str from bauh.gems.web import INSTALLED_PATH, nativefier, DESKTOP_ENTRY_PATH_PATTERN, URL_FIX_PATTERN, ENV_PATH, UA_CHROME, \ SUGGESTIONS_CACHE_FILE, ROOT_DIR, TEMP_PATH, FIX_FILE_PATH, ELECTRON_CACHE_DIR, \ get_icon_path, URL_PROPS_PATTERN diff --git a/bauh/view/core/downloader.py b/bauh/view/core/downloader.py index 993b4b14..c1d17243 100644 --- a/bauh/view/core/downloader.py +++ b/bauh/view/core/downloader.py @@ -14,7 +14,8 @@ from bauh.api.abstract.download import FileDownloader from bauh.api.abstract.handler import ProcessWatcher from bauh.api.http import HttpClient from bauh.commons.html import bold -from bauh.commons.system import ProcessHandler, SimpleProcess, get_human_size_str +from bauh.commons.system import ProcessHandler, SimpleProcess +from bauh.commons.view_utils import get_human_size_str from bauh.view.util.translation import I18n RE_HAS_EXTENSION = re.compile(r'.+\.\w+$') diff --git a/bauh/view/qt/thread.py b/bauh/view/qt/thread.py index 870f379c..a692d517 100644 --- a/bauh/view/qt/thread.py +++ b/bauh/view/qt/thread.py @@ -23,7 +23,8 @@ from bauh.api.exception import NoInternetException from bauh.api.paths import LOGS_DIR from bauh.commons.html import bold from bauh.commons.internet import InternetChecker -from bauh.commons.system import get_human_size_str, ProcessHandler, SimpleProcess +from bauh.commons.system import ProcessHandler, SimpleProcess +from bauh.commons.view_utils import get_human_size_str from bauh.view.core import timeshift from bauh.view.core.config import CoreConfigManager from bauh.view.qt import commons diff --git a/tests/common/test_system.py b/tests/common/test_system.py deleted file mode 100644 index 43df06b6..00000000 --- a/tests/common/test_system.py +++ /dev/null @@ -1,35 +0,0 @@ -from unittest import TestCase - -from bauh.commons import system - - -class GetHumanSizeStrTest(TestCase): - - def test__must_properly_display_B(self): - self.assertEqual('1023 B', system.get_human_size_str(1023)) - self.assertEqual('-1023 B', system.get_human_size_str(-1023)) - - def test__must_properly_convert_to_Kb(self): - self.assertEqual('1.00 Kb', system.get_human_size_str(1024)) - self.assertEqual('1.50 Kb', system.get_human_size_str(1536)) - self.assertEqual('1.75 Kb', system.get_human_size_str(1792)) - self.assertEqual('57.30 Kb', system.get_human_size_str(58675.2)) - self.assertEqual('-1.00 Kb', system.get_human_size_str(-1024)) - - def test__must_properly_convert_to_Mb(self): - self.assertEqual('1.00 Mb', system.get_human_size_str(1048576)) - self.assertEqual('1.20 Mb', system.get_human_size_str(1258291)) - self.assertEqual('1.50 Mb', system.get_human_size_str(1572864)) - self.assertEqual('57.30 Mb', system.get_human_size_str(60083404.8)) - - def test__must_properly_convert_to_Gb(self): - self.assertEqual('1.00 Gb', system.get_human_size_str(1073741824)) - self.assertEqual('57.30 Gb', system.get_human_size_str(61525406515.2)) - - def test__must_properly_convert_to_Tb(self): - self.assertEqual('1.00 Tb', system.get_human_size_str(1099511627776)) - self.assertEqual('57.30 Tb', system.get_human_size_str(63002016271564.8)) - - def test__must_properly_convert_to_Pb(self): - self.assertEqual('1.00 Pb', system.get_human_size_str(1125899906842624)) - self.assertEqual('57.30 Pb', system.get_human_size_str(64514064662082350)) diff --git a/tests/common/test_view_utils.py b/tests/common/test_view_utils.py new file mode 100644 index 00000000..063bf2b2 --- /dev/null +++ b/tests/common/test_view_utils.py @@ -0,0 +1,35 @@ +from unittest import TestCase + +from bauh.commons.view_utils import get_human_size_str + + +class GetHumanSizeStrTest(TestCase): + + def test__must_properly_display_B(self): + self.assertEqual('1023 B', get_human_size_str(1023)) + self.assertEqual('-1023 B', get_human_size_str(-1023)) + + def test__must_properly_convert_to_Kb(self): + self.assertEqual('1.00 Kb', get_human_size_str(1024)) + self.assertEqual('1.50 Kb', get_human_size_str(1536)) + self.assertEqual('1.75 Kb', get_human_size_str(1792)) + self.assertEqual('57.30 Kb', get_human_size_str(58675.2)) + self.assertEqual('-1.00 Kb', get_human_size_str(-1024)) + + def test__must_properly_convert_to_Mb(self): + self.assertEqual('1.00 Mb', get_human_size_str(1048576)) + self.assertEqual('1.20 Mb', get_human_size_str(1258291)) + self.assertEqual('1.50 Mb', get_human_size_str(1572864)) + self.assertEqual('57.30 Mb', get_human_size_str(60083404.8)) + + def test__must_properly_convert_to_Gb(self): + self.assertEqual('1.00 Gb', get_human_size_str(1073741824)) + self.assertEqual('57.30 Gb', get_human_size_str(61525406515.2)) + + def test__must_properly_convert_to_Tb(self): + self.assertEqual('1.00 Tb', get_human_size_str(1099511627776)) + self.assertEqual('57.30 Tb', get_human_size_str(63002016271564.8)) + + def test__must_properly_convert_to_Pb(self): + self.assertEqual('1.00 Pb', get_human_size_str(1125899906842624)) + self.assertEqual('57.30 Pb', get_human_size_str(64514064662082350))