mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -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:
|
||||
root = E.calibredb()
|
||||
for r in data:
|
||||
record = E.record()
|
||||
root.append(record)
|
||||
try:
|
||||
record = E.record()
|
||||
root.append(record)
|
||||
|
||||
for field in fields:
|
||||
if field.startswith('#'):
|
||||
val = db.get_field(r['id'], field, index_is_id=True)
|
||||
if not isinstance(val, str):
|
||||
val = str(val)
|
||||
item = getattr(E, field.replace('#', '_'))(val)
|
||||
record.append(item)
|
||||
for field in fields:
|
||||
if field.startswith('#'):
|
||||
val = db.get_field(r['id'], field, index_is_id=True)
|
||||
if not isinstance(val, str):
|
||||
val = str(val)
|
||||
item = getattr(E, field.replace('#', '_'))(val)
|
||||
record.append(item)
|
||||
|
||||
for field in ('id', 'uuid', 'publisher', 'rating', 'size',
|
||||
'isbn', 'ondevice', 'identifiers'):
|
||||
if field in fields:
|
||||
val = r[field]
|
||||
if not val:
|
||||
continue
|
||||
if not isinstance(val, (bytes, str)):
|
||||
if (fm.get(field, {}).get('datatype', None) ==
|
||||
'rating' and val):
|
||||
val = '%.2g' % (val / 2)
|
||||
val = str(val)
|
||||
item = getattr(E, field)(val)
|
||||
record.append(item)
|
||||
for field in ('id', 'uuid', 'publisher', 'rating', 'size',
|
||||
'isbn', 'ondevice', 'identifiers'):
|
||||
if field in fields:
|
||||
val = r[field]
|
||||
if not val:
|
||||
continue
|
||||
if not isinstance(val, (bytes, str)):
|
||||
if (fm.get(field, {}).get('datatype', None) ==
|
||||
'rating' and val):
|
||||
val = '%.2g' % (val / 2)
|
||||
val = str(val)
|
||||
item = getattr(E, field)(val)
|
||||
record.append(item)
|
||||
|
||||
if 'title' in fields:
|
||||
title = E.title(r['title'], sort=r['sort'])
|
||||
record.append(title)
|
||||
if 'title' in fields:
|
||||
title = E.title(r['title'], sort=r['sort'])
|
||||
record.append(title)
|
||||
|
||||
if 'authors' in fields:
|
||||
aus = E.authors(sort=r['author_sort'])
|
||||
for au in r['authors']:
|
||||
aus.append(E.author(au))
|
||||
record.append(aus)
|
||||
if 'authors' in fields:
|
||||
aus = E.authors(sort=r['author_sort'])
|
||||
for au in r['authors']:
|
||||
aus.append(E.author(au))
|
||||
record.append(aus)
|
||||
|
||||
for field in ('timestamp', 'pubdate'):
|
||||
if field in fields:
|
||||
record.append(getattr(E, field)(isoformat(r[field], as_utc=False)))
|
||||
for field in ('timestamp', 'pubdate'):
|
||||
if field in fields:
|
||||
record.append(getattr(E, field)(isoformat(r[field], as_utc=False)))
|
||||
|
||||
if 'tags' in fields and r['tags']:
|
||||
tags = E.tags()
|
||||
for tag in r['tags']:
|
||||
tags.append(E.tag(tag))
|
||||
record.append(tags)
|
||||
if 'tags' in fields and r['tags']:
|
||||
tags = E.tags()
|
||||
for tag in r['tags']:
|
||||
tags.append(E.tag(tag))
|
||||
record.append(tags)
|
||||
|
||||
if 'comments' in fields and r['comments']:
|
||||
record.append(E.comments(r['comments']))
|
||||
if 'comments' in fields and r['comments']:
|
||||
record.append(E.comments(r['comments']))
|
||||
|
||||
if 'series' in fields and r['series']:
|
||||
record.append(E.series(r['series'],
|
||||
index=str(r['series_index'])))
|
||||
if 'series' in fields and r['series']:
|
||||
record.append(E.series(r['series'],
|
||||
index=str(r['series_index'])))
|
||||
|
||||
if 'languages' in fields and r['languages']:
|
||||
record.append(E.languages(r['languages']))
|
||||
if 'languages' in fields and r['languages']:
|
||||
record.append(E.languages(r['languages']))
|
||||
|
||||
if 'cover' in fields and r['cover']:
|
||||
record.append(E.cover(r['cover'].replace(os.sep, '/')))
|
||||
if 'cover' in fields and r['cover']:
|
||||
record.append(E.cover(r['cover'].replace(os.sep, '/')))
|
||||
|
||||
if 'formats' in fields and r['formats']:
|
||||
fmt = E.formats()
|
||||
for f in r['formats']:
|
||||
fmt.append(E.format(f.replace(os.sep, '/')))
|
||||
record.append(fmt)
|
||||
if 'formats' in fields and r['formats']:
|
||||
fmt = E.formats()
|
||||
for f in r['formats']:
|
||||
fmt.append(E.format(f.replace(os.sep, '/')))
|
||||
record.append(fmt)
|
||||
|
||||
if 'library_name' in fields:
|
||||
record.append(E.library_name(current_library))
|
||||
if 'library_name' in fields:
|
||||
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:
|
||||
f.write(etree.tostring(root, encoding='utf-8',
|
||||
|
Loading…
x
Reference in New Issue
Block a user