mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Better error when serializing catalog to XML fails
This commit is contained in:
parent
1cc6f9eb7b
commit
4f364dc351
@ -177,72 +177,75 @@ class CSV_XML(CatalogPlugin):
|
|||||||
else:
|
else:
|
||||||
root = E.calibredb()
|
root = E.calibredb()
|
||||||
for r in data:
|
for r in data:
|
||||||
record = E.record()
|
try:
|
||||||
root.append(record)
|
record = E.record()
|
||||||
|
root.append(record)
|
||||||
|
|
||||||
for field in fields:
|
for field in fields:
|
||||||
if field.startswith('#'):
|
if field.startswith('#'):
|
||||||
val = db.get_field(r['id'], field, index_is_id=True)
|
val = db.get_field(r['id'], field, index_is_id=True)
|
||||||
if not isinstance(val, str):
|
if not isinstance(val, str):
|
||||||
val = str(val)
|
val = str(val)
|
||||||
item = getattr(E, field.replace('#', '_'))(val)
|
item = getattr(E, field.replace('#', '_'))(val)
|
||||||
record.append(item)
|
record.append(item)
|
||||||
|
|
||||||
for field in ('id', 'uuid', 'publisher', 'rating', 'size',
|
for field in ('id', 'uuid', 'publisher', 'rating', 'size',
|
||||||
'isbn', 'ondevice', 'identifiers'):
|
'isbn', 'ondevice', 'identifiers'):
|
||||||
if field in fields:
|
if field in fields:
|
||||||
val = r[field]
|
val = r[field]
|
||||||
if not val:
|
if not val:
|
||||||
continue
|
continue
|
||||||
if not isinstance(val, (bytes, str)):
|
if not isinstance(val, (bytes, str)):
|
||||||
if (fm.get(field, {}).get('datatype', None) ==
|
if (fm.get(field, {}).get('datatype', None) ==
|
||||||
'rating' and val):
|
'rating' and val):
|
||||||
val = '%.2g' % (val / 2)
|
val = '%.2g' % (val / 2)
|
||||||
val = str(val)
|
val = str(val)
|
||||||
item = getattr(E, field)(val)
|
item = getattr(E, field)(val)
|
||||||
record.append(item)
|
record.append(item)
|
||||||
|
|
||||||
if 'title' in fields:
|
if 'title' in fields:
|
||||||
title = E.title(r['title'], sort=r['sort'])
|
title = E.title(r['title'], sort=r['sort'])
|
||||||
record.append(title)
|
record.append(title)
|
||||||
|
|
||||||
if 'authors' in fields:
|
if 'authors' in fields:
|
||||||
aus = E.authors(sort=r['author_sort'])
|
aus = E.authors(sort=r['author_sort'])
|
||||||
for au in r['authors']:
|
for au in r['authors']:
|
||||||
aus.append(E.author(au))
|
aus.append(E.author(au))
|
||||||
record.append(aus)
|
record.append(aus)
|
||||||
|
|
||||||
for field in ('timestamp', 'pubdate'):
|
for field in ('timestamp', 'pubdate'):
|
||||||
if field in fields:
|
if field in fields:
|
||||||
record.append(getattr(E, field)(isoformat(r[field], as_utc=False)))
|
record.append(getattr(E, field)(isoformat(r[field], as_utc=False)))
|
||||||
|
|
||||||
if 'tags' in fields and r['tags']:
|
if 'tags' in fields and r['tags']:
|
||||||
tags = E.tags()
|
tags = E.tags()
|
||||||
for tag in r['tags']:
|
for tag in r['tags']:
|
||||||
tags.append(E.tag(tag))
|
tags.append(E.tag(tag))
|
||||||
record.append(tags)
|
record.append(tags)
|
||||||
|
|
||||||
if 'comments' in fields and r['comments']:
|
if 'comments' in fields and r['comments']:
|
||||||
record.append(E.comments(r['comments']))
|
record.append(E.comments(r['comments']))
|
||||||
|
|
||||||
if 'series' in fields and r['series']:
|
if 'series' in fields and r['series']:
|
||||||
record.append(E.series(r['series'],
|
record.append(E.series(r['series'],
|
||||||
index=str(r['series_index'])))
|
index=str(r['series_index'])))
|
||||||
|
|
||||||
if 'languages' in fields and r['languages']:
|
if 'languages' in fields and r['languages']:
|
||||||
record.append(E.languages(r['languages']))
|
record.append(E.languages(r['languages']))
|
||||||
|
|
||||||
if 'cover' in fields and r['cover']:
|
if 'cover' in fields and r['cover']:
|
||||||
record.append(E.cover(r['cover'].replace(os.sep, '/')))
|
record.append(E.cover(r['cover'].replace(os.sep, '/')))
|
||||||
|
|
||||||
if 'formats' in fields and r['formats']:
|
if 'formats' in fields and r['formats']:
|
||||||
fmt = E.formats()
|
fmt = E.formats()
|
||||||
for f in r['formats']:
|
for f in r['formats']:
|
||||||
fmt.append(E.format(f.replace(os.sep, '/')))
|
fmt.append(E.format(f.replace(os.sep, '/')))
|
||||||
record.append(fmt)
|
record.append(fmt)
|
||||||
|
|
||||||
if 'library_name' in fields:
|
if 'library_name' in fields:
|
||||||
record.append(E.library_name(current_library))
|
record.append(E.library_name(current_library))
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception('Failed to convert {} to XML with error: {}'.format(r['title'], e)) from e
|
||||||
|
|
||||||
with open(path_to_output, 'wb') as f:
|
with open(path_to_output, 'wb') as f:
|
||||||
f.write(etree.tostring(root, encoding='utf-8',
|
f.write(etree.tostring(root, encoding='utf-8',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user