mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-06 21:44:16 +02:00
[common] refactoring: 'get_human_size_str' moved from 'system' to 'view_utils'
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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}'
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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+$')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
35
tests/common/test_view_utils.py
Normal file
35
tests/common/test_view_utils.py
Normal file
@@ -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))
|
||||
Reference in New Issue
Block a user