From dc3a6a1f618aa964801762dc7ad8dccb945044fc Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 2 Apr 2009 06:10:50 -0400 Subject: [PATCH 1/3] Finish txt output --- src/calibre/ebooks/txt/output.py | 14 ++++---------- src/calibre/ebooks/txt/writer.py | 6 +++--- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/calibre/ebooks/txt/output.py b/src/calibre/ebooks/txt/output.py index 5e58d47ef1..c1e48d98fd 100644 --- a/src/calibre/ebooks/txt/output.py +++ b/src/calibre/ebooks/txt/output.py @@ -24,23 +24,17 @@ class TXTOutput(OutputFormatPlugin): 'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. ' 'For Mac OS X use \'unix\'. \'system\' will default to the newline ' 'type used by this OS.' % sorted(TxtNewlines.NEWLINE_TYPES.keys()))), - OptionRecommendation(name='prepend_author', recommended_value='true', - level=OptionRecommendation.LOW, long_switch='prepend_author', + OptionRecommendation(name='prepend_metadata', recommended_value='false', + level=OptionRecommendation.LOW, long_switch='prepend_metadata', choices=['true', 'false'], - help=_('Write the author to the beginning of the file. ' + help=_('Write the title and author to the beginning of the file. ' 'Default is \'true\'. Use \'false\' to disable.')), - OptionRecommendation(name='prepend_title', recommended_value='true', - choices=['true', 'false'], - level=OptionRecommendation.LOW, long_switch='prepend_title', - help=_('Write the title to the beginning of the file. ' - 'Default is \'true\'. Use \'false\' to disable.')) ]) def convert(self, oeb_book, output_path, input_plugin, opts, log): metadata = TxtMetadata() - if opts.prepend_author.lower() == 'true': + if opts.prepend_metadata.lower() == 'true': metadata.author = opts.authors if opts.authors else authors_to_string(oeb_book.metadata.authors.value) if oeb_book.metadata.authors != [] else _('Unknown') - if opts.prepend_title.lower() == 'true': metadata.title = opts.title if opts.title else oeb_book.metadata.title[0].value if oeb_book.metadata.title != [] else _('Unknown') writer = TxtWriter(TxtNewlines(opts.newline).newline, log) diff --git a/src/calibre/ebooks/txt/writer.py b/src/calibre/ebooks/txt/writer.py index 2fb5f9550a..efd3ec0a2f 100644 --- a/src/calibre/ebooks/txt/writer.py +++ b/src/calibre/ebooks/txt/writer.py @@ -34,9 +34,9 @@ class TxtWriter(object): # Prepend metadata if metadata.author != None and metadata.author != '': - out = (u'%s%s%s%s' % (metadata.author.upper(), self.newline, self.newline, self.newline)) + out - if metadata.title != None and metadata.title != '': - out = (u'%s%s%s%s' % (metadata.title.upper(), self.newline, self.newline, self.newline)) + out + if metadata.title != None and metadata.title != '': + out = (u'%s%s%s%s' % (metadata.author.upper(), self.newline, self.newline, self.newline)) + out + out = (u'%s%s%s%s' % (metadata.title.upper(), self.newline, self.newline, self.newline)) + out # Put two blank lines at end of file end = out[-3 * len(self.newline):] From 1d669699ba76d856e5af7b2e4199af0cf30c9343 Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 2 Apr 2009 06:38:55 -0400 Subject: [PATCH 2/3] PDF output complete --- src/calibre/ebooks/pdf/output.py | 45 +++++++++++++++++++------------- src/calibre/ebooks/pdf/writer.py | 12 +++++---- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/calibre/ebooks/pdf/output.py b/src/calibre/ebooks/pdf/output.py index 5af4e4bed7..e76bcdd3d7 100644 --- a/src/calibre/ebooks/pdf/output.py +++ b/src/calibre/ebooks/pdf/output.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from __future__ import with_statement + __license__ = 'GPL 3' __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' @@ -9,10 +11,12 @@ Convert OEB ebook format to PDF. #unit, papersize, orientation, custom_size, profile -import os +import os, glob from calibre.customize.conversion import OutputFormatPlugin, \ OptionRecommendation +from calibre.ebooks.oeb.output import OEBOutput +from calibre.ptempfile import TemporaryDirectory from calibre.ebooks.pdf.writer import PDFWriter from calibre.ebooks.pdf.pageoptions import UNITS, unit, PAPER_SIZES, \ paper_size, ORIENTATIONS, orientation, PageOptions @@ -65,21 +69,26 @@ class PDFOutput(OutputFormatPlugin): popts.unit = unit(opts.unit) popts.paper_size = paper_size(opts.paper_size) popts.orientation = orientation(opts.orientation) - - writer = PDFWriter(log, popts) + + with TemporaryDirectory('_any2pdf') as oebdir: + OEBOutput(None).convert(oeb_book, oebdir, input_plugin, opts, log) + + opf = glob.glob(os.path.join(oebdir, '*.opf'))[0] + + writer = PDFWriter(log, popts) - close = False - if not hasattr(output_path, 'write'): - close = True - if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '': - os.makedirs(os.path.dirname(output_path)) - out_stream = open(output_path, 'wb') - else: - out_stream = output_path - - out_stream.seek(0) - out_stream.truncate() - writer.dump(oeb_book.spine, out_stream) - - if close: - out_stream.close() + close = False + if not hasattr(output_path, 'write'): + close = True + if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '': + os.makedirs(os.path.dirname(output_path)) + out_stream = open(output_path, 'wb') + else: + out_stream = output_path + + out_stream.seek(0) + out_stream.truncate() + writer.dump(opf, out_stream) + + if close: + out_stream.close() diff --git a/src/calibre/ebooks/pdf/writer.py b/src/calibre/ebooks/pdf/writer.py index cf77aebc14..a618213189 100644 --- a/src/calibre/ebooks/pdf/writer.py +++ b/src/calibre/ebooks/pdf/writer.py @@ -13,6 +13,7 @@ import os, shutil, sys from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ebooks.pdf.pageoptions import PageOptions +from calibre.ebooks.metadata.opf2 import OPF from PyQt4 import QtCore from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, QApplication, QPrinter, \ @@ -37,10 +38,11 @@ class PDFWriter(QObject): self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts') self.popts = popts - def dump(self, spine, out_stream): + def dump(self, opfpath, out_stream): self._delete_tmpdir() - self.render_queue = spine[:] + opf = OPF(opfpath, os.path.dirname(opfpath)) + self.render_queue = [i.path for i in opf.spine] self.combine_queue = [] self.out_stream = out_stream @@ -56,7 +58,7 @@ class PDFWriter(QObject): def _render_next(self): item = str(self.render_queue.pop(0)) - self.combine_queue.append(os.path.join(self.tmp_path, '%s_%i.pdf' % (os.path.basename(item), len(self.combine_queue)))) + self.combine_queue.append(os.path.join(self.tmp_path, '%i.pdf' % (len(self.combine_queue) + 1))) self.logger.info('Processing %s...' % item) @@ -64,9 +66,9 @@ class PDFWriter(QObject): def _render_html(self, ok): if ok: - item_path = os.path.join(self.tmp_path, '%s_%i.pdf' % (os.path.basename(str(self.view.url().toLocalFile())), len(self.combine_queue) - 1)) + item_path = os.path.join(self.tmp_path, '%i.pdf' % len(self.combine_queue)) - self.logger.debug('\tRendering item as %s' % item_path) + self.logger.debug('\tRendering item %s as %i' % (os.path.basename(str(self.view.url().toLocalFile())), len(self.combine_queue))) printer = QPrinter(QPrinter.HighResolution) printer.setPageMargins(self.popts.margin_left, self.popts.margin_top, self.popts.margin_right, self.popts.margin_bottom, self.popts.unit) From 57e643caf6614688a8e88c453d5e3951fcbf02a9 Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 2 Apr 2009 06:44:55 -0400 Subject: [PATCH 3/3] remove any2pdf references --- src/calibre/ebooks/pdf/output.py | 2 +- src/calibre/ebooks/pdf/writer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/pdf/output.py b/src/calibre/ebooks/pdf/output.py index e76bcdd3d7..230beed9ae 100644 --- a/src/calibre/ebooks/pdf/output.py +++ b/src/calibre/ebooks/pdf/output.py @@ -70,7 +70,7 @@ class PDFOutput(OutputFormatPlugin): popts.paper_size = paper_size(opts.paper_size) popts.orientation = orientation(opts.orientation) - with TemporaryDirectory('_any2pdf') as oebdir: + with TemporaryDirectory('_pdf_out') as oebdir: OEBOutput(None).convert(oeb_book, oebdir, input_plugin, opts, log) opf = glob.glob(os.path.join(oebdir, '*.opf'))[0] diff --git a/src/calibre/ebooks/pdf/writer.py b/src/calibre/ebooks/pdf/writer.py index a618213189..2aebd7322c 100644 --- a/src/calibre/ebooks/pdf/writer.py +++ b/src/calibre/ebooks/pdf/writer.py @@ -82,7 +82,7 @@ class PDFWriter(QObject): def _delete_tmpdir(self): if os.path.exists(self.tmp_path): shutil.rmtree(self.tmp_path, True) - self.tmp_path = PersistentTemporaryDirectory('_any2pdf_parts') + self.tmp_path = PersistentTemporaryDirectory('_pdf_out_parts') def _write(self): self.logger.info('Combining individual PDF parts...')