Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more units and ignore case in file-size config options #277

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/pydistcheck/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ class ExitCodes:
show_default=True,
type=str,
help=(
"maximum allowed compressed size, a string like '1.5M' indicating"
"maximum allowed compressed size, a string like '1.5MB' indicating"
" '1.5 megabytes'. Supported units:\n"
" - B = bytes\n"
" - K = kilobytes\n"
" - M = megabytes\n"
" - G = gigabytes"
" - KB = kilobytes\n"
" - K, Ki = kibibytes\n"
" - MB = megabytes\n"
" - M, Mi = mebibytes\n"
" - GB = gigabytes\n"
" - G, Gi = gibibytes"
),
)
@click.option(
Expand All @@ -140,12 +143,15 @@ class ExitCodes:
show_default=True,
type=str,
help=(
"maximum allowed uncompressed size, a string like '1.5M' indicating"
"maximum allowed uncompressed size, a string like '1.5MB' indicating"
" '1.5 megabytes'. Supported units:\n"
" - B = bytes\n"
" - K = kilobytes\n"
" - M = megabytes\n"
" - G = gigabytes"
" - KB = kilobytes\n"
" - K, Ki = kibibytes\n"
" - MB = megabytes\n"
" - M, Mi = mebibytes\n"
" - GB = gigabytes\n"
" - G, Gi = gibibytes"
),
)
@click.option(
Expand Down
20 changes: 18 additions & 2 deletions src/pydistcheck/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@

from typing import Tuple

_UNIT_TO_NUM_BYTES = {"B": 1, "K": 1024, "M": 1024**2, "G": 1024**3}
# references:
#
# * https://physics.nist.gov/cuu/Units/binary.html
# * https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#meaning-of-memory
#
_UNIT_TO_NUM_BYTES = {
"b": 1,
"k": 1024,
"kb": 1000,
"ki": 1024,
"m": 1024**2,
"mb": 1000000,
"mi": 1024**2,
"g": 1024**3,
"gb": 1000000000,
"gi": 1024**3,
}


def _recommend_size_str(num_bytes: int) -> Tuple[float, str]:
Expand Down Expand Up @@ -35,7 +51,7 @@ def from_string(cls, size_str: str) -> "_FileSize":

@property
def total_size_bytes(self) -> int:
return int(self._num * _UNIT_TO_NUM_BYTES[self._unit_str])
return int(self._num * _UNIT_TO_NUM_BYTES[self._unit_str.lower()])

def __eq__(self, other: object) -> bool:
return (
Expand Down
30 changes: 27 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,33 @@
from pydistcheck.utils import _FileSize


def test_file_size_minimally_works():
fs = _FileSize(num=1.0, unit_str="G")
assert fs.total_size_bytes == int(1024**3)
@pytest.mark.parametrize(
("unit_str", "expected_total_bytes"),
[
("B", 3),
("K", 3 * 1024),
("KB", 3e3),
("kB", 3e3),
("kb", 3e3),
("Ki", 3 * 1024),
("ki", 3 * 1024),
("M", 3 * (1024**2)),
("MB", 3e6),
("mB", 3e6),
("mb", 3e6),
("Mi", 3 * (1024**2)),
("mi", 3 * (1024**2)),
("G", 3 * (1024**3)),
("GB", 3e9),
("gB", 3e9),
("gb", 3e9),
("Gi", 3 * (1024**3)),
("gi", 3 * (1024**3)),
],
)
def test_file_size_minimally_works(expected_total_bytes, unit_str):
fs = _FileSize(num=3.0, unit_str=unit_str)
assert fs.total_size_bytes == int(expected_total_bytes)


def test_file_size_comparisons_work():
Expand Down
Loading