From 05d21e3e5a2d295397482c4181ce7c5781184b9c Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Tue, 24 Sep 2024 23:31:00 +0800 Subject: [PATCH] Rename the functions for size conversion --- Whatsapp_Chat_Exporter/__main__.py | 8 ++++---- Whatsapp_Chat_Exporter/android_handler.py | 4 ++-- Whatsapp_Chat_Exporter/utility.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Whatsapp_Chat_Exporter/__main__.py b/Whatsapp_Chat_Exporter/__main__.py index f34f270..b284e9c 100644 --- a/Whatsapp_Chat_Exporter/__main__.py +++ b/Whatsapp_Chat_Exporter/__main__.py @@ -17,8 +17,8 @@ else: from Whatsapp_Chat_Exporter import exported_handler, android_handler from Whatsapp_Chat_Exporter import ios_handler, ios_media_handler from Whatsapp_Chat_Exporter.data_model import ChatStore -from Whatsapp_Chat_Exporter.utility import APPLE_TIME, Crypt, DbType, chat_is_empty, convert_size_reverse -from Whatsapp_Chat_Exporter.utility import check_update, import_from_json, sanitize_filename, convert_size +from Whatsapp_Chat_Exporter.utility import APPLE_TIME, Crypt, DbType, chat_is_empty, readable_to_bytes +from Whatsapp_Chat_Exporter.utility import check_update, import_from_json, sanitize_filename, bytes_to_readable from argparse import ArgumentParser, SUPPRESS from datetime import datetime from sys import exit @@ -329,7 +329,7 @@ def main(): parser.error("When --enrich-from-vcards is provided, you must also set --default-country-code") if args.size is not None and not isinstance(args.size, int) and not args.size.isnumeric(): try: - args.size = convert_size_reverse(args.size) + args.size = readable_to_bytes(args.size) except ValueError: parser.error("The value for --split must be ended in pure bytes or with a proper unit (e.g., 1048576 or 1MB)") if args.filter_date is not None: @@ -580,7 +580,7 @@ def main(): ensure_ascii=not args.avoid_encoding_json, indent=args.pretty_print_json ) - print(f"\nWriting JSON file...({convert_size(len(data))})") + print(f"\nWriting JSON file...({bytes_to_readable(len(data))})") f.write(data) else: if args.json[-5:] == ".json": diff --git a/Whatsapp_Chat_Exporter/android_handler.py b/Whatsapp_Chat_Exporter/android_handler.py index 492dfb8..b10c40c 100644 --- a/Whatsapp_Chat_Exporter/android_handler.py +++ b/Whatsapp_Chat_Exporter/android_handler.py @@ -15,7 +15,7 @@ from Whatsapp_Chat_Exporter.data_model import ChatStore, Message from Whatsapp_Chat_Exporter.utility import MAX_SIZE, ROW_SIZE, DbType, convert_time_unit, determine_metadata from Whatsapp_Chat_Exporter.utility import rendering, Crypt, Device, get_file_name, setup_template, JidType from Whatsapp_Chat_Exporter.utility import brute_force_offset, CRYPT14_OFFSETS, get_status_location -from Whatsapp_Chat_Exporter.utility import get_chat_condition, slugify, convert_size, chat_is_empty +from Whatsapp_Chat_Exporter.utility import get_chat_condition, slugify, bytes_to_readable, chat_is_empty try: import zlib @@ -740,7 +740,7 @@ def calls(db, data, timezone_offset, filter_chat): call.data += "unavailable." elif content['call_result'] == 5: call_time = convert_time_unit(content['duration']) - call_bytes = convert_size(content['bytes_transferred']) + call_bytes = bytes_to_readable(content['bytes_transferred']) call.data += ( f"initiated and lasted for {call_time} " f"with {call_bytes} data transferred." diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 8c4fc95..f13bf1e 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -44,7 +44,7 @@ def convert_time_unit(time_second: int): return time -def convert_size(size_bytes): +def bytes_to_readable(size_bytes): """From https://stackoverflow.com/a/14822210/9478891 Authors: james-sapam & other contributors Licensed under CC BY-SA 3.0 @@ -59,7 +59,7 @@ def convert_size(size_bytes): return "%s %s" % (s, size_name[i]) -def convert_size_reverse(size_str): +def readable_to_bytes(size_str): SIZE_UNITS = { 'B': 1, 'KB': 1024,