Add export command to calibredb

This commit is contained in:
Kovid Goyal 2008-06-21 19:57:50 -07:00
parent aab8744748
commit 233662ebd7
2 changed files with 36 additions and 1 deletions

View File

@ -362,10 +362,42 @@ show_metadata command.
id, opf = int(args[1]), open(args[2], 'rb')
do_set_metadata(get_db(dbpath, opts), id, opf)
return 0
def do_export(db, ids, dir, single_dir, by_author):
if ids is None:
ids = db.all_ids()
db.export_to_dir(dir, ids, byauthor=by_author, single_dir=single_dir, index_is_id=True)
def command_export(args, dbpath):
parser = get_parser(_('''\
%prog export [options] ids
Export the books specified by ids (a comma separated list) to the filesystem.
The export operation saves all formats of the book, its cover and metadata (in
an opf file). You can get id numbers from the list command.
'''))
parser.add_option('--all', default=False, action='store_true',
help=_('Export all books in database, ignoring the list of ids.'))
parser.add_option('--to-dir', default='.',
help=(_('Export books to the specified directory. Default is')+' %default'))
parser.add_option('--single-dir', default=False, action='store_true',
help=_('Export all books into a single directory'))
parser.add_option('--by-author', default=False, action='store_true',
help=_('Create file names as author - title instead of title - author'))
opts, args = parser.parse_args(sys.argv[1:]+args)
if (len(args) < 2 and not opts.all):
parser.print_help()
print
print >>sys.stderr, _('You must specify some ids or the %s option')%'--all'
return 1
ids = None if opts.all else map(int, args[1].split(','))
dir = os.path.abspath(os.path.expanduser(opts.to_dir))
do_export(get_db(dbpath, opts), ids, dir, opts.single_dir, opts.by_author)
return 0
def main(args=sys.argv):
commands = ('list', 'add', 'remove', 'add_format', 'remove_format',
'show_metadata', 'set_metadata')
'show_metadata', 'set_metadata', 'export')
parser = OptionParser(_(
'''\
%%prog command [options] [arguments]

View File

@ -1384,6 +1384,9 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
self.conn.execute('VACUUM;')
self.conn.commit()
def all_ids(self):
return [i[0] for i in self.conn.execute('SELECT id FROM books').fetchall()]
def export_to_dir(self, dir, indices, byauthor=False, single_dir=False,
index_is_id=False):
if not os.path.exists(dir):