mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
commit
ac174d8175
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2012, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2012, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
@ -12,6 +12,7 @@ from calibre.customize import CatalogPlugin
|
|||||||
from calibre.library.catalogs import FIELDS
|
from calibre.library.catalogs import FIELDS
|
||||||
from calibre.customize.conversion import DummyReporter
|
from calibre.customize.conversion import DummyReporter
|
||||||
|
|
||||||
|
|
||||||
class CSV_XML(CatalogPlugin):
|
class CSV_XML(CatalogPlugin):
|
||||||
'CSV/XML catalog generator'
|
'CSV/XML catalog generator'
|
||||||
|
|
||||||
@ -22,27 +23,27 @@ class CSV_XML(CatalogPlugin):
|
|||||||
supported_platforms = ['windows', 'osx', 'linux']
|
supported_platforms = ['windows', 'osx', 'linux']
|
||||||
author = 'Greg Riker'
|
author = 'Greg Riker'
|
||||||
version = (1, 0, 0)
|
version = (1, 0, 0)
|
||||||
file_types = set(['csv','xml'])
|
file_types = set(['csv', 'xml'])
|
||||||
|
|
||||||
cli_options = [
|
cli_options = [
|
||||||
Option('--fields',
|
Option('--fields',
|
||||||
default = 'all',
|
default='all',
|
||||||
dest = 'fields',
|
dest='fields',
|
||||||
action = None,
|
action=None,
|
||||||
help = _('The fields to output when cataloging books in the '
|
help=_('The fields to output when cataloging books in the '
|
||||||
'database. Should be a comma-separated list of fields.\n'
|
'database. Should be a comma-separated list of fields.\n'
|
||||||
'Available fields: %(fields)s,\n'
|
'Available fields: %(fields)s,\n'
|
||||||
'plus user-created custom fields.\n'
|
'plus user-created custom fields.\n'
|
||||||
'Example: %(opt)s=title,authors,tags\n'
|
'Example: %(opt)s=title,authors,tags\n'
|
||||||
"Default: '%%default'\n"
|
"Default: '%%default'\n"
|
||||||
"Applies to: CSV, XML output formats")%dict(
|
"Applies to: CSV, XML output formats") % dict(
|
||||||
fields=', '.join(FIELDS), opt='--fields')),
|
fields=', '.join(FIELDS), opt='--fields')),
|
||||||
|
|
||||||
Option('--sort-by',
|
Option('--sort-by',
|
||||||
default = 'id',
|
default='id',
|
||||||
dest = 'sort_by',
|
dest='sort_by',
|
||||||
action = None,
|
action=None,
|
||||||
help = _('Output field to sort on.\n'
|
help=_('Output field to sort on.\n'
|
||||||
'Available fields: author_sort, id, rating, size, timestamp, title_sort\n'
|
'Available fields: author_sort, id, rating, size, timestamp, title_sort\n'
|
||||||
"Default: '%default'\n"
|
"Default: '%default'\n"
|
||||||
"Applies to: CSV, XML output formats"))]
|
"Applies to: CSV, XML output formats"))]
|
||||||
@ -97,7 +98,7 @@ class CSV_XML(CatalogPlugin):
|
|||||||
for entry in data:
|
for entry in data:
|
||||||
entry['ondevice'] = db.catalog_plugin_on_device_temp_mapping[entry['id']]['ondevice']
|
entry['ondevice'] = db.catalog_plugin_on_device_temp_mapping[entry['id']]['ondevice']
|
||||||
|
|
||||||
fm = {x:db.field_metadata.get(x, {}) for x in fields}
|
fm = {x: db.field_metadata.get(x, {}) for x in fields}
|
||||||
|
|
||||||
if self.fmt == 'csv':
|
if self.fmt == 'csv':
|
||||||
outfile = codecs.open(path_to_output, 'w', 'utf8')
|
outfile = codecs.open(path_to_output, 'w', 'utf8')
|
||||||
@ -113,7 +114,7 @@ class CSV_XML(CatalogPlugin):
|
|||||||
outstr = []
|
outstr = []
|
||||||
for field in fields:
|
for field in fields:
|
||||||
if field.startswith('#'):
|
if field.startswith('#'):
|
||||||
item = db.get_field(entry['id'],field,index_is_id=True)
|
item = db.get_field(entry['id'], field, index_is_id=True)
|
||||||
elif field == 'library_name':
|
elif field == 'library_name':
|
||||||
item = current_library
|
item = current_library
|
||||||
elif field == 'title_sort':
|
elif field == 'title_sort':
|
||||||
@ -129,7 +130,7 @@ class CSV_XML(CatalogPlugin):
|
|||||||
for format in item:
|
for format in item:
|
||||||
fmt_list.append(format.rpartition('.')[2].lower())
|
fmt_list.append(format.rpartition('.')[2].lower())
|
||||||
item = ', '.join(fmt_list)
|
item = ', '.join(fmt_list)
|
||||||
elif field in ['authors','tags']:
|
elif field in ['authors', 'tags']:
|
||||||
item = ', '.join(item)
|
item = ', '.join(item)
|
||||||
elif field == 'isbn':
|
elif field == 'isbn':
|
||||||
# Could be 9, 10 or 13 digits
|
# Could be 9, 10 or 13 digits
|
||||||
@ -137,20 +138,20 @@ class CSV_XML(CatalogPlugin):
|
|||||||
elif field in ['pubdate', 'timestamp']:
|
elif field in ['pubdate', 'timestamp']:
|
||||||
item = isoformat(item)
|
item = isoformat(item)
|
||||||
elif field == 'comments':
|
elif field == 'comments':
|
||||||
item = item.replace(u'\r\n',u' ')
|
item = item.replace(u'\r\n', u' ')
|
||||||
item = item.replace(u'\n',u' ')
|
item = item.replace(u'\n', u' ')
|
||||||
elif fm.get(field, {}).get('datatype', None) == 'rating' and item:
|
elif fm.get(field, {}).get('datatype', None) == 'rating' and item:
|
||||||
item = u'%.2g'%(item/2.0)
|
item = u'%.2g' % (item / 2.0)
|
||||||
|
|
||||||
# Convert HTML to markdown text
|
# Convert HTML to markdown text
|
||||||
if type(item) is unicode:
|
if type(item) is unicode:
|
||||||
opening_tag = re.search('<(\w+)(\x20|>)',item)
|
opening_tag = re.search('<(\w+)(\x20|>)', item)
|
||||||
if opening_tag:
|
if opening_tag:
|
||||||
closing_tag = re.search('<\/%s>$' % opening_tag.group(1), item)
|
closing_tag = re.search('<\/%s>$' % opening_tag.group(1), item)
|
||||||
if closing_tag:
|
if closing_tag:
|
||||||
item = html2text(item)
|
item = html2text(item)
|
||||||
|
|
||||||
outstr.append(u'"%s"' % unicode(item).replace('"','""'))
|
outstr.append(u'"%s"' % unicode(item).replace('"', '""'))
|
||||||
|
|
||||||
outfile.write(u','.join(outstr) + u'\n')
|
outfile.write(u','.join(outstr) + u'\n')
|
||||||
outfile.close()
|
outfile.close()
|
||||||
@ -165,14 +166,14 @@ class CSV_XML(CatalogPlugin):
|
|||||||
|
|
||||||
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, unicode)):
|
if not isinstance(val, (str, unicode)):
|
||||||
val = unicode(val)
|
val = unicode(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:
|
||||||
@ -180,7 +181,7 @@ class CSV_XML(CatalogPlugin):
|
|||||||
if not isinstance(val, (str, unicode)):
|
if not isinstance(val, (str, unicode)):
|
||||||
if (fm.get(field, {}).get('datatype', None) ==
|
if (fm.get(field, {}).get('datatype', None) ==
|
||||||
'rating' and val):
|
'rating' and val):
|
||||||
val = u'%.2g'%(val/2.0)
|
val = u'%.2g' % (val / 2.0)
|
||||||
val = unicode(val)
|
val = unicode(val)
|
||||||
item = getattr(E, field)(val)
|
item = getattr(E, field)(val)
|
||||||
record.append(item)
|
record.append(item)
|
||||||
@ -227,4 +228,3 @@ class CSV_XML(CatalogPlugin):
|
|||||||
with open(path_to_output, 'w') as f:
|
with open(path_to_output, 'w') as f:
|
||||||
f.write(etree.tostring(root, encoding='utf-8',
|
f.write(etree.tostring(root, encoding='utf-8',
|
||||||
xml_declaration=True, pretty_print=True))
|
xml_declaration=True, pretty_print=True))
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user