Cleanup previous PR

This commit is contained in:
Kovid Goyal 2022-11-15 19:42:39 +05:30
parent 19ffac569e
commit 414991fa00
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -36,18 +36,17 @@ def implementation(
db, notify_changes, fields, sort_by, ascending, search_text, limit, template=None db, notify_changes, fields, sort_by, ascending, search_text, limit, template=None
): ):
is_remote = notify_changes is not None is_remote = notify_changes is not None
if 'template' in fields: formatter = None
# Yes, this leaves formatter undefined if template isn't a field but
# that isn't a problem with the current implementation
from calibre.ebooks.metadata.book.formatter import SafeFormat
formatter = SafeFormat()
with db.safe_read_lock: with db.safe_read_lock:
fm = db.field_metadata fm = db.field_metadata
afields = set(FIELDS) | {'id'} afields = set(FIELDS) | {'id'}
for k in fm.custom_field_keys(): for k in fm.custom_field_keys():
afields.add('*' + k[1:]) afields.add('*' + k[1:])
if 'all' in fields: if 'all' in fields:
fields = sorted(afields) if template:
fields = sorted(afields - {'template'})
else:
fields = sorted(afields)
sort_by = sort_by or 'id' sort_by = sort_by or 'id'
if sort_by not in afields: if sort_by not in afields:
return f'Unknown sort field: {sort_by}' return f'Unknown sort field: {sort_by}'
@ -71,11 +70,13 @@ def implementation(
continue continue
if field == 'template': if field == 'template':
vals = {} vals = {}
globals = {} global_vars = {}
if formatter is None:
from calibre.ebooks.metadata.book.formatter import SafeFormat
formatter = SafeFormat()
for book_id in book_ids: for book_id in book_ids:
mi = db.get_proxy_metadata(book_id) mi = db.get_proxy_metadata(book_id)
vals[book_id] = formatter.safe_format(template, {}, 'TEMPLATE ERROR', vals[book_id] = formatter.safe_format(template, {}, 'TEMPLATE ERROR', mi, global_vars=global_vars)
mi, global_vars=globals)
data['template'] = vals data['template'] = vals
continue continue
field = field.replace('*', '#') field = field.replace('*', '#')
@ -161,7 +162,7 @@ def do_list(
): ):
if sort_by is None: if sort_by is None:
ascending = True ascending = True
if 'template' in [f.strip() for f in fields]: if 'template' in (f.strip() for f in fields):
if template_file: if template_file:
with lopen(template_file, 'rb') as f: with lopen(template_file, 'rb') as f:
template = f.read().decode('utf-8') template = f.read().decode('utf-8')
@ -232,11 +233,11 @@ def do_list(
for record in output_table: for record in output_table:
text = [ text = [
wrappers[i](record[i]) for i, _ in enumerate(fields) wrappers[i](record[i]) for i in range(len(fields))
] ]
lines = max(map(len, text)) lines = max(map(len, text))
for l in range(lines): for l in range(lines):
for i, _ in enumerate(text): for i in range(len(text)):
ft = text[i][l] if l < len(text[i]) else '' ft = text[i][l] if l < len(text[i]) else ''
stdout.write(ft.encode('utf-8')) stdout.write(ft.encode('utf-8'))
if i < len(text) - 1: if i < len(text) - 1: