diff --git a/borg/cache.py b/borg/cache.py index 569ca7f3d..ac6d34804 100644 --- a/borg/cache.py +++ b/borg/cache.py @@ -1,5 +1,6 @@ from configparser import RawConfigParser from .remote import cache_if_remote +from collections import namedtuple import errno import logging logger = logging.getLogger(__name__) @@ -15,7 +16,7 @@ import tempfile from .key import PlaintextKey from .helpers import Error, get_cache_dir, decode_dict, st_mtime_ns, unhexlify, int_to_bigint, \ - bigint_to_int + bigint_to_int, format_file_size from .locking import UpgradableLock from .hashindex import ChunkIndex @@ -73,6 +74,21 @@ class Cache: def __del__(self): self.close() + def __str__(self): + return format(self, """All archives: {0.total_size:>20s} {0.total_csize:>20s} {0.unique_csize:>20s} + + Unique chunks Total chunks +Chunk index: {0.total_unique_chunks:20d} {0.total_chunks:20d}""") + + def __format__(self, format_spec): + # XXX: this should really be moved down to `hashindex.pyx` + Summary = namedtuple('Summary', ['total_size', 'total_csize', 'unique_size', 'unique_csize', 'total_unique_chunks', 'total_chunks']) + stats = Summary(*self.chunks.summarize())._asdict() + for field in ['total_size', 'total_csize', 'unique_csize']: + stats[field] = format_file_size(stats[field]) + stats = Summary(**stats) + return format_spec.format(stats) + def _confirm(self, message, env_var_override=None): print(message, file=sys.stderr) if env_var_override and os.environ.get(env_var_override): diff --git a/borg/helpers.py b/borg/helpers.py index 98a424876..2404f80cd 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -145,16 +145,20 @@ class Statistics: self.usize += csize def print_(self, label, cache): - total_size, total_csize, unique_size, unique_csize, total_unique_chunks, total_chunks = cache.chunks.summarize() - buf = "\n" - buf += ' Original size Compressed size Deduplicated size' - buf += '%-15s %20s %20s %20s' % (label, format_file_size(self.osize), format_file_size(self.csize), format_file_size(self.usize)) - buf += 'All archives: %20s %20s %20s' % (format_file_size(total_size), format_file_size(total_csize), format_file_size(unique_csize)) + buf = str(self) % label buf += "\n" - buf += ' Unique chunks Total chunks' - buf += 'Chunk index: %20d %20d' % (total_unique_chunks, total_chunks) + buf += str(cache) return buf + def __str__(self): + return format(self, """ Original size Compressed size Deduplicated size +%-15s {0.osize:>20s} {0.csize:>20s} {0.usize:>20s}""") + + def __format__(self, format_spec): + fields = ['osize', 'csize', 'usize'] + FormattedStats = namedtuple('FormattedStats', fields) + return format_spec.format(FormattedStats(*map(format_file_size, [ getattr(self, x) for x in fields ]))) + def show_progress(self, item=None, final=False): if not final: path = remove_surrogates(item[b'path']) if item else ''