mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-06 21:44:16 +02:00
[commons.util] refactoring: centralizing String size conversion into 'size_to_byte'
This commit is contained in:
@@ -2,7 +2,7 @@ import logging
|
||||
from abc import ABC
|
||||
from datetime import datetime
|
||||
from logging import Logger
|
||||
from typing import Optional
|
||||
from typing import Optional, Union
|
||||
|
||||
|
||||
class NullLoggerFactory(ABC):
|
||||
@@ -28,9 +28,18 @@ def deep_update(source: dict, overrides: dict):
|
||||
return source
|
||||
|
||||
|
||||
def size_to_byte(size: float, unit: str) -> float:
|
||||
def size_to_byte(size: Union[float, int, str], unit: str, logger: Optional[Logger] = None) -> Optional[float]:
|
||||
lower_unit = unit.lower()
|
||||
final_size = size
|
||||
|
||||
if isinstance(size, str):
|
||||
try:
|
||||
final_size = float(size.strip().replace(',', '.').replace(' ', ''))
|
||||
except ValueError:
|
||||
if logger:
|
||||
logger.error(f"Could not parse string size {size} to bytes")
|
||||
return
|
||||
else:
|
||||
final_size = float(size)
|
||||
|
||||
if unit == 'b' or len(lower_unit) > 1 and lower_unit.endswith('ib'):
|
||||
final_size /= 8
|
||||
|
||||
@@ -478,7 +478,7 @@ def map_update_sizes(pkgs: List[str]) -> Dict[str, float]: # bytes:
|
||||
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
||||
|
||||
if output:
|
||||
return {pkgs[idx]: size_to_byte(float(size[0].replace(',', '.')), size[1]) for idx, size in enumerate(RE_INSTALLED_SIZE.findall(output))}
|
||||
return {pkgs[idx]: size_to_byte(size[0], size[1]) for idx, size in enumerate(RE_INSTALLED_SIZE.findall(output))}
|
||||
|
||||
return {}
|
||||
|
||||
@@ -487,7 +487,7 @@ def map_download_sizes(pkgs: List[str]) -> Dict[str, float]: # bytes:
|
||||
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
||||
|
||||
if output:
|
||||
return {pkgs[idx]: size_to_byte(float(size[0].replace(',', '.')), size[1]) for idx, size in enumerate(RE_DOWNLOAD_SIZE.findall(output))}
|
||||
return {pkgs[idx]: size_to_byte(size[0], size[1]) for idx, size in enumerate(RE_DOWNLOAD_SIZE.findall(output))}
|
||||
|
||||
return {}
|
||||
|
||||
@@ -496,7 +496,7 @@ def get_installed_size(pkgs: List[str]) -> Dict[str, float]: # bytes
|
||||
output = run_cmd('pacman -Qi {}'.format(' '.join(pkgs)))
|
||||
|
||||
if output:
|
||||
return {pkgs[idx]: size_to_byte(float(size[0].replace(',', '.')), size[1]) for idx, size in enumerate(RE_INSTALLED_SIZE.findall(output))}
|
||||
return {pkgs[idx]: size_to_byte(size[0], size[1]) for idx, size in enumerate(RE_INSTALLED_SIZE.findall(output))}
|
||||
|
||||
return {}
|
||||
|
||||
@@ -658,11 +658,11 @@ def map_updates_data(pkgs: Iterable[str], files: bool = False) -> dict:
|
||||
latest_field = 'c'
|
||||
elif field == 'Download Size':
|
||||
size = val.split(' ')
|
||||
data['ds'] = size_to_byte(float(size[0].replace(',', '.')), size[1])
|
||||
data['ds'] = size_to_byte(size[0], size[1])
|
||||
latest_field = 'ds'
|
||||
elif field == 'Installed Size':
|
||||
size = val.split(' ')
|
||||
data['s'] = size_to_byte(float(size[0].replace(',', '.')), size[1])
|
||||
data['s'] = size_to_byte(size[0], size[1])
|
||||
latest_field = 's'
|
||||
elif latest_name and latest_field == 's':
|
||||
res[latest_name] = data
|
||||
|
||||
@@ -72,14 +72,8 @@ class Aptitude:
|
||||
size_split = final_val.split(' ')
|
||||
|
||||
if len(size_split) >= 1:
|
||||
try:
|
||||
number = float(size_split[0].replace(',', '.'))
|
||||
measure = size_split[1].strip().lower() if len(size_split) >= 2 else 'b'
|
||||
final_val = size_to_byte(number, measure)
|
||||
except ValueError:
|
||||
self._log.warning(f"Could not parse the attribute '{final_field}' "
|
||||
f"value {final_val} to float")
|
||||
final_val = None
|
||||
measure = size_split[1].strip().lower() if len(size_split) >= 2 else 'b'
|
||||
final_val = size_to_byte(size_split[0], measure, self._log)
|
||||
else:
|
||||
self._log.warning(f"Unhandled value ({val}) for attribute '{field}'")
|
||||
final_val = None
|
||||
@@ -117,14 +111,8 @@ class Aptitude:
|
||||
|
||||
if size:
|
||||
size_split = size.strip().split(' ')
|
||||
|
||||
try:
|
||||
number = float(size_split[0].strip().replace(',', '.'))
|
||||
measure = size_split[1].strip()[0].lower() if len(size_split) >= 2 else 'b'
|
||||
pkg.transaction_size = size_to_byte(number, measure)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
pass
|
||||
measure = size_split[1].strip()[0].lower() if len(size_split) >= 2 else 'b'
|
||||
pkg.transaction_size = size_to_byte(size_split[0], measure, self._log)
|
||||
|
||||
current_collection.add(pkg)
|
||||
|
||||
@@ -189,13 +177,8 @@ class Aptitude:
|
||||
|
||||
if fill_size:
|
||||
size_split = line_split[no_attrs - 2].split(' ')
|
||||
number = float(size_split[0].replace(',', '.'))
|
||||
measure = size_split[1][0].lower() if len(size_split) >= 2 else 'b'
|
||||
|
||||
try:
|
||||
size = size_to_byte(number, measure)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
size = size_to_byte(size_split[0], measure, self._log)
|
||||
|
||||
if latest_version is not None:
|
||||
installed_version = line_split[1] if not self.re_none.match(line_split[1]) else None
|
||||
|
||||
@@ -438,12 +438,12 @@ def map_update_download_size(app_ids: Iterable[str], installation: str, version:
|
||||
|
||||
if size and len(size) > 1:
|
||||
try:
|
||||
res[related_id[0].strip()] = size_to_byte(float(size[0].replace(',', '.')), size[1].strip())
|
||||
res[related_id[0].strip()] = size_to_byte(size[0], size[1].strip())
|
||||
except:
|
||||
traceback.print_exc()
|
||||
else:
|
||||
try:
|
||||
res[related_id[0].strip()] = size_to_byte(float(size_tuple[0].replace(',', '.')), size_tuple[1].strip())
|
||||
res[related_id[0].strip()] = size_to_byte(size_tuple[0], size_tuple[1].strip())
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user