From 2337ee6bdcad00cb289fda08e2f355219f726565 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Sun, 16 Apr 2023 11:08:58 +0300 Subject: [PATCH] Updated readme --- ChangeLog.txt | 1 + slpkg/utilities.py | 27 ++++++++++++--------------- slpkg/views/views.py | 5 ++++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 4558ff62..f59cbe2a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,7 @@ 4.7.9 - 15/04/203 Updated: - For proxies configuration +- For convert file sizes Fixed: - Clear screen when dialog is disabled diff --git a/slpkg/utilities.py b/slpkg/utilities.py index b308ed56..2c9cb30e 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -162,23 +162,20 @@ class Utilities: """ A general method to raise an error message and exit. """ raise SystemExit(f"\n{self.configs.prog_name}: {self.bred}Error{self.endc}: {message}.\n") + def get_file_size(self, file: Path) -> str: + """ Get the local file size and converted to units. """ + size = Path(file).stat().st_size + return self.convert_file_sizes(size) + @staticmethod - def get_file_size(file: Path) -> str: - """ Get the file size and converted to units. """ - unit: str = 'KB' - file_size = Path(file).stat().st_size - mb = file_size / 1024 ** 2 - gb = file_size / 1024 ** 3 + def convert_file_sizes(size: int) -> str: + """ Convert file sizes. """ + units: list = ['KB', 'MB', 'GB'] - if mb >= 0.1: - file_size = mb - unit: str = 'MB' - - if gb >= 0.1: - file_size = mb - unit: str = 'GB' - - return f'{str(round(file_size, 2))} {unit}' + for unit in units: + size: float = size / 1000 + if size < 1000: + return f'{size:.2f} {unit}' @staticmethod def apply_package_pattern(data: dict, packages: list) -> list: diff --git a/slpkg/views/views.py b/slpkg/views/views.py index e027be5d..c8786381 100644 --- a/slpkg/views/views.py +++ b/slpkg/views/views.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import os +import re from typing import Any from pathlib import Path @@ -60,7 +61,9 @@ class ViewMessage(Configs): if self.option_for_binaries: version: str = self.data[package][0] - size: str = self.data[package][4] + size: str = self.utils.convert_file_sizes( + int(''.join(re.findall(r'\d+', self.data[package][4]))) + ) repo: str = self.repo else: version: str = self.data[package][2]