1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-25 09:19:31 +00:00

use custom JSON encoder for repr'ing Borg objects consistently

This commit is contained in:
Marian Beermann 2017-02-23 12:09:09 +01:00
parent 7cbade2f8c
commit 2ab5d0f213

View file

@ -65,8 +65,25 @@
STATS_HEADER = " Original size Compressed size Deduplicated size" STATS_HEADER = " Original size Compressed size Deduplicated size"
class BorgJsonEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Repository) or isinstance(o, RemoteRepository):
return {
'id': bin_to_hex(o.id),
'location': o._location.canonical_path(),
}
if isinstance(o, Archive):
return o.info()
if isinstance(o, Cache):
return {
'path': o.path,
'stats': o.stats(),
}
return super().default(o)
def print_as_json(obj): def print_as_json(obj):
print(json.dumps(obj, sort_keys=True, indent=4)) print(json.dumps(obj, sort_keys=True, indent=4, cls=BorgJsonEncoder))
def argument(args, str_or_bool): def argument(args, str_or_bool):
@ -371,9 +388,10 @@ def create_inner(archive, cache):
if args.stats: if args.stats:
if args.json: if args.json:
print_as_json({ print_as_json({
'cache_stats': cache.stats(), 'repository': repository,
'cache': cache,
'stats': archive.stats.as_dict(), 'stats': archive.stats.as_dict(),
'archive': archive.info(), 'archive': archive,
}) })
else: else:
log_multi(DASHES, log_multi(DASHES,
@ -1026,9 +1044,8 @@ def _info_repository(self, args, repository, key, cache):
encryption += '\nKey file: %s' % key.find_key() encryption += '\nKey file: %s' % key.find_key()
info = { info = {
'id': bin_to_hex(repository.id), 'repository': repository,
'location': repository._location.canonical_path(), 'cache': cache,
'cache': cache.path,
'security_dir': cache.security_manager.dir, 'security_dir': cache.security_manager.dir,
'encryption': encryption, 'encryption': encryption,
} }
@ -1041,9 +1058,12 @@ def _info_repository(self, args, repository, key, cache):
Repository ID: {id} Repository ID: {id}
Location: {location} Location: {location}
{encryption} {encryption}
Cache: {cache} Cache: {cache.path}
Security dir: {security_dir} Security dir: {security_dir}
""").strip().format_map(info)) """).strip().format(
id=bin_to_hex(repository.id),
location=repository._location.canonical_path(),
**info))
print(DASHES) print(DASHES)
print(STATS_HEADER) print(STATS_HEADER)
print(str(cache)) print(str(cache))