def add_formats(self, book_id, paths, mi, replace=True, is_an_add=False): fmap = {p.rpartition(os.path.extsep)[-1].lower(): p for p in paths} fmt_map = {} for fmt, path in iteritems(fmap): # The onimport plugins have already been run by the read metadata # worker if self.ignore_opf and fmt.lower() == 'opf': continue try: if self.db.add_format(book_id, fmt, path, run_hooks=False, replace=replace): run_plugins_on_postimport(self.dbref(), book_id, fmt) fmt_map[fmt.lower()] = path except Exception: a = self.report.append a(''), a('-' * 70) a( _('Failed to add the file {0} to the book: {1}').format( path, mi.title)) a(_('With error:')), a(traceback.format_exc()) if is_an_add: run_plugins_on_postadd(self.dbref(), book_id, fmt_map)
def add_format(self, book_id, fmt, stream_or_path, replace=True, run_hooks=True, dbapi=None): if run_hooks: # Run import plugins npath = run_import_plugins(stream_or_path, fmt) fmt = os.path.splitext(npath)[-1].lower().replace('.', '').upper() stream_or_path = lopen(npath, 'rb') fmt = check_ebook_format(stream_or_path, fmt) fmt = (fmt or '').upper() self.format_metadata_cache[book_id].pop(fmt, None) try: name = self.fields['formats'].format_fname(book_id, fmt) except: name = None if name and not replace: return False path = self._field_for('path', book_id).replace('/', os.sep) title = self._field_for('title', book_id, default_value=_('Unknown')) author = self._field_for('authors', book_id, default_value=(_('Unknown'),))[0] stream = stream_or_path if hasattr(stream_or_path, 'read') else lopen(stream_or_path, 'rb') size, fname = self.backend.add_format(book_id, fmt, stream, title, author, path) del stream max_size = self.fields['formats'].table.update_fmt(book_id, fmt, fname, size, self.backend) self.fields['size'].table.update_sizes({book_id: max_size}) self._update_last_modified((book_id,)) if run_hooks: # Run post import plugins run_plugins_on_postimport(dbapi or self, book_id, fmt) stream_or_path.close() return True
def add_formats(self, id, formats, replace=True): for path in formats: fmt = os.path.splitext(path)[-1].replace('.', '').upper() with open(path, 'rb') as f: # At this point, the filetype on import plugins have already # been run by the metadata reading code, so we only need to run # the postimport plugins, on a successful add. if self.db.add_format(id, fmt, f, index_is_id=True, notify=False, replace=replace): run_plugins_on_postimport(self.db, id, fmt)
def cli_convert(self, args): #NODOC printsd(args) if args.all: # `calibre-debug -r djvumaker -- convert --all` printsd('in cli convert_all') # TODO: make work `djvumaker -- convert --all` # raise NotImplementedError('Convert all is not implemented.') user_input = ask_yesno_input( 'Do you wany to copy-convert all PDFs to DJVU?') if not user_input: return None from calibre.library import db from calibre.customize.ui import run_plugins_on_postimport db = db() # initialize calibre library database for book_id in list(db.all_ids()): if db.has_format(book_id, 'DJVU', index_is_id=True): continue # TODO: shouldn't work with this code, db has not atributte run_plugins_on_postimport # https://github.com/kovidgoyal/calibre/blob/master/src/calibre/customize/ui.py if db.has_format(book_id, 'PDF', index_is_id=True): run_plugins_on_postimport(db, book_id, 'pdf') continue elif args.path is not None: # `calibre-debug -r djvumaker -- convert -p test.pdf` -> tempfile(test.djvu) printsd('in path') if is_rasterbook(args.path): djvu = self.run_backend(args.path, log=self.prints.func) if djvu: input_filename, _ = os.path.splitext(args.path) shutil.copy2(djvu, input_filename + '.djvu') prints("Finished DJVU outputed to: {}.".format( input_filename + '.djvu')) user_input = ask_yesno_input( 'Do you want to open djvused in subshell?' ' (may not work on not macOS)') if not user_input: return None # de-munge the tty sys.stdin = sys.__stdin__ sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ os.system("stat '%s'" % djvu) # TODO: doesn't work on Windows, why is it here? os.system("djvused -e dump '%s'" % djvu) os.system("djvused -v '%s'" % djvu) elif args.id is not None: # `calibre-debug -r djvumaker -- convert -i 123 #id(123).pdf` -> tempfile(id(123).djvu) printsd('in convert by id') self._postimport(args.id)
def add_formats(self, book_id, paths, mi, replace=True): fmap = {p.rpartition(os.path.extsep)[-1].lower():p for p in paths} for fmt, path in fmap.iteritems(): # The onimport plugins have already been run by the read metadata # worker try: if self.db.add_format(book_id, fmt, path, run_hooks=False, replace=replace): run_plugins_on_postimport(self.dbref(), book_id, fmt) except Exception: a = self.report.append a(''), a('-' * 70) a(_('Failed to add the file {0} to the book: {1}').format(path, mi.title)) a(_('With error:')), a(traceback.format_exc())
def cli_convert(self, args): #NODOC printsd(args) if args.all: # `calibre-debug -r djvumaker -- convert --all` printsd('in cli convert_all') # TODO: make work `djvumaker -- convert --all` # raise NotImplementedError('Convert all is not implemented.') user_input = ask_yesno_input('Do you wany to copy-convert all PDFs to DJVU?') if not user_input: return None from calibre.library import db from calibre.customize.ui import run_plugins_on_postimport db = db() # initialize calibre library database for book_id in list(db.all_ids()): if db.has_format(book_id, 'DJVU', index_is_id=True): continue # TODO: shouldn't work with this code, db has not atributte run_plugins_on_postimport # https://github.com/kovidgoyal/calibre/blob/master/src/calibre/customize/ui.py if db.has_format(book_id, 'PDF', index_is_id=True): run_plugins_on_postimport(db, book_id, 'pdf') continue elif args.path is not None: # `calibre-debug -r djvumaker -- convert -p test.pdf` -> tempfile(test.djvu) printsd('in path') if is_rasterbook(args.path): djvu = self.run_backend(args.path, log=self.prints.func) if djvu: input_filename, _ = os.path.splitext(args.path) shutil.copy2(djvu, input_filename + '.djvu') prints("Finished DJVU outputed to: {}.".format(input_filename + '.djvu')) user_input = ask_yesno_input('Do you want to open djvused in subshell?' ' (may not work on not macOS)') if not user_input: return None # de-munge the tty sys.stdin = sys.__stdin__ sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ os.system("stat '%s'" % djvu) # TODO: doesn't work on Windows, why is it here? os.system("djvused -e dump '%s'" % djvu) os.system("djvused -v '%s'" % djvu) elif args.id is not None: # `calibre-debug -r djvumaker -- convert -i 123 #id(123).pdf` -> tempfile(id(123).djvu) printsd('in convert by id') self._postimport(args.id, fork_job=False)
def add_formats(self, book_id, paths, mi, replace=True, is_an_add=False): fmap = {p.rpartition(os.path.extsep)[-1].lower():p for p in paths} fmt_map = {} for fmt, path in iteritems(fmap): # The onimport plugins have already been run by the read metadata # worker if self.ignore_opf and fmt.lower() == 'opf': continue try: if self.db.add_format(book_id, fmt, path, run_hooks=False, replace=replace): run_plugins_on_postimport(self.dbref(), book_id, fmt) fmt_map[fmt.lower()] = path except Exception: a = self.report.append a(''), a('-' * 70) a(_('Failed to add the file {0} to the book: {1}').format(path, mi.title)) a(_('With error:')), a(traceback.format_exc()) if is_an_add: run_plugins_on_postadd(self.dbref(), book_id, fmt_map)