Skip to content

Commit

Permalink
add MPath.pretty_size()
Browse files Browse the repository at this point in the history
  • Loading branch information
ungarj committed Sep 11, 2024
1 parent 1e2fe9e commit 76033ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions mapchete/cli/mpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from mapchete.cli import options
from mapchete.io import copy
from mapchete.path import MPath
from mapchete.pretty import pretty_bytes

opt_recursive = click.option("--recursive", "-r", is_flag=True)

Expand Down Expand Up @@ -79,9 +78,11 @@ def _print_rows(
columns=[
"",
"",
f"{str(subpath)}/"
if absolute_paths
else f"{subpath.relative_to(path)}/",
(
f"{str(subpath)}/"
if absolute_paths
else f"{subpath.relative_to(path)}/"
),
],
widths=[last_modified_column_width, size_column_width, None],
spacing=spacing,
Expand All @@ -92,7 +93,7 @@ def _print_rows(
_row(
columns=[
subpath.last_modified().strftime(date_format),
pretty_bytes(subpath.size()),
subpath.pretty_size(),
str(subpath) if absolute_paths else subpath.relative_to(path),
],
widths=[last_modified_column_width, size_column_width, None],
Expand Down
4 changes: 4 additions & 0 deletions mapchete/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from retry import retry

from mapchete.executor import Executor
from mapchete.pretty import pretty_bytes
from mapchete.settings import GDALHTTPOptions, IORetrySettings, mapchete_options
from mapchete.tile import BufferedTile
from mapchete.types import MPathLike
Expand Down Expand Up @@ -442,6 +443,9 @@ def rm(self, recursive: bool = False, ignore_errors: bool = False) -> None:
def size(self) -> int:
return self.info().get("size", self.info().get("Size"))

def pretty_size(self) -> str:
return pretty_bytes(self.size())

def last_modified(self) -> datetime:
# for S3 objects
last_modified = self.info().get("LastModified")
Expand Down
10 changes: 10 additions & 0 deletions test/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ def test_size_remote(path):
test_size(path)


@pytest.mark.parametrize(
"path",
[
lazy_fixture("metadata_json"),
],
)
def test_pretty_size(path):
assert path.pretty_size()


@pytest.mark.parametrize(
"path",
[
Expand Down

0 comments on commit 76033ff

Please sign in to comment.