From 9daba19f6146db2d9ac64929222fbe9964edd063 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Wed, 6 Apr 2022 15:41:07 -0300 Subject: [PATCH] [view] improvement: displaying update sizes as localized numbers (update summary) --- CHANGELOG.md | 1 + bauh/app.py | 7 +++++++ bauh/commons/view_utils.py | 4 +++- tests/common/test_view_utils.py | 7 +++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e49f425d..6e3c30c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - update summary: - displaying a '+' for positive sizes (previously the sign was only displayed for negative numbers) (#250)[https://github.com/vinifmor/bauh/issues/250] - changing some words and symbols to improve readability and cohesion (#250)[https://github.com/vinifmor/bauh/issues/250] + - displaying update sizes as localized numbers [#250](https://github.com/vinifmor/bauh/issues/250) ### Fixes - General diff --git a/bauh/app.py b/bauh/app.py index 8f21bdb7..5ee5bcd2 100755 --- a/bauh/app.py +++ b/bauh/app.py @@ -1,4 +1,5 @@ import faulthandler +import locale import os import sys import traceback @@ -25,6 +26,12 @@ def main(tray: bool = False): logger = logs.new_logger(__app_name__, bool(args.logs)) + try: + locale.setlocale(locale.LC_NUMERIC, '') + except: + logger.error("Could not set locale 'LC_NUMBERIC' to '' to display localized numbers") + traceback.print_exc() + if args.offline: logger.warning("offline mode activated") diff --git a/bauh/commons/view_utils.py b/bauh/commons/view_utils.py index 56717b19..082e1d19 100644 --- a/bauh/commons/view_utils.py +++ b/bauh/commons/view_utils.py @@ -1,3 +1,4 @@ +import locale from typing import Tuple, Optional, Iterable from bauh.api.abstract.view import SelectViewType, InputOption, SingleSelectComponent @@ -34,5 +35,6 @@ def get_human_size_str(size, positive_sign: bool = False) -> Optional[str]: if size_unit < 1024: size_unit = size_unit if size > 0 else size_unit * -1 - size_str = f'{int(size_unit)} {unit}' if unit == 'B' else f'{size_unit:.2f} {unit}' + localized_size = locale.format_string('%.2f', size_unit) + size_str = f'{int(size_unit)} {unit}' if unit == 'B' else f"{localized_size} {unit}" return f'+{size_str}' if positive_sign and size_unit > 0 else size_str diff --git a/tests/common/test_view_utils.py b/tests/common/test_view_utils.py index 9bf88c39..f715585e 100644 --- a/tests/common/test_view_utils.py +++ b/tests/common/test_view_utils.py @@ -1,3 +1,4 @@ +import locale from unittest import TestCase from bauh.commons.view_utils import get_human_size_str @@ -5,6 +6,12 @@ from bauh.commons.view_utils import get_human_size_str class GetHumanSizeStrTest(TestCase): + def setUp(self): + try: + locale.setlocale(locale.LC_NUMERIC, None) + except: + print("Error: could not set locale.LC_NUMERIC to None") + 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))