Sync to pluginize

This commit is contained in:
John Schember 2009-04-11 22:23:35 -04:00
commit 00f24eaf5d
5 changed files with 93 additions and 63 deletions

View File

@ -255,11 +255,11 @@ def plugin_for_input_format(fmt):
return plugin return plugin
def available_input_formats(): def available_input_formats():
formats = [] formats = set([])
for plugin in input_format_plugins(): for plugin in input_format_plugins():
if not is_disabled(plugin): if not is_disabled(plugin):
for format in plugin.file_types: for format in plugin.file_types:
formats.append(format) formats.add(format)
return formats return formats
def output_format_plugins(): def output_format_plugins():
@ -273,10 +273,10 @@ def plugin_for_output_format(fmt):
return plugin return plugin
def available_output_formats(): def available_output_formats():
formats = [] formats = set([])
for plugin in output_format_plugins(): for plugin in output_format_plugins():
if not is_disabled(plugin): if not is_disabled(plugin):
formats.append(plugin.file_type) formats.add(plugin.file_type)
return formats return formats
def disable_plugin(plugin_or_name): def disable_plugin(plugin_or_name):

View File

@ -179,6 +179,9 @@ OptionRecommendation(name='language',
raise ValueError('Input file must have an extension') raise ValueError('Input file must have an extension')
input_fmt = input_fmt[1:].lower() input_fmt = input_fmt[1:].lower()
if os.path.exists(output) and os.path.isdir(output):
output_fmt = 'oeb'
else:
output_fmt = os.path.splitext(output)[1] output_fmt = os.path.splitext(output)[1]
if not output_fmt: if not output_fmt:
output_fmt = '.oeb' output_fmt = '.oeb'

View File

@ -5,20 +5,20 @@ __copyright__ = '2008 Kovid Goyal <kovid at kovidgoyal.net>'
Iterate over the HTML files in an ebook. Useful for writing viewers. Iterate over the HTML files in an ebook. Useful for writing viewers.
''' '''
import re, os, math, copy import re, os, math
from cStringIO import StringIO from cStringIO import StringIO
from PyQt4.Qt import QFontDatabase from PyQt4.Qt import QFontDatabase
from calibre.ebooks.epub.from_any import MAP from calibre.customize.ui import available_input_formats
from calibre.ebooks.epub.from_html import TITLEPAGE from calibre.ebooks.epub.from_html import TITLEPAGE
from calibre.ebooks.epub import config from calibre.ebooks.metadata.opf2 import OPF, OPFCreator
from calibre.ebooks.metadata.opf2 import OPF
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.ebooks.html_old import create_dir
from calibre.utils.zipfile import safe_replace, ZipFile from calibre.utils.zipfile import safe_replace, ZipFile
from calibre.utils.config import DynamicConfig from calibre.utils.config import DynamicConfig
from calibre.utils.logging import Log
from calibre import CurrentDir
def character_count(html): def character_count(html):
''' '''
@ -50,11 +50,28 @@ class SpineItem(unicode):
obj.max_page = -1 obj.max_page = -1
return obj return obj
def html2opf(path, tdir, opts): class FakeOpts(object):
opts = copy.copy(opts) verbose = 0
opts.output = tdir breadth_first = False
create_dir(path, opts) max_levels = 5
return os.path.join(tdir, 'metadata.opf') input_encoding = None
def html2opf(path, tdir, log):
from calibre.ebooks.html.input import get_filelist
from calibre.ebooks.metadata.meta import get_metadata
with CurrentDir(tdir):
fl = get_filelist(path, tdir, FakeOpts(), log)
mi = get_metadata(open(path, 'rb'), 'html')
mi = OPFCreator(os.getcwdu(), mi)
mi.guide = None
entries = [(f.path, 'application/xhtml+xml') for f in fl]
mi.create_manifest(entries)
mi.create_spine([f.path for f in fl])
mi.render(open('metadata.opf', 'wb'))
opfpath = os.path.abspath('metadata.opf')
return opfpath
def opf2opf(path, tdir, opts): def opf2opf(path, tdir, opts):
return path return path
@ -62,24 +79,22 @@ def opf2opf(path, tdir, opts):
def is_supported(path): def is_supported(path):
ext = os.path.splitext(path)[1].replace('.', '').lower() ext = os.path.splitext(path)[1].replace('.', '').lower()
ext = re.sub(r'(x{0,1})htm(l{0,1})', 'html', ext) ext = re.sub(r'(x{0,1})htm(l{0,1})', 'html', ext)
return ext in list(MAP.keys())+['html', 'opf'] return ext in available_input_formats()
class EbookIterator(object): class EbookIterator(object):
CHARACTERS_PER_PAGE = 1000 CHARACTERS_PER_PAGE = 1000
def __init__(self, pathtoebook): def __init__(self, pathtoebook, log=None):
self.log = log
if log is None:
self.log = Log()
pathtoebook = pathtoebook.strip() pathtoebook = pathtoebook.strip()
self.pathtoebook = os.path.abspath(pathtoebook) self.pathtoebook = os.path.abspath(pathtoebook)
self.config = DynamicConfig(name='iterator') self.config = DynamicConfig(name='iterator')
ext = os.path.splitext(pathtoebook)[1].replace('.', '').lower() ext = os.path.splitext(pathtoebook)[1].replace('.', '').lower()
ext = re.sub(r'(x{0,1})htm(l{0,1})', 'html', ext) ext = re.sub(r'(x{0,1})htm(l{0,1})', 'html', ext)
map = dict(MAP) self.ebook_ext = ext
map['html'] = html2opf
map['opf'] = opf2opf
if ext not in map.keys():
raise UnsupportedFormatError(ext)
self.to_opf = map[ext]
def search(self, text, index): def search(self, text, index):
text = text.lower() text = text.lower()
@ -115,14 +130,24 @@ class EbookIterator(object):
def __enter__(self): def __enter__(self):
self._tdir = TemporaryDirectory('_ebook_iter') self._tdir = TemporaryDirectory('_ebook_iter')
self.base = self._tdir.__enter__() self.base = self._tdir.__enter__()
opts = config('').parse() if self.ebook_ext == 'opf':
self.pathtoopf = self.to_opf(self.pathtoebook, self.base, opts) self.pathtoopf = self.pathtoebook
elif self.ebook_ext == 'html':
self.pathtoopf = html2opf(self.pathtoebook, self.base, self.log)
else:
from calibre.ebooks.conversion.plumber import Plumber
plumber = Plumber(self.pathtoebook, self.base, self.log)
plumber.setup_options()
self.pathtoopf = plumber.input_plugin(open(plumber.input, 'rb'),
plumber.opts, plumber.input_fmt, self.log,
{}, self.base)
self.opf = OPF(self.pathtoopf, os.path.dirname(self.pathtoopf)) self.opf = OPF(self.pathtoopf, os.path.dirname(self.pathtoopf))
self.spine = [SpineItem(i.path) for i in self.opf.spine] self.spine = [SpineItem(i.path) for i in self.opf.spine]
cover = self.opf.cover cover = self.opf.cover
if os.path.splitext(self.pathtoebook)[1].lower() in \ if self.ebook_ext in ('lit', 'mobi', 'prc', 'opf') and cover:
('.lit', '.mobi', '.prc') and cover:
cfile = os.path.join(os.path.dirname(self.spine[0]), 'calibre_ei_cover.html') cfile = os.path.join(os.path.dirname(self.spine[0]), 'calibre_ei_cover.html')
open(cfile, 'wb').write(TITLEPAGE%cover) open(cfile, 'wb').write(TITLEPAGE%cover)
self.spine[0:0] = [SpineItem(cfile)] self.spine[0:0] = [SpineItem(cfile)]
@ -131,7 +156,6 @@ class EbookIterator(object):
self.opf.path_to_html_toc not in self.spine: self.opf.path_to_html_toc not in self.spine:
self.spine.append(SpineItem(self.opf.path_to_html_toc)) self.spine.append(SpineItem(self.opf.path_to_html_toc))
sizes = [i.character_count for i in self.spine] sizes = [i.character_count for i in self.spine]
self.pages = [math.ceil(i/float(self.CHARACTERS_PER_PAGE)) for i in sizes] self.pages = [math.ceil(i/float(self.CHARACTERS_PER_PAGE)) for i in sizes]
for p, s in zip(self.pages, self.spine): for p, s in zip(self.pages, self.spine):

View File

@ -17,14 +17,14 @@ from calibre.gui2.viewer.bookmarkmanager import BookmarkManager
from calibre.gui2.main_window import MainWindow from calibre.gui2.main_window import MainWindow
from calibre.gui2 import Application, ORG_NAME, APP_UID, choose_files, \ from calibre.gui2 import Application, ORG_NAME, APP_UID, choose_files, \
info_dialog, error_dialog info_dialog, error_dialog
from calibre.ebooks.epub.iterator import EbookIterator from calibre.ebooks.oeb.iterator import EbookIterator
from calibre.ebooks.epub.from_any import SOURCE_FORMATS
from calibre.ebooks import DRMError from calibre.ebooks import DRMError
from calibre.gui2.dialogs.conversion_error import ConversionErrorDialog from calibre.gui2.dialogs.conversion_error import ConversionErrorDialog
from calibre.constants import islinux from calibre.constants import islinux
from calibre.utils.config import Config, StringConfig from calibre.utils.config import Config, StringConfig
from calibre.gui2.library import SearchBox from calibre.gui2.library import SearchBox
from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata import MetaInformation
from calibre.customize.ui import available_input_formats
class TOCItem(QStandardItem): class TOCItem(QStandardItem):
@ -362,7 +362,8 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
def open_ebook(self, checked): def open_ebook(self, checked):
files = choose_files(self, 'ebook viewer open dialog', files = choose_files(self, 'ebook viewer open dialog',
_('Choose ebook'), _('Choose ebook'),
[(_('Ebooks'), SOURCE_FORMATS)], all_files=False, [(_('Ebooks'), available_input_formats())],
all_files=False,
select_only_single_file=True) select_only_single_file=True)
if files: if files:
self.load_ebook(files[0]) self.load_ebook(files[0])

View File

@ -445,6 +445,8 @@ class BasicNewsRecipe(object):
:param progress_reporter: A Callable that takes two arguments: progress (a number between 0 and 1) and a string message. The message should be optional. :param progress_reporter: A Callable that takes two arguments: progress (a number between 0 and 1) and a string message. The message should be optional.
''' '''
self.log = Log() self.log = Log()
if options.verbose:
self.log.filter_level = self.log.DEBUG
if not isinstance(self.title, unicode): if not isinstance(self.title, unicode):
self.title = unicode(self.title, 'utf-8', 'replace') self.title = unicode(self.title, 'utf-8', 'replace')