def extract_book(self, book, fpath, fmt): fdir = os.path.dirname(fpath).replace(settings['with_library'], settings['extract_path']) subprocess.call(['mkdir', '-p', fdir]) #fdir = os.path.dirname(fpath) + "/extract" if os.path.isfile(fdir+"/META-INF/container.xml"): subprocess.call(["chmod", "a+rx", "-R", fdir + "/META-INF"]) return progress_file = open(self.get_path_progress(book['id']), "w", 0) new_path = "" if fmt != "epub": new_fmt = "epub" new_path = os.path.join(settings["convert_path"], 'book-%s-%s.%s'%(book['id'], int(time.time()), new_fmt) ) logging.error('convert book: %s => %s' % ( fpath, new_path)); log = Log() log.outputs = [FileStream(progress_file)] plumber = Plumber(fpath, new_path, log) recommendations = [ ('flow_size', 15, OptionRecommendation.HIGH) ] plumber.merge_ui_recommendations(recommendations) try: plumber.run() except Exception as e: progress_file.write(u"\n%s\n" % e) progress_file.write(u"\n服务器处理异常,请在QQ群里联系管理员。\n[FINISH]") self.add_msg("danger", u'文件格式转换失败,请在QQ群里联系管理员.') return self.db.add_format(book['id'], new_fmt, open(new_path, "rb"), index_is_id=True) fpath = new_path # extract to dir logging.error('extract book: %s' % fpath) subprocess.call(["unzip", fpath, "-d", fdir], stdout=progress_file) subprocess.call(["chmod", "a+rx", "-R", fdir+ "/META-INF"]) if new_path: subprocess.call(["rm", new_fpath]) return
def extract_book(self, book, fpath, fmt): fdir = os.path.dirname(fpath).replace(settings['with_library'], settings['extract_path']) subprocess.call(['mkdir', '-p', fdir]) #fdir = os.path.dirname(fpath) + "/extract" if os.path.isfile(fdir + "/META-INF/container.xml"): subprocess.call(["chmod", "a+rx", "-R", fdir + "/META-INF"]) return new_path = "" if fmt != "epub": new_fmt = "epub" new_path = os.path.join( settings["convert_path"], 'book-%s-%s.%s' % (book['id'], int(time.time()), new_fmt)) logging.error('convert book: %s => %s' % (fpath, new_path)) log = Log() plumber = Plumber(fpath, new_path, log) recommendations = [('flow_size', 15, OptionRecommendation.HIGH)] plumber.merge_ui_recommendations(recommendations) plumber.run() self.db.add_format(book['id'], new_fmt, open(new_path, "rb"), index_is_id=True) fpath = new_path # extract to dir logging.error('extract book: %s' % fpath) subprocess.call(["unzip", fpath, "-d", fdir]) subprocess.call(["chmod", "a+rx", "-R", fdir + "/META-INF"]) if new_path: subprocess.call(["rm", new_fpath]) return
def convert_book(path_to_ebook, opf_path, cover_path, output_fmt, recs): from calibre.customize.conversion import OptionRecommendation from calibre.ebooks.conversion.plumber import Plumber from calibre.utils.logging import Log recs.append(('verbose', 2, OptionRecommendation.HIGH)) recs.append( ('read_metadata_from_opf', opf_path, OptionRecommendation.HIGH)) if cover_path: recs.append(('cover', cover_path, OptionRecommendation.HIGH)) log = Log() os.chdir(os.path.dirname(path_to_ebook)) status_file = share_open('status', 'wb') def notification(percent, msg): status_file.write('{}:{}|||\n'.format(percent, msg).encode('utf-8')) status_file.flush() output_path = os.path.abspath('output.' + output_fmt.lower()) plumber = Plumber(path_to_ebook, output_path, log, report_progress=notification, override_input_metadata=True) plumber.merge_ui_recommendations(recs) plumber.run()
def convert_book(self, book, mail_to=None): fmt = 'mobi' fpath = os.path.join(settings['convert_path'], '%s.%s' % (ascii_filename(book['title']), fmt)) progress_file = open(self.get_path_progress(book['id']), "w", 0) log = Log() log.outputs = [FileStream(progress_file)] old_path = None for f in ['txt', 'azw3', 'epub']: old_path = book.get('fmt_%s' % f, old_path) #old_path = book.get('fmt_epub', book.get('fmt_txt']) plumber = Plumber(old_path, fpath, log) plumber.run() try: plumber.run() except Exception as e: progress_file.write("\n%s\n" % e) progress_file.write(u"\n服务器处理异常,请在QQ群里联系管理员。\n[FINISH]") self.add_msg("danger", u'文件格式转换失败,请在QQ群里联系管理员.') return self.db.add_format(book['id'], fmt, open(fpath, "rb"), index_is_id=True) if mail_to: self.do_send_mail(book, mail_to, fmt, fpath) return
def generate_books(self, mi, fpath, fmt): # convert another format new_fmt = {'epub': 'mobi', 'mobi': 'epub'}.get(fmt, "epub") new_path = '/tmp/calibre-tmp.' + new_fmt log = Log() plumber = Plumber(fpath, new_path, log) plumber.run() self.db.add_books([new_path], [new_fmt], mi, add_duplicates=False)
def gui_convert(input, output, recommendations, notification=DummyReporter(), abort_after_input_dump=False, log=None, override_input_metadata=False): recommendations = list(recommendations) recommendations.append(('verbose', 2, OptionRecommendation.HIGH)) if log is None: log = Log() plumber = Plumber(input, output, log, report_progress=notification, abort_after_input_dump=abort_after_input_dump, override_input_metadata=override_input_metadata) plumber.merge_ui_recommendations(recommendations) plumber.run()
def convert_book(self, book, mail_to=None): fmt = 'mobi' fpath = '/tmp/%s.%s' % (ascii_filename(book['title']), fmt) log = Log() plumber = Plumber(book['fmt_epub'], fpath, log) plumber.run() self.db.add_format(book['id'], fmt, open(fpath, "rb"), index_is_id=True) if mail_to: self.do_send_mail(book, mail_to, fmt, fpath) return
def get_preprocess_html(path_to_ebook, output=None): from calibre.ebooks.conversion.plumber import set_regex_wizard_callback, Plumber from calibre.utils.logging import DevNull from calibre.ptempfile import TemporaryDirectory raw = {} set_regex_wizard_callback(raw.__setitem__) with TemporaryDirectory('_regex_wiz') as tdir: pl = Plumber(path_to_ebook, os.path.join(tdir, 'a.epub'), DevNull(), for_regex_wizard=True) pl.run() items = [raw[item.href] for item in pl.oeb.spine if item.href in raw] with (sys.stdout if output is None else open(output, 'wb')) as out: for html in items: out.write(html.encode('utf-8')) out.write(b'\n\n' + b'-'*80 + b'\n\n')
def get_preprocess_html(path_to_ebook, output=None): from calibre.ebooks.conversion.plumber import set_regex_wizard_callback, Plumber from calibre.utils.logging import DevNull from calibre.ptempfile import TemporaryDirectory raw = {} set_regex_wizard_callback(raw.__setitem__) with TemporaryDirectory('_regex_wiz') as tdir: pl = Plumber(path_to_ebook, os.path.join(tdir, 'a.epub'), DevNull(), for_regex_wizard=True) pl.run() items = [raw[item.href] for item in pl.oeb.spine if item.href in raw] with (sys.stdout if output is None else open(output, 'wb')) as out: for html in items: out.write(html.encode('utf-8')) out.write(b'\n\n' + b'-' * 80 + b'\n\n')
def convert_book(self, book, mail_to=None): fmt = 'mobi' fpath = os.path.join(settings['convert_path'], '%s.%s' % (ascii_filename(book['title']), fmt)) log = Log() old_path = None for f in ['txt', 'azw3', 'epub']: old_path = book.get('fmt_%s' % f, old_path) #old_path = book.get('fmt_epub', book.get('fmt_txt']) plumber = Plumber(old_path, fpath, log) plumber.run() self.db.add_format(book['id'], fmt, open(fpath, "rb"), index_is_id=True) if mail_to: self.do_send_mail(book, mail_to, fmt, fpath) return
def convert_document(source: Path, target: Path, cover_dir: Path): post = frontmatter.load(source) log = Log() title = post.get("title", "Untitled") author = post.get("creator", "Shannan Lekwati") args = [ ("authors", author), ("language", post.get("lang", "en")), ("title", title), ] date = post.get("date") if date: args += [ ("pubdate", str(post["date"])), ("timestamp", str(post["date"])) ] summary = post.get("summary") if summary: args += [ ("comments", summary) ] cover_image = post.get("cover", {}).get("image") if cover_image: cover_path = cover_dir.joinpath(cover_image).absolute() if cover_path.exists(): args += [ ("cover", str(cover_path)) ] else: print(f"WARNING: {cover_image} in {source} does not exists", file=sys.stderr) with NamedTemporaryFile(suffix=source.suffix, mode="w") as f: f.write(f"# {title} by **{author}** \n") if "dedication" in post: f.write(f"{dedication}\n") f.write(post.content) f.flush() plumber = Plumber(f.name, target, log) recommendations = [(k, v, OptionRecommendation.HIGH) for (k,v) in args] plumber.merge_ui_recommendations(recommendations) plumber.run()
def opf_to_book(opf, outpath, container): from calibre.ebooks.conversion.plumber import Plumber, create_oebbook class Item(Manifest.Item): def _parse_css(self, data): # The default CSS parser used by oeb.base inserts the h namespace # and resolves all @import rules. We dont want that. return container.parse_css(data) def specialize(oeb): oeb.manifest.Item = Item plumber = Plumber(opf, outpath, container.log) plumber.setup_options() class Reader(OEBReader): def _metadata_from_opf(self, opf): for e in xpath(opf, 'o2:metadata//o2:meta'): if e.attrib.get('name') == 'original-resolution': comic_book_exth_values['original-resolution'] = e.attrib.get('content', '660x800') return OEBReader._metadata_from_opf(self, opf) oeb = create_oebbook(container.log, opf, plumber.opts, specialize=specialize, reader=Reader) fixup_metadata(oeb) set_cover_image(oeb) plumber.opts.dont_compress = True plumber.opts.toc_title = None plumber.opts.mobi_toc_at_start = False plumber.opts.no_inline_toc = True plumber.opts.mobi_periodical = False res = Resources(oeb, plumber.opts, False, process_images=False) if path.splitext(outpath)[1] != '.azw3': plumber.run() else: book = create_kf8_book(oeb, plumber.opts, res) book.opts.prefer_author_sort = False book.opts.share_not_sync = False print ('\nWriting out: {}\n'.format(outpath)) book.write(outpath)
def convert_book(path_to_ebook, opf_path, cover_path, output_fmt, recs): from calibre.customize.conversion import OptionRecommendation from calibre.ebooks.conversion.plumber import Plumber from calibre.utils.logging import Log recs.append(('verbose', 2, OptionRecommendation.HIGH)) recs.append(('read_metadata_from_opf', opf_path, OptionRecommendation.HIGH)) if cover_path: recs.append(('cover', cover_path, OptionRecommendation.HIGH)) log = Log() os.chdir(os.path.dirname(path_to_ebook)) status_file = share_open('status', 'wb') def notification(percent, msg=''): status_file.write('{}:{}|||\n'.format(percent, msg).encode('utf-8')) status_file.flush() output_path = os.path.abspath('output.' + output_fmt.lower()) plumber = Plumber(path_to_ebook, output_path, log, report_progress=notification, override_input_metadata=True) plumber.merge_ui_recommendations(recs) plumber.run()
opts.catalog_title.replace('"', '\\"'), 'calibre') new_cover_path.write(new_cover) new_cover_path.close() recommendations.append( ('cover', new_cover_path.name, OptionRecommendation.HIGH)) # Run ebook-convert from calibre.ebooks.conversion.plumber import Plumber plumber = Plumber(os.path.join(catalog.catalog_path, opts.basename + '.opf'), path_to_output, log, report_progress=notification, abort_after_input_dump=False) plumber.merge_ui_recommendations(recommendations) plumber.run() try: os.remove(cpath) except: pass if GENERATE_DEBUG_EPUB: from calibre.ebooks.epub import initialize_container from calibre.ebooks.tweak import zip_rebuilder from calibre.utils.zipfile import ZipFile input_path = os.path.join(catalog_debug_path, 'input') epub_shell = os.path.join(catalog_debug_path, 'epub_shell.zip') initialize_container(epub_shell, opf_name='content.opf') with ZipFile(epub_shell, 'r') as zf: zf.extractall(path=input_path)
def run(self, path_to_output, opts, db, notification=DummyReporter()): from calibre.library.catalogs.epub_mobi_builder import CatalogBuilder from calibre.utils.logging import default_log as log opts.log = log opts.fmt = self.fmt = path_to_output.rpartition('.')[2] # Add local options opts.creator = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y')) opts.creator_sort_as = '%s %s' % ('calibre', strftime('%Y-%m-%d')) opts.connected_kindle = False # Finalize output_profile op = opts.output_profile if op is None: op = 'default' if opts.connected_device['name'] and 'kindle' in opts.connected_device['name'].lower(): opts.connected_kindle = True if opts.connected_device['serial'] and \ opts.connected_device['serial'][:4] in ['B004','B005']: op = "kindle_dx" else: op = "kindle" opts.descriptionClip = 380 if op.endswith('dx') or 'kindle' not in op else 100 opts.authorClip = 100 if op.endswith('dx') or 'kindle' not in op else 60 opts.output_profile = op opts.basename = "Catalog" opts.cli_environment = not hasattr(opts,'sync') # Hard-wired to always sort descriptions by author, with series after non-series opts.sort_descriptions_by_author = True build_log = [] build_log.append(u"%s(): Generating %s %sin %s environment" % (self.name,self.fmt,'for %s ' % opts.output_profile if opts.output_profile else '', 'CLI' if opts.cli_environment else 'GUI')) # If exclude_genre is blank, assume user wants all genre tags included if opts.exclude_genre.strip() == '': opts.exclude_genre = '\[^.\]' build_log.append(" converting empty exclude_genre to '\[^.\]'") if opts.connected_device['is_device_connected'] and \ opts.connected_device['kind'] == 'device': if opts.connected_device['serial']: build_log.append(u" connected_device: '%s' #%s%s " % \ (opts.connected_device['name'], opts.connected_device['serial'][0:4], 'x' * (len(opts.connected_device['serial']) - 4))) for storage in opts.connected_device['storage']: if storage: build_log.append(u" mount point: %s" % storage) else: build_log.append(u" connected_device: '%s'" % opts.connected_device['name']) try: for storage in opts.connected_device['storage']: if storage: build_log.append(u" mount point: %s" % storage) except: build_log.append(u" (no mount points)") else: build_log.append(u" connected_device: '%s'" % opts.connected_device['name']) opts_dict = vars(opts) if opts_dict['ids']: build_log.append(" book count: %d" % len(opts_dict['ids'])) sections_list = [] if opts.generate_authors: sections_list.append('Authors') if opts.generate_titles: sections_list.append('Titles') if opts.generate_series: sections_list.append('Series') if opts.generate_genres: sections_list.append('Genres') if opts.generate_recently_added: sections_list.append('Recently Added') if opts.generate_descriptions: sections_list.append('Descriptions') if not sections_list: if opts.cli_environment: opts.log.warn('*** No Section switches specified, enabling all Sections ***') opts.generate_authors = True opts.generate_titles = True opts.generate_series = True opts.generate_genres = True opts.generate_recently_added = True opts.generate_descriptions = True sections_list = ['Authors','Titles','Series','Genres','Recently Added','Descriptions'] else: opts.log.warn('\n*** No enabled Sections, terminating catalog generation ***') return ["No Included Sections","No enabled Sections.\nCheck E-book options tab\n'Included sections'\n"] if opts.fmt == 'mobi' and sections_list == ['Descriptions']: warning = _("\n*** Adding 'By Authors' Section required for MOBI output ***") opts.log.warn(warning) sections_list.insert(0,'Authors') opts.generate_authors = True opts.log(u" Sections: %s" % ', '.join(sections_list)) opts.section_list = sections_list # Limit thumb_width to 1.0" - 2.0" try: if float(opts.thumb_width) < float(self.THUMB_SMALLEST): log.warning("coercing thumb_width from '%s' to '%s'" % (opts.thumb_width,self.THUMB_SMALLEST)) opts.thumb_width = self.THUMB_SMALLEST if float(opts.thumb_width) > float(self.THUMB_LARGEST): log.warning("coercing thumb_width from '%s' to '%s'" % (opts.thumb_width,self.THUMB_LARGEST)) opts.thumb_width = self.THUMB_LARGEST opts.thumb_width = "%.2f" % float(opts.thumb_width) except: log.error("coercing thumb_width from '%s' to '%s'" % (opts.thumb_width,self.THUMB_SMALLEST)) opts.thumb_width = "1.0" # Display opts keys = opts_dict.keys() keys.sort() build_log.append(" opts:") for key in keys: if key in ['catalog_title','authorClip','connected_kindle','descriptionClip', 'exclude_book_marker','exclude_genre','exclude_tags', 'header_note_source_field','merge_comments', 'output_profile','read_book_marker', 'search_text','sort_by','sort_descriptions_by_author','sync', 'thumb_width','wishlist_tag']: build_log.append(" %s: %s" % (key, repr(opts_dict[key]))) if opts.verbose: log('\n'.join(line for line in build_log)) self.opts = opts # Launch the Catalog builder catalog = CatalogBuilder(db, opts, self, report_progress=notification) if opts.verbose: log.info(" Begin catalog source generation") catalog.createDirectoryStructure() catalog.copyResources() catalog.calculateThumbnailSize() catalog_source_built = catalog.buildSources() if opts.verbose: if catalog_source_built: log.info(" Completed catalog source generation\n") else: log.error(" *** Terminated catalog generation, check log for details ***") if catalog_source_built: recommendations = [] recommendations.append(('remove_fake_margins', False, OptionRecommendation.HIGH)) recommendations.append(('comments', '', OptionRecommendation.HIGH)) # Use to debug generated catalog code before conversion #setattr(opts,'debug_pipeline',os.path.expanduser("~/Desktop/Catalog debug")) dp = getattr(opts, 'debug_pipeline', None) if dp is not None: recommendations.append(('debug_pipeline', dp, OptionRecommendation.HIGH)) if opts.fmt == 'mobi' and opts.output_profile and opts.output_profile.startswith("kindle"): recommendations.append(('output_profile', opts.output_profile, OptionRecommendation.HIGH)) recommendations.append(('no_inline_toc', True, OptionRecommendation.HIGH)) recommendations.append(('book_producer',opts.output_profile, OptionRecommendation.HIGH)) # If cover exists, use it cpath = None try: search_text = 'title:"%s" author:%s' % ( opts.catalog_title.replace('"', '\\"'), 'calibre') matches = db.search(search_text, return_matches=True) if matches: cpath = db.cover(matches[0], index_is_id=True, as_path=True) if cpath and os.path.exists(cpath): recommendations.append(('cover', cpath, OptionRecommendation.HIGH)) except: pass # Run ebook-convert from calibre.ebooks.conversion.plumber import Plumber plumber = Plumber(os.path.join(catalog.catalogPath, opts.basename + '.opf'), path_to_output, log, report_progress=notification, abort_after_input_dump=False) plumber.merge_ui_recommendations(recommendations) plumber.run() try: os.remove(cpath) except: pass # returns to gui2.actions.catalog:catalog_generated() return catalog.error
def run(self, path_to_output, opts, db, notification=DummyReporter()): from calibre.library.catalogs.epub_mobi_builder import CatalogBuilder from calibre.utils.logging import default_log as log from calibre.utils.config import JSONConfig # If preset specified from the cli, insert stored options from JSON file if hasattr(opts, 'preset') and opts.preset: available_presets = JSONConfig("catalog_presets") if opts.preset not in available_presets: if available_presets: print(_('Error: Preset "%s" not found.' % opts.preset)) print(_('Stored presets: %s' % ', '.join([p for p in sorted(available_presets.keys())]))) else: print(_('Error: No stored presets.')) return 1 # Copy the relevant preset values to the opts object for item in available_presets[opts.preset]: if item not in ['exclusion_rules_tw', 'format', 'prefix_rules_tw']: setattr(opts, item, available_presets[opts.preset][item]) # Provide an unconnected device opts.connected_device = { 'is_device_connected': False, 'kind': None, 'name': None, 'save_template': None, 'serial': None, 'storage': None, } # Convert prefix_rules and exclusion_rules from JSON lists to tuples prs = [] for rule in opts.prefix_rules: prs.append(tuple(rule)) opts.prefix_rules = tuple(prs) ers = [] for rule in opts.exclusion_rules: ers.append(tuple(rule)) opts.exclusion_rules = tuple(ers) opts.log = log opts.fmt = self.fmt = path_to_output.rpartition('.')[2] # Add local options opts.creator = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y')) opts.creator_sort_as = '%s %s' % ('calibre', strftime('%Y-%m-%d')) opts.connected_kindle = False # Finalize output_profile op = opts.output_profile if op is None: op = 'default' if opts.connected_device['name'] and 'kindle' in opts.connected_device['name'].lower(): opts.connected_kindle = True if opts.connected_device['serial'] and \ opts.connected_device['serial'][:4] in ['B004', 'B005']: op = "kindle_dx" else: op = "kindle" opts.description_clip = 380 if op.endswith('dx') or 'kindle' not in op else 100 opts.author_clip = 100 if op.endswith('dx') or 'kindle' not in op else 60 opts.output_profile = op opts.basename = "Catalog" opts.cli_environment = not hasattr(opts, 'sync') # Hard-wired to always sort descriptions by author, with series after non-series opts.sort_descriptions_by_author = True build_log = [] build_log.append(u"%s('%s'): Generating %s %sin %s environment, locale: '%s'" % (self.name, current_library_name(), self.fmt, 'for %s ' % opts.output_profile if opts.output_profile else '', 'CLI' if opts.cli_environment else 'GUI', calibre_langcode_to_name(canonicalize_lang(get_lang()), localize=False)) ) # If exclude_genre is blank, assume user wants all tags as genres if opts.exclude_genre.strip() == '': # opts.exclude_genre = '\[^.\]' # build_log.append(" converting empty exclude_genre to '\[^.\]'") opts.exclude_genre = 'a^' build_log.append(" converting empty exclude_genre to 'a^'") if opts.connected_device['is_device_connected'] and \ opts.connected_device['kind'] == 'device': if opts.connected_device['serial']: build_log.append(u" connected_device: '%s' #%s%s " % (opts.connected_device['name'], opts.connected_device['serial'][0:4], 'x' * (len(opts.connected_device['serial']) - 4))) for storage in opts.connected_device['storage']: if storage: build_log.append(u" mount point: %s" % storage) else: build_log.append(u" connected_device: '%s'" % opts.connected_device['name']) try: for storage in opts.connected_device['storage']: if storage: build_log.append(u" mount point: %s" % storage) except: build_log.append(u" (no mount points)") else: build_log.append(u" connected_device: '%s'" % opts.connected_device['name']) opts_dict = vars(opts) if opts_dict['ids']: build_log.append(" book count: %d" % len(opts_dict['ids'])) sections_list = [] if opts.generate_authors: sections_list.append('Authors') if opts.generate_titles: sections_list.append('Titles') if opts.generate_series: sections_list.append('Series') if opts.generate_genres: sections_list.append('Genres') if opts.generate_recently_added: sections_list.append('Recently Added') if opts.generate_descriptions: sections_list.append('Descriptions') if not sections_list: if opts.cli_environment: opts.log.warn('*** No Section switches specified, enabling all Sections ***') opts.generate_authors = True opts.generate_titles = True opts.generate_series = True opts.generate_genres = True opts.generate_recently_added = True opts.generate_descriptions = True sections_list = ['Authors', 'Titles', 'Series', 'Genres', 'Recently Added', 'Descriptions'] else: opts.log.warn('\n*** No enabled Sections, terminating catalog generation ***') return ["No Included Sections", "No enabled Sections.\nCheck E-book options tab\n'Included sections'\n"] if opts.fmt == 'mobi' and sections_list == ['Descriptions']: warning = _("\n*** Adding 'By authors' section required for MOBI output ***") opts.log.warn(warning) sections_list.insert(0, 'Authors') opts.generate_authors = True opts.log(u" Sections: %s" % ', '.join(sections_list)) opts.section_list = sections_list # Limit thumb_width to 1.0" - 2.0" try: if float(opts.thumb_width) < float(self.THUMB_SMALLEST): log.warning("coercing thumb_width from '%s' to '%s'" % (opts.thumb_width, self.THUMB_SMALLEST)) opts.thumb_width = self.THUMB_SMALLEST if float(opts.thumb_width) > float(self.THUMB_LARGEST): log.warning("coercing thumb_width from '%s' to '%s'" % (opts.thumb_width, self.THUMB_LARGEST)) opts.thumb_width = self.THUMB_LARGEST opts.thumb_width = "%.2f" % float(opts.thumb_width) except: log.error("coercing thumb_width from '%s' to '%s'" % (opts.thumb_width, self.THUMB_SMALLEST)) opts.thumb_width = "1.0" # eval prefix_rules if passed from command line if type(opts.prefix_rules) is not tuple: try: opts.prefix_rules = eval(opts.prefix_rules) except: log.error("malformed --prefix-rules: %s" % opts.prefix_rules) raise for rule in opts.prefix_rules: if len(rule) != 4: log.error("incorrect number of args for --prefix-rules: %s" % repr(rule)) # eval exclusion_rules if passed from command line if type(opts.exclusion_rules) is not tuple: try: opts.exclusion_rules = eval(opts.exclusion_rules) except: log.error("malformed --exclusion-rules: %s" % opts.exclusion_rules) raise for rule in opts.exclusion_rules: if len(rule) != 3: log.error("incorrect number of args for --exclusion-rules: %s" % repr(rule)) # Display opts keys = sorted(opts_dict.keys()) build_log.append(" opts:") for key in keys: if key in ['catalog_title', 'author_clip', 'connected_kindle', 'creator', 'cross_reference_authors', 'description_clip', 'exclude_book_marker', 'exclude_genre', 'exclude_tags', 'exclusion_rules', 'fmt', 'genre_source_field', 'header_note_source_field', 'merge_comments_rule', 'output_profile', 'prefix_rules', 'preset', 'read_book_marker', 'search_text', 'sort_by', 'sort_descriptions_by_author', 'sync', 'thumb_width', 'use_existing_cover', 'wishlist_tag']: build_log.append(" %s: %s" % (key, repr(opts_dict[key]))) if opts.verbose: log('\n'.join(line for line in build_log)) # Capture start_time opts.start_time = time.time() self.opts = opts if opts.verbose: log.info(" Begin catalog source generation (%s)" % str(datetime.timedelta(seconds=int(time.time() - opts.start_time)))) # Launch the Catalog builder catalog = CatalogBuilder(db, opts, self, report_progress=notification) try: catalog.build_sources() if opts.verbose: log.info(" Completed catalog source generation (%s)\n" % str(datetime.timedelta(seconds=int(time.time() - opts.start_time)))) except (AuthorSortMismatchException, EmptyCatalogException) as e: log.error(" *** Terminated catalog generation: %s ***" % e) except: log.error(" unhandled exception in catalog generator") raise else: recommendations = [] recommendations.append(('remove_fake_margins', False, OptionRecommendation.HIGH)) recommendations.append(('comments', '', OptionRecommendation.HIGH)) """ >>> Use to debug generated catalog code before pipeline conversion <<< """ GENERATE_DEBUG_EPUB = False if GENERATE_DEBUG_EPUB: catalog_debug_path = os.path.join(os.path.expanduser('~'), 'Desktop', 'Catalog debug') setattr(opts, 'debug_pipeline', os.path.expanduser(catalog_debug_path)) dp = getattr(opts, 'debug_pipeline', None) if dp is not None: recommendations.append(('debug_pipeline', dp, OptionRecommendation.HIGH)) if opts.output_profile and opts.output_profile.startswith("kindle"): recommendations.append(('output_profile', opts.output_profile, OptionRecommendation.HIGH)) recommendations.append(('book_producer', opts.output_profile, OptionRecommendation.HIGH)) if opts.fmt == 'mobi': recommendations.append(('no_inline_toc', True, OptionRecommendation.HIGH)) recommendations.append(('verbose', 2, OptionRecommendation.HIGH)) # Use existing cover or generate new cover cpath = None existing_cover = False try: search_text = 'title:"%s" author:%s' % ( opts.catalog_title.replace('"', '\\"'), 'calibre') matches = db.search(search_text, return_matches=True, sort_results=False) if matches: cpath = db.cover(matches[0], index_is_id=True, as_path=True) if cpath and os.path.exists(cpath): existing_cover = True except: pass if self.opts.use_existing_cover and not existing_cover: log.warning("no existing catalog cover found") if self.opts.use_existing_cover and existing_cover: recommendations.append(('cover', cpath, OptionRecommendation.HIGH)) log.info("using existing catalog cover") else: from calibre.ebooks.covers import calibre_cover2 log.info("replacing catalog cover") new_cover_path = PersistentTemporaryFile(suffix='.jpg') new_cover = calibre_cover2(opts.catalog_title, 'calibre') new_cover_path.write(new_cover) new_cover_path.close() recommendations.append(('cover', new_cover_path.name, OptionRecommendation.HIGH)) # Run ebook-convert from calibre.ebooks.conversion.plumber import Plumber plumber = Plumber(os.path.join(catalog.catalog_path, opts.basename + '.opf'), path_to_output, log, report_progress=notification, abort_after_input_dump=False) plumber.merge_ui_recommendations(recommendations) plumber.run() try: os.remove(cpath) except: pass if GENERATE_DEBUG_EPUB: from calibre.ebooks.epub import initialize_container from calibre.ebooks.tweak import zip_rebuilder from calibre.utils.zipfile import ZipFile input_path = os.path.join(catalog_debug_path, 'input') epub_shell = os.path.join(catalog_debug_path, 'epub_shell.zip') initialize_container(epub_shell, opf_name='content.opf') with ZipFile(epub_shell, 'r') as zf: zf.extractall(path=input_path) os.remove(epub_shell) zip_rebuilder(input_path, os.path.join(catalog_debug_path, 'input.epub')) if opts.verbose: log.info(" Catalog creation complete (%s)\n" % str(datetime.timedelta(seconds=int(time.time() - opts.start_time)))) # returns to gui2.actions.catalog:catalog_generated() return catalog.error
log.info("using existing catalog cover") else: log.info("replacing catalog cover") new_cover_path = PersistentTemporaryFile(suffix='.jpg') new_cover = calibre_cover(opts.catalog_title.replace('"', '\\"'), 'calibre') new_cover_path.write(new_cover) new_cover_path.close() recommendations.append(('cover', new_cover_path.name, OptionRecommendation.HIGH)) # Run ebook-convert from calibre.ebooks.conversion.plumber import Plumber plumber = Plumber(os.path.join(catalog.catalog_path, opts.basename + '.opf'), path_to_output, log, report_progress=notification, abort_after_input_dump=False) plumber.merge_ui_recommendations(recommendations) plumber.run() try: os.remove(cpath) except: pass if GENERATE_DEBUG_EPUB: from calibre.ebooks.epub import initialize_container from calibre.ebooks.tweak import zip_rebuilder from calibre.utils.zipfile import ZipFile input_path = os.path.join(catalog_debug_path, 'input') epub_shell = os.path.join(catalog_debug_path, 'epub_shell.zip') initialize_container(epub_shell, opf_name='content.opf') with ZipFile(epub_shell, 'r') as zf: zf.extractall(path=input_path)