Repository navigation
Add command line support for naturalsize #505
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
Open
htaheri17
wants to merge
2
commits into
python-humanize:main
Choose a base branch
from
htaheri17:feature/naturalsize-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| # Filesize | ||
|
|
||
| ::: humanize.filesize | ||
|
|
||
| ## Command line | ||
|
|
||
| Format a byte count with the `humanize` command or with `python -m humanize`: | ||
|
|
||
| ```console | ||
| $ humanize naturalsize 8589934592 | ||
| 8.6 GB | ||
| $ humanize naturalsize --binary 8589934592 | ||
| 8.0 GiB | ||
| $ humanize naturalsize --gnu --format "%.2f" 8589934592 | ||
| 8.00G | ||
| ``` | ||
|
|
||
| With no positional value, one value is read from each input line: | ||
|
|
||
| ```console | ||
| $ printf '1000\n2048\n' | humanize naturalsize | ||
| 1.0 kB | ||
| 2.0 kB | ||
| ``` | ||
|
|
||
| Invalid values produce an error identifying the input line and a nonzero exit status. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """Run humanize as a module.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
|
|
||
| from humanize.cli import main | ||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| """Command-line interface for humanize.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import sys | ||
| from collections.abc import Iterable, Sequence | ||
| from typing import TextIO | ||
|
|
||
| from humanize.filesize import naturalsize | ||
|
|
||
|
|
||
| def _parser() -> argparse.ArgumentParser: | ||
| parser = argparse.ArgumentParser(prog="humanize") | ||
| commands = parser.add_subparsers(dest="command", required=True) | ||
|
|
||
| filesize = commands.add_parser( | ||
| "naturalsize", | ||
| help="format byte counts as human-readable file sizes", | ||
| description="format byte counts as human-readable file sizes", | ||
| ) | ||
| filesize.add_argument( | ||
| "value", | ||
| nargs="?", | ||
| help="number of bytes; read one value per line from stdin when omitted", | ||
| ) | ||
| filesize.add_argument( | ||
| "--binary", | ||
| action="store_true", | ||
| help="use binary units such as KiB and MiB", | ||
| ) | ||
| filesize.add_argument( | ||
| "--gnu", | ||
| action="store_true", | ||
| help="use GNU-style binary units such as K and M", | ||
| ) | ||
| filesize.add_argument( | ||
| "--format", | ||
| default="%.1f", | ||
| help="format string for the numeric portion (default: %%.1f)", | ||
| ) | ||
| return parser | ||
|
|
||
|
|
||
| def _format_value(value: str, args: argparse.Namespace) -> str: | ||
| return naturalsize(value, binary=args.binary, gnu=args.gnu, format=args.format) | ||
|
|
||
|
|
||
| def _run_naturalsize( | ||
| args: argparse.Namespace, | ||
| stdin: TextIO, | ||
| stdout: TextIO, | ||
| stderr: TextIO, | ||
| ) -> int: | ||
| try: | ||
| _format_value("1024", args) | ||
| except (TypeError, ValueError, OverflowError) as error: | ||
| stderr.write(f"humanize: error: invalid format {args.format!r}: {error}\n") | ||
| return 2 | ||
|
|
||
| if args.value is not None: | ||
| values: Iterable[tuple[int, str]] = ((1, args.value),) | ||
| elif stdin.isatty(): | ||
| stderr.write("humanize: error: a value is required when stdin is interactive\n") | ||
| return 2 | ||
| else: | ||
| values = enumerate(stdin, start=1) | ||
|
|
||
| for line_number, value in values: | ||
| value = value.strip() | ||
| try: | ||
| result = _format_value(value, args) | ||
| except (TypeError, ValueError, OverflowError) as error: | ||
| if args.value is not None: | ||
| stderr.write(f"humanize: error: invalid value {value!r}: {error}\n") | ||
| else: | ||
| stderr.write( | ||
| f"humanize: error: invalid value on line {line_number} " | ||
| f"{value!r}: {error}\n" | ||
| ) | ||
| return 2 | ||
| stdout.write(result + "\n") | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| def main(argv: Sequence[str] | None = None) -> int: | ||
| """Run the humanize command-line interface.""" | ||
| args = _parser().parse_args(argv) | ||
| if args.command == "naturalsize": | ||
| return _run_naturalsize(args, sys.stdin, sys.stdout, sys.stderr) | ||
| command = f"unknown command: {args.command}" | ||
| raise AssertionError(command) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| """Tests for the humanize command-line interface.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from humanize.cli import main | ||
|
|
||
| ROOT = Path(__file__).parents[1] | ||
|
|
||
|
|
||
| def run_cli( | ||
| args: list[str], | ||
| input_text: str = "", | ||
| *, | ||
| interactive: bool = False, | ||
| ) -> tuple[int, str, str]: | ||
| stdin = io.StringIO(input_text) | ||
| stdin.isatty = lambda: interactive # type: ignore[method-assign] | ||
| stdout = io.StringIO() | ||
| stderr = io.StringIO() | ||
|
|
||
| old_stdin, old_stdout, old_stderr = sys.stdin, sys.stdout, sys.stderr | ||
| try: | ||
| sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr | ||
| try: | ||
| status = main(args) | ||
| except SystemExit as error: | ||
| status = error.code if isinstance(error.code, int) else 1 | ||
| finally: | ||
| sys.stdin, sys.stdout, sys.stderr = old_stdin, old_stdout, old_stderr | ||
| return status, stdout.getvalue(), stderr.getvalue() | ||
|
|
||
|
|
||
| def test_naturalsize_defaults_to_decimal() -> None: | ||
| assert run_cli(["naturalsize", "8589934592"]) == (0, "8.6 GB\n", "") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "option, expected", | ||
| [ | ||
| ("--binary", "8.0 GiB\n"), | ||
| ("--gnu", "8.0G\n"), | ||
| ], | ||
| ) | ||
| def test_naturalsize_supports_unit_options(option: str, expected: str) -> None: | ||
| assert run_cli(["naturalsize", option, "8589934592"]) == (0, expected, "") | ||
|
|
||
|
|
||
| def test_naturalsize_supports_custom_format() -> None: | ||
| assert run_cli(["naturalsize", "--format", "%.2f", "3000"]) == ( | ||
| 0, | ||
| "3.00 kB\n", | ||
| "", | ||
| ) | ||
|
|
||
|
|
||
| def test_naturalsize_reads_one_value_per_piped_line() -> None: | ||
| assert run_cli(["naturalsize"], "1000\n2048\n") == ( | ||
| 0, | ||
| "1.0 kB\n2.0 kB\n", | ||
| "", | ||
| ) | ||
|
|
||
|
|
||
| def test_naturalsize_reports_invalid_piped_line() -> None: | ||
| assert run_cli(["naturalsize"], "1000\nnot-a-number\n3000\n") == ( | ||
| 2, | ||
| "1.0 kB\n", | ||
| ( | ||
| "humanize: error: invalid value on line 2 'not-a-number': " | ||
| "could not convert string to float: 'not-a-number'\n" | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def test_naturalsize_reports_invalid_value() -> None: | ||
| status, stdout, stderr = run_cli(["naturalsize", "invalid"]) | ||
|
|
||
| assert status == 2 | ||
| assert stdout == "" | ||
| assert "invalid value 'invalid'" in stderr | ||
| assert "could not convert string to float" in stderr | ||
|
|
||
|
|
||
| def test_naturalsize_reports_malformed_format() -> None: | ||
| status, stdout, stderr = run_cli(["naturalsize", "--format", "%q", "3000"]) | ||
|
|
||
| assert status == 2 | ||
| assert stdout == "" | ||
| assert "invalid format '%q'" in stderr | ||
| assert "unsupported format character" in stderr | ||
|
|
||
|
|
||
| def test_naturalsize_requires_value_for_interactive_stdin() -> None: | ||
| assert run_cli(["naturalsize"], interactive=True) == ( | ||
| 2, | ||
| "", | ||
| "humanize: error: a value is required when stdin is interactive\n", | ||
| ) | ||
|
|
||
|
|
||
| def test_help() -> None: | ||
| status, stdout, stderr = run_cli(["naturalsize", "--help"]) | ||
|
|
||
| assert status == 0 | ||
| assert "format byte counts as human-readable file sizes" in stdout | ||
| assert "--binary" in stdout | ||
| assert "--gnu" in stdout | ||
| assert "--format" in stdout | ||
| assert stderr == "" | ||
|
|
||
|
|
||
| def test_python_module_invocation() -> None: | ||
| env = os.environ.copy() | ||
| env["PYTHONPATH"] = str(ROOT / "src") | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "humanize", "naturalsize", "8589934592"], | ||
| cwd=ROOT, | ||
| env=env, | ||
| capture_output=True, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
|
|
||
| assert result.returncode == 0 | ||
| assert result.stdout == "8.6 GB\n" | ||
| assert result.stderr == "" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small version-specific input edge: on Python 3.12,
humanize naturalsize -1e3is rejected as an unrecognized argument, although1e3,-1000, and the underlyingnaturalsize("-1e3")all work.humanize naturalsize -- -1e3and piping-1e3through stdin work as well.On Python 3.15 the direct form works, so this appears to depend on the Python version's argument parser. Would a test or a note about the
--form be useful for the older supported versions? I checked the PR's Python code with captured streams, not the installed console entry point or the full suite.