From a82cf819753eee00d01fe1dcef8341e65c2a8e2e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 10 Dec 2014 22:43:55 +0530 Subject: [PATCH] Fix #1401085 ['calibredb list --for-machine' ignores custom columns](https://bugs.launchpad.net/calibre/+bug/1401085) --- src/calibre/library/cli.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index d566b6c2d4..41bc786f61 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -90,17 +90,21 @@ def do_list(db, fields, afields, sort_by, ascending, search_text, line_width, se return ans if for_machine: import json + record_keys = {field_name(field):field for field in fields} for record in data: - for key in set(record) - set(fields): + for key in set(record) - set(record_keys): del record[key] + for key in tuple(record): + if record_keys[key] != key: # A custom column, use the column label as the key rather than the column id number + record[record_keys[key]] = record.pop(key) for key, val in tuple(record.iteritems()): if hasattr(val, 'isoformat'): record[key] = isoformat(val, as_utc=True) elif val is None: del record[key] return json.dumps(data, indent=2, sort_keys=True) - fields = list(map(field_name, fields)) + fields = list(map(field_name, fields)) for f in data: fmts = [x for x in f['formats'] if x is not None] f['formats'] = u'[%s]'%u', '.join(fmts)