From 233662ebd7872c7f2bd9b9451eb71c8e7548f73c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 21 Jun 2008 19:57:50 -0700 Subject: [PATCH] Add export command to calibredb --- src/calibre/library/cli.py | 34 ++++++++++++++++++++++++++++++++- src/calibre/library/database.py | 3 +++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index bce926ad35..6e4ceb95b8 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -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] diff --git a/src/calibre/library/database.py b/src/calibre/library/database.py index 9d54afc88c..ceff444186 100644 --- a/src/calibre/library/database.py +++ b/src/calibre/library/database.py @@ -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):