mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-08 17:54:15 +02:00
[common] refactoring: 'get_human_size_str' moved from 'system' to 'view_utils'
This commit is contained in:
@@ -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}'
|
||||
|
||||
Reference in New Issue
Block a user