mirror of
https://github.com/borgbackup/borg.git
synced 2025-01-01 12:45:34 +00:00
Apply --format when listing repository archives
This commit is contained in:
parent
0942bcd1b4
commit
c839eb63f7
2 changed files with 35 additions and 9 deletions
|
@ -29,7 +29,7 @@
|
||||||
from .helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR
|
from .helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR
|
||||||
from .helpers import Error, NoManifestError
|
from .helpers import Error, NoManifestError
|
||||||
from .helpers import location_validator, archivename_validator, ChunkerParams, CompressionSpec
|
from .helpers import location_validator, archivename_validator, ChunkerParams, CompressionSpec
|
||||||
from .helpers import ItemFormatter, format_time, format_file_size, format_archive
|
from .helpers import ItemFormatter, ArchiveFormatter, format_time, format_file_size, format_archive
|
||||||
from .helpers import safe_encode, remove_surrogates, bin_to_hex
|
from .helpers import safe_encode, remove_surrogates, bin_to_hex
|
||||||
from .helpers import prune_within, prune_split
|
from .helpers import prune_within, prune_split
|
||||||
from .helpers import to_localtime, timestamp
|
from .helpers import to_localtime, timestamp
|
||||||
|
@ -762,13 +762,19 @@ def write(bytestring):
|
||||||
for item in archive.iter_items(lambda item: matcher.match(item.path)):
|
for item in archive.iter_items(lambda item: matcher.match(item.path)):
|
||||||
write(safe_encode(formatter.format_item(item)))
|
write(safe_encode(formatter.format_item(item)))
|
||||||
else:
|
else:
|
||||||
|
if args.format:
|
||||||
|
format = args.format
|
||||||
|
elif args.short:
|
||||||
|
format = "{archive}{NL}"
|
||||||
|
else:
|
||||||
|
format = "{archive:<36} {time} [{id}]{NL}"
|
||||||
|
formatter = ArchiveFormatter(format)
|
||||||
|
|
||||||
for archive_info in manifest.list_archive_infos(sort_by='ts'):
|
for archive_info in manifest.list_archive_infos(sort_by='ts'):
|
||||||
if args.prefix and not archive_info.name.startswith(args.prefix):
|
if args.prefix and not archive_info.name.startswith(args.prefix):
|
||||||
continue
|
continue
|
||||||
if args.short:
|
write(safe_encode(formatter.format_item(archive_info)))
|
||||||
print(archive_info.name)
|
|
||||||
else:
|
|
||||||
print(format_archive(archive_info))
|
|
||||||
return self.exit_code
|
return self.exit_code
|
||||||
|
|
||||||
@with_repository(cache=True)
|
@with_repository(cache=True)
|
||||||
|
|
|
@ -1132,7 +1132,7 @@ def log_multi(*msgs, level=logging.INFO, logger=logger):
|
||||||
logger.log(level, line)
|
logger.log(level, line)
|
||||||
|
|
||||||
|
|
||||||
class ItemFormatter:
|
class BaseFormatter:
|
||||||
FIXED_KEYS = {
|
FIXED_KEYS = {
|
||||||
# Formatting aids
|
# Formatting aids
|
||||||
'LF': '\n',
|
'LF': '\n',
|
||||||
|
@ -1143,6 +1143,29 @@ class ItemFormatter:
|
||||||
'NEWLINE': os.linesep,
|
'NEWLINE': os.linesep,
|
||||||
'NL': os.linesep,
|
'NL': os.linesep,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def get_item_data(self, item):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def format_item(self, item):
|
||||||
|
return self.format.format_map(self.get_item_data(item))
|
||||||
|
|
||||||
|
|
||||||
|
class ArchiveFormatter(BaseFormatter):
|
||||||
|
|
||||||
|
def __init__(self, format):
|
||||||
|
self.format = partial_format(format, self.FIXED_KEYS)
|
||||||
|
|
||||||
|
def get_item_data(self, archive):
|
||||||
|
return {
|
||||||
|
'barchive': archive.name,
|
||||||
|
'archive': remove_surrogates(archive.name),
|
||||||
|
'id': bin_to_hex(archive.id),
|
||||||
|
'time': format_time(to_localtime(archive.ts)),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ItemFormatter(BaseFormatter):
|
||||||
KEY_DESCRIPTIONS = {
|
KEY_DESCRIPTIONS = {
|
||||||
'bpath': 'verbatim POSIX path, can contain any character except NUL',
|
'bpath': 'verbatim POSIX path, can contain any character except NUL',
|
||||||
'path': 'path interpreted as text (might be missing non-text characters, see bpath)',
|
'path': 'path interpreted as text (might be missing non-text characters, see bpath)',
|
||||||
|
@ -1254,9 +1277,6 @@ def get_item_data(self, item):
|
||||||
item_data[key] = self.call_keys[key](item)
|
item_data[key] = self.call_keys[key](item)
|
||||||
return item_data
|
return item_data
|
||||||
|
|
||||||
def format_item(self, item):
|
|
||||||
return self.format.format_map(self.get_item_data(item))
|
|
||||||
|
|
||||||
def calculate_num_chunks(self, item):
|
def calculate_num_chunks(self, item):
|
||||||
return len(item.get('chunks', []))
|
return len(item.get('chunks', []))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue