mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-06 22:54: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 abc import ABC
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
from typing import Optional
|
from typing import Optional, Union
|
||||||
|
|
||||||
|
|
||||||
class NullLoggerFactory(ABC):
|
class NullLoggerFactory(ABC):
|
||||||
@@ -28,9 +28,18 @@ def deep_update(source: dict, overrides: dict):
|
|||||||
return source
|
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()
|
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'):
|
if unit == 'b' or len(lower_unit) > 1 and lower_unit.endswith('ib'):
|
||||||
final_size /= 8
|
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)))
|
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
||||||
|
|
||||||
if output:
|
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 {}
|
return {}
|
||||||
|
|
||||||
@@ -487,7 +487,7 @@ def map_download_sizes(pkgs: List[str]) -> Dict[str, float]: # bytes:
|
|||||||
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
||||||
|
|
||||||
if output:
|
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 {}
|
return {}
|
||||||
|
|
||||||
@@ -496,7 +496,7 @@ def get_installed_size(pkgs: List[str]) -> Dict[str, float]: # bytes
|
|||||||
output = run_cmd('pacman -Qi {}'.format(' '.join(pkgs)))
|
output = run_cmd('pacman -Qi {}'.format(' '.join(pkgs)))
|
||||||
|
|
||||||
if output:
|
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 {}
|
return {}
|
||||||
|
|
||||||
@@ -658,11 +658,11 @@ def map_updates_data(pkgs: Iterable[str], files: bool = False) -> dict:
|
|||||||
latest_field = 'c'
|
latest_field = 'c'
|
||||||
elif field == 'Download Size':
|
elif field == 'Download Size':
|
||||||
size = val.split(' ')
|
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'
|
latest_field = 'ds'
|
||||||
elif field == 'Installed Size':
|
elif field == 'Installed Size':
|
||||||
size = val.split(' ')
|
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'
|
latest_field = 's'
|
||||||
elif latest_name and latest_field == 's':
|
elif latest_name and latest_field == 's':
|
||||||
res[latest_name] = data
|
res[latest_name] = data
|
||||||
|
|||||||
@@ -72,14 +72,8 @@ class Aptitude:
|
|||||||
size_split = final_val.split(' ')
|
size_split = final_val.split(' ')
|
||||||
|
|
||||||
if len(size_split) >= 1:
|
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'
|
measure = size_split[1].strip().lower() if len(size_split) >= 2 else 'b'
|
||||||
final_val = size_to_byte(number, measure)
|
final_val = size_to_byte(size_split[0], measure, self._log)
|
||||||
except ValueError:
|
|
||||||
self._log.warning(f"Could not parse the attribute '{final_field}' "
|
|
||||||
f"value {final_val} to float")
|
|
||||||
final_val = None
|
|
||||||
else:
|
else:
|
||||||
self._log.warning(f"Unhandled value ({val}) for attribute '{field}'")
|
self._log.warning(f"Unhandled value ({val}) for attribute '{field}'")
|
||||||
final_val = None
|
final_val = None
|
||||||
@@ -117,14 +111,8 @@ class Aptitude:
|
|||||||
|
|
||||||
if size:
|
if size:
|
||||||
size_split = size.strip().split(' ')
|
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'
|
measure = size_split[1].strip()[0].lower() if len(size_split) >= 2 else 'b'
|
||||||
pkg.transaction_size = size_to_byte(number, measure)
|
pkg.transaction_size = size_to_byte(size_split[0], measure, self._log)
|
||||||
except:
|
|
||||||
traceback.print_exc()
|
|
||||||
pass
|
|
||||||
|
|
||||||
current_collection.add(pkg)
|
current_collection.add(pkg)
|
||||||
|
|
||||||
@@ -189,13 +177,8 @@ class Aptitude:
|
|||||||
|
|
||||||
if fill_size:
|
if fill_size:
|
||||||
size_split = line_split[no_attrs - 2].split(' ')
|
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'
|
measure = size_split[1][0].lower() if len(size_split) >= 2 else 'b'
|
||||||
|
size = size_to_byte(size_split[0], measure, self._log)
|
||||||
try:
|
|
||||||
size = size_to_byte(number, measure)
|
|
||||||
except:
|
|
||||||
traceback.print_exc()
|
|
||||||
|
|
||||||
if latest_version is not None:
|
if latest_version is not None:
|
||||||
installed_version = line_split[1] if not self.re_none.match(line_split[1]) else 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:
|
if size and len(size) > 1:
|
||||||
try:
|
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:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
else:
|
else:
|
||||||
try:
|
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:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return res
|
return res
|
||||||
|
|||||||
@@ -31,3 +31,14 @@ class SizeToByteTest(TestCase):
|
|||||||
self.assertEqual(6926923254988.8, size_to_byte(50.4, 'TiB'))
|
self.assertEqual(6926923254988.8, size_to_byte(50.4, 'TiB'))
|
||||||
self.assertEqual(140737488355328, size_to_byte(1, 'PiB'))
|
self.assertEqual(140737488355328, size_to_byte(1, 'PiB'))
|
||||||
self.assertEqual(330733097635020.8, size_to_byte(2.35, 'PiB'))
|
self.assertEqual(330733097635020.8, size_to_byte(2.35, 'PiB'))
|
||||||
|
|
||||||
|
def test_must_return_converted_string_sizes(self):
|
||||||
|
self.assertEqual(1024, size_to_byte('1', 'K'))
|
||||||
|
self.assertEqual(58675.2, size_to_byte('57.3', 'K'))
|
||||||
|
self.assertEqual(60083404.8, size_to_byte('57.3', 'M'))
|
||||||
|
self.assertEqual(61525406515.2, size_to_byte('57.3', 'G'))
|
||||||
|
self.assertEqual(63002016271564.8, size_to_byte('57.3', 'T'))
|
||||||
|
self.assertEqual(6383852471797678, size_to_byte('5.67', 'P'))
|
||||||
|
|
||||||
|
def test_must_treat_string_sizes_before_converting(self):
|
||||||
|
self.assertEqual(58675.2, size_to_byte(' 57 , 3 ', 'K'))
|
||||||
|
|||||||
Reference in New Issue
Block a user