Cast numeric string in readable_to_bytes

This commit is contained in:
KnugiHK
2025-06-01 12:15:15 +08:00
parent 706466f63b
commit 0ecfe6c59a
3 changed files with 10 additions and 12 deletions

View File

@@ -334,16 +334,12 @@ def validate_args(parser: ArgumentParser, args) -> None:
# Size validation and conversion # Size validation and conversion
if args.size is not None: if args.size is not None:
stripped_size = args.size.strip() try:
if stripped_size.isnumeric(): args.size = readable_to_bytes(args.size)
args.size = int(stripped_size) except ValueError:
else: parser.error(
try: "The value for --split must be pure bytes or use a proper unit (e.g., 1048576 or 1MB)"
args.size = readable_to_bytes(stripped_size) )
except ValueError:
parser.error(
"The value for --split must be pure bytes or use a proper unit (e.g., 1048576 or 1MB)"
)
# Date filter validation and processing # Date filter validation and processing
if args.filter_date is not None: if args.filter_date is not None:

View File

@@ -119,6 +119,9 @@ def readable_to_bytes(size_str: str) -> int:
} }
size_str = size_str.upper().strip() size_str = size_str.upper().strip()
match = re.fullmatch(r'^(\d+(\.\d+)?)\s*([KMGTPEZY]?B)?$', size_str) match = re.fullmatch(r'^(\d+(\.\d+)?)\s*([KMGTPEZY]?B)?$', size_str)
if size_str.isnumeric():
# If the string is purely numeric, assume it's in bytes
return int(size_str)
if not match: if not match:
raise ValueError("Invalid size format. Expected format like '10MB', '1024GB', or '512'.") raise ValueError("Invalid size format. Expected format like '10MB', '1024GB', or '512'.")
unit = ''.join(filter(str.isalpha, size_str)).strip() unit = ''.join(filter(str.isalpha, size_str)).strip()

View File

@@ -76,8 +76,7 @@ class TestReadableToBytes:
readable_to_bytes("ABC KB") readable_to_bytes("ABC KB")
def test_missing_unit(self): def test_missing_unit(self):
with pytest.raises(ValueError, match="Invalid input for size_str"): assert readable_to_bytes("100") == 100
readable_to_bytes("100")
class TestSanitizeExcept: class TestSanitizeExcept: