1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-03-18 15:53:41 +01:00

Next portion of logging adapt.

This commit is contained in:
2021-06-24 19:42:51 +02:00
parent 84abd62a61
commit 6dfcaea2dd
20 changed files with 105 additions and 122 deletions

View File

@@ -1,16 +1,17 @@
import unittest, sys, argparse import argparse
import sys
import unittest
from lxml import etree, html from lxml import etree
from lxml import html
from ebook_converter.css_selectors.errors import SelectorSyntaxError, ExpressionError from ebook_converter.css_selectors.errors import ExpressionError
from ebook_converter.css_selectors.parser import tokenize, parse from ebook_converter.css_selectors.errors import SelectorSyntaxError
from ebook_converter.css_selectors.parser import parse
from ebook_converter.css_selectors.parser import tokenize
from ebook_converter.css_selectors.select import Select from ebook_converter.css_selectors.select import Select
__license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
class TestCSSSelectors(unittest.TestCase): class TestCSSSelectors(unittest.TestCase):
# Test data {{{ # Test data {{{

View File

@@ -197,9 +197,9 @@ class InputFormatPlugin(Plugin):
def __call__(self, stream, options, file_ext, log, def __call__(self, stream, options, file_ext, log,
accelerators, output_dir): accelerators, output_dir):
try: try:
log('InputFormatPlugin: %s running'%self.name) log.info('InputFormatPlugin: %s running', self.name)
if hasattr(stream, 'name'): if hasattr(stream, 'name'):
log('on', stream.name) log.info('on: %s', stream.name)
except: except:
# In case stdout is broken # In case stdout is broken
pass pass

View File

@@ -4,10 +4,6 @@ from collections import namedtuple
from ebook_converter.customize import Plugin from ebook_converter.customize import Plugin
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
class DevicePlugin(Plugin): class DevicePlugin(Plugin):
""" """
Defines the interface that should be implemented by backends that Defines the interface that should be implemented by backends that
@@ -16,7 +12,7 @@ class DevicePlugin(Plugin):
type = 'Device interface' type = 'Device interface'
#: Ordered list of supported formats #: Ordered list of supported formats
FORMATS = ["lrf", "rtf", "pdf", "txt"] FORMATS = ["lrf", "rtf", "pdf", "txt"]
# If True, the config dialog will not show the formats box # If True, the config dialog will not show the formats box
HIDE_FORMATS_CONFIG_BOX = False HIDE_FORMATS_CONFIG_BOX = False

View File

@@ -28,7 +28,7 @@ class CHMInput(InputFormatPlugin):
from ebook_converter.ebooks.chm.reader import CHMReader from ebook_converter.ebooks.chm.reader import CHMReader
log.debug('Opening CHM file') log.debug('Opening CHM file')
rdr = CHMReader(chm_path, log, input_encoding=self.opts.input_encoding) rdr = CHMReader(chm_path, log, input_encoding=self.opts.input_encoding)
log.debug('Extracting CHM to %s' % output_dir) log.debug('Extracting CHM to %s', output_dir)
rdr.extract_content(output_dir, debug_dump=debug_dump) rdr.extract_content(output_dir, debug_dump=debug_dump)
self._chm_reader = rdr self._chm_reader = rdr
return rdr.hhc_path return rdr.hhc_path
@@ -53,8 +53,8 @@ class CHMInput(InputFormatPlugin):
# closing stream so CHM can be opened by external library # closing stream so CHM can be opened by external library
stream.close() stream.close()
log.debug('tdir=%s' % tdir) log.debug('tdir=%s', tdir)
log.debug('stream.name=%s' % stream.name) log.debug('stream.name=%s', stream.name)
debug_dump = False debug_dump = False
odi = options.debug_pipeline odi = options.debug_pipeline
if odi: if odi:
@@ -121,7 +121,7 @@ class CHMInput(InputFormatPlugin):
resolve_entities=True)[0] resolve_entities=True)[0]
hhcroot = html.fromstring(hhcdata) hhcroot = html.fromstring(hhcdata)
toc = self._process_nodes(hhcroot) toc = self._process_nodes(hhcroot)
log.debug('Found %d section nodes' % toc.count()) log.debug('Found %d section nodes', toc.count())
htmlpath = os.path.splitext(hhcpath)[0] + ".html" htmlpath = os.path.splitext(hhcpath)[0] + ".html"
base = os.path.dirname(os.path.abspath(htmlpath)) base = os.path.dirname(os.path.abspath(htmlpath))

View File

@@ -9,11 +9,6 @@ from ebook_converter.ptempfile import PersistentTemporaryDirectory
from ebook_converter.utils import directory from ebook_converter.utils import directory
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
class ComicInput(InputFormatPlugin): class ComicInput(InputFormatPlugin):
name = 'Comic Input' name = 'Comic Input'
@@ -152,7 +147,7 @@ class ComicInput(InputFormatPlugin):
self.log.warning('Could not process the following pages ' self.log.warning('Could not process the following pages '
'(run with --verbose to see why):') '(run with --verbose to see why):')
for f in failures: for f in failures:
self.log.warning('\t', f) self.log.warning('\t%s', f)
if not new_pages: if not new_pages:
raise ValueError('Could not find any valid pages in comic: %s' raise ValueError('Could not find any valid pages in comic: %s'
% comic) % comic)

View File

@@ -90,7 +90,7 @@ class FB2Input(InputFormatPlugin):
ss = f.read() ss = f.read()
ss = ss.replace("__FB_NS__", fb_ns) ss = ss.replace("__FB_NS__", fb_ns)
if options.no_inline_fb2_toc: if options.no_inline_fb2_toc:
log('Disabling generation of inline FB2 TOC') log.info('Disabling generation of inline FB2 TOC')
ss = re.compile(r'<!-- BUILD TOC -->.*<!-- END BUILD TOC -->', ss = re.compile(r'<!-- BUILD TOC -->.*<!-- END BUILD TOC -->',
re.DOTALL).sub('', ss) re.DOTALL).sub('', ss)
@@ -180,7 +180,7 @@ class FB2Input(InputFormatPlugin):
data = base64_decode(raw) data = base64_decode(raw)
except TypeError: except TypeError:
self.log.exception('Binary data with id=%s is corrupted, ' self.log.exception('Binary data with id=%s is corrupted, '
'ignoring' % elem.get('id')) 'ignoring', elem.get('id'))
else: else:
with open(fname, 'wb') as f: with open(fname, 'wb') as f:
f.write(data) f.write(data)

View File

@@ -3,11 +3,6 @@ import os
from ebook_converter.customize.conversion import OutputFormatPlugin, OptionRecommendation from ebook_converter.customize.conversion import OutputFormatPlugin, OptionRecommendation
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
class FB2Output(OutputFormatPlugin): class FB2Output(OutputFormatPlugin):
name = 'FB2 Output' name = 'FB2 Output'
@@ -177,7 +172,8 @@ class FB2Output(OutputFormatPlugin):
rasterizer = SVGRasterizer() rasterizer = SVGRasterizer()
rasterizer(oeb_book, opts) rasterizer(oeb_book, opts)
except Unavailable: except Unavailable:
log.warn('SVG rasterizer unavailable, SVG will not be converted') log.warning('SVG rasterizer unavailable, SVG will not be '
'converted')
linearize_jacket(oeb_book) linearize_jacket(oeb_book)

View File

@@ -101,7 +101,7 @@ class HTMLInput(InputFormatPlugin):
if not metadata.language: if not metadata.language:
l = canonicalize_lang(getattr(opts, 'language', None)) l = canonicalize_lang(getattr(opts, 'language', None))
if not l: if not l:
oeb.logger.warn('Language not specified') oeb.logger.warning('Language not specified')
l = get_lang().replace('_', '-') l = get_lang().replace('_', '-')
metadata.add('language', l) metadata.add('language', l)
if not metadata.creator: if not metadata.creator:
@@ -109,12 +109,12 @@ class HTMLInput(InputFormatPlugin):
if a: if a:
a = string_to_authors(a) a = string_to_authors(a)
if not a: if not a:
oeb.logger.warn('Creator not specified') oeb.logger.warning('Creator not specified')
a = [self.oeb.translate('Unknown')] a = [self.oeb.translate('Unknown')]
for aut in a: for aut in a:
metadata.add('creator', aut) metadata.add('creator', aut)
if not metadata.title: if not metadata.title:
oeb.logger.warn('Title not specified') oeb.logger.warning('Title not specified')
metadata.add('title', self.oeb.translate('Unknown')) metadata.add('title', self.oeb.translate('Unknown'))
bookid = str(uuid.uuid4()) bookid = str(uuid.uuid4())
metadata.add('identifier', bookid, id='uuid_id', scheme='uuid') metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
@@ -141,14 +141,14 @@ class HTMLInput(InputFormatPlugin):
self.added_resources = {} self.added_resources = {}
self.log = log self.log = log
self.log('Normalizing filename cases') self.log.info('Normalizing filename cases')
for path, href in htmlfile_map.items(): for path, href in htmlfile_map.items():
self.added_resources[path] = href self.added_resources[path] = href
self.urlnormalize, self.DirContainer = urlnormalize, DirContainer self.urlnormalize, self.DirContainer = urlnormalize, DirContainer
self.urldefrag = urllib.parse.urldefrag self.urldefrag = urllib.parse.urldefrag
self.BINARY_MIME = BINARY_MIME self.BINARY_MIME = BINARY_MIME
self.log('Rewriting HTML links') self.log.info('Rewriting HTML links')
for f in filelist: for f in filelist:
path = f.path path = f.path
dpath = os.path.dirname(path) dpath = os.path.dirname(path)
@@ -208,12 +208,12 @@ class HTMLInput(InputFormatPlugin):
try: try:
link_ = link_.decode('utf-8', 'error') link_ = link_.decode('utf-8', 'error')
except: except:
self.log.warn('Failed to decode link %r. Ignoring'%link_) self.log.warning('Failed to decode link %r. Ignoring', link_)
return None, None return None, None
try: try:
l = Link(link_, base if base else os.getcwd()) l = Link(link_, base if base else os.getcwd())
except: except:
self.log.exception('Failed to process link: %r'%link_) self.log.exception('Failed to process link: %r', link_)
return None, None return None, None
if l.path is None: if l.path is None:
# Not a local resource # Not a local resource
@@ -237,7 +237,7 @@ class HTMLInput(InputFormatPlugin):
if not os.access(link, os.R_OK): if not os.access(link, os.R_OK):
return link_ return link_
if os.path.isdir(link): if os.path.isdir(link):
self.log.warn(link_, 'is a link to a directory. Ignoring.') self.log.warning(link_, 'is a link to a directory. Ignoring.')
return link_ return link_
if link not in self.added_resources: if link not in self.added_resources:
bhref = os.path.basename(link) bhref = os.path.basename(link)
@@ -245,7 +245,7 @@ class HTMLInput(InputFormatPlugin):
guessed = mimetypes.guess_type(href)[0] guessed = mimetypes.guess_type(href)[0]
media_type = guessed or self.BINARY_MIME media_type = guessed or self.BINARY_MIME
if media_type == 'text/plain': if media_type == 'text/plain':
self.log.warn('Ignoring link to text file %r'%link_) self.log.warning('Ignoring link to text file %r', link_)
return None return None
if media_type == self.BINARY_MIME: if media_type == self.BINARY_MIME:
# Check for the common case, images # Check for the common case, images
@@ -257,7 +257,7 @@ class HTMLInput(InputFormatPlugin):
if img: if img:
media_type = mimetypes.guess_type('dummy.'+img)[0] or self.BINARY_MIME media_type = mimetypes.guess_type('dummy.'+img)[0] or self.BINARY_MIME
self.oeb.log.debug('Added', link) self.oeb.log.debug('Added %s', link)
self.oeb.container = self.DirContainer(os.path.dirname(link), self.oeb.container = self.DirContainer(os.path.dirname(link),
self.oeb.log, ignore_opf=True) self.oeb.log, ignore_opf=True)
# Load into memory # Load into memory
@@ -287,6 +287,6 @@ class HTMLInput(InputFormatPlugin):
raw = f.read().decode('utf-8', 'replace') raw = f.read().decode('utf-8', 'replace')
raw = self.oeb.css_preprocessor(raw, add_namespace=False) raw = self.oeb.css_preprocessor(raw, add_namespace=False)
except: except:
self.log.exception('Failed to read CSS file: %r'%link) self.log.exception('Failed to read CSS file: %r', link)
return (None, None) return (None, None)
return (None, raw) return (None, raw)

View File

@@ -228,7 +228,7 @@ class HTMLOutput(OutputFormatPlugin):
shutil.rmtree(opts.extract_to) shutil.rmtree(opts.extract_to)
os.makedirs(opts.extract_to) os.makedirs(opts.extract_to)
zfile.extractall(opts.extract_to) zfile.extractall(opts.extract_to)
self.log('Zip file extracted to', opts.extract_to) self.log.info('Zip file extracted to %s', opts.extract_to)
zfile.close() zfile.close()

View File

@@ -55,8 +55,8 @@ class HTMLZInput(InputFormatPlugin):
# HTMLZ archive probably won't turn out as the user expects. With # HTMLZ archive probably won't turn out as the user expects. With
# Multiple HTML files ZIP input should be used in place of HTMLZ. # Multiple HTML files ZIP input should be used in place of HTMLZ.
if multiple_html: if multiple_html:
log.warn('Multiple HTML files found in the archive. Only %s will ' log.warning('Multiple HTML files found in the archive. Only %s '
'be used.' % index) 'will be used.', index)
if index: if index:
with open(index, 'rb') as tf: with open(index, 'rb') as tf:

View File

@@ -40,8 +40,8 @@ class LITInput(InputFormatPlugin):
from ebook_converter.ebooks.txt.processor import \ from ebook_converter.ebooks.txt.processor import \
convert_basic, separate_paragraphs_single_line convert_basic, separate_paragraphs_single_line
from ebook_converter.ebooks.chardet import xml_to_unicode from ebook_converter.ebooks.chardet import xml_to_unicode
self.log('LIT file with all text in singe <pre> tag ' self.log.info('LIT file with all text in singe <pre> tag '
'detected') 'detected')
html = separate_paragraphs_single_line(pre.text) html = separate_paragraphs_single_line(pre.text)
html = convert_basic(html).replace('<html>', html = convert_basic(html).replace('<html>',
'<html xmlns="%s">' % '<html xmlns="%s">' %

View File

@@ -3,11 +3,6 @@ import os
from ebook_converter.customize.conversion import InputFormatPlugin from ebook_converter.customize.conversion import InputFormatPlugin
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
class MOBIInput(InputFormatPlugin): class MOBIInput(InputFormatPlugin):
name = 'MOBI Input' name = 'MOBI Input'
@@ -37,7 +32,7 @@ class MOBIInput(InputFormatPlugin):
mr.extract_content('.', parse_cache) mr.extract_content('.', parse_cache)
if mr.kf8_type is not None: if mr.kf8_type is not None:
log('Found KF8 MOBI of type %r'%mr.kf8_type) log.info('Found KF8 MOBI of type %r', mr.kf8_type)
if mr.kf8_type == 'joint': if mr.kf8_type == 'joint':
self.mobi_is_joint = True self.mobi_is_joint = True
from ebook_converter.ebooks.mobi.reader.mobi8 import Mobi8Reader from ebook_converter.ebooks.mobi.reader.mobi8 import Mobi8Reader

View File

@@ -2,11 +2,6 @@ from ebook_converter.customize.conversion import (OutputFormatPlugin,
OptionRecommendation) OptionRecommendation)
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
def remove_html_cover(oeb, log): def remove_html_cover(oeb, log):
from ebook_converter.ebooks.oeb.base import OEB_DOCS from ebook_converter.ebooks.oeb.base import OEB_DOCS
@@ -17,10 +12,10 @@ def remove_html_cover(oeb, log):
del oeb.guide['cover'] del oeb.guide['cover']
item = oeb.manifest.hrefs[href] item = oeb.manifest.hrefs[href]
if item.spine_position is not None: if item.spine_position is not None:
log.warn('Found an HTML cover: ', item.href, 'removing it.', log.warning('Found an HTML cover: %s', 'removing it. If you find some '
'If you find some content missing from the output MOBI, it ' 'content missing from the output MOBI, it is because you '
'is because you misidentified the HTML cover in the input ' 'misidentified the HTML cover in the input document',
'document') item.href)
oeb.spine.remove(item) oeb.spine.remove(item)
if item.media_type in OEB_DOCS: if item.media_type in OEB_DOCS:
oeb.manifest.remove(item) oeb.manifest.remove(item)
@@ -117,7 +112,8 @@ class MOBIOutput(OutputFormatPlugin):
found = 'masthead' in self.oeb.guide found = 'masthead' in self.oeb.guide
if not found: if not found:
from ebook_converter.ebooks import generate_masthead from ebook_converter.ebooks import generate_masthead
self.oeb.log.debug('No masthead found in manifest, generating default mastheadImage...') self.oeb.log.debug('No masthead found in manifest, generating '
'default mastheadImage...')
raw = generate_masthead(str(self.oeb.metadata['title'][0])) raw = generate_masthead(str(self.oeb.metadata['title'][0]))
id, href = self.oeb.manifest.generate('masthead', 'masthead') id, href = self.oeb.manifest.generate('masthead', 'masthead')
self.oeb.manifest.add(id, href, 'image/gif', data=raw) self.oeb.manifest.add(id, href, 'image/gif', data=raw)
@@ -132,7 +128,7 @@ class MOBIOutput(OutputFormatPlugin):
return return
if toc and toc[0].klass != 'periodical': if toc and toc[0].klass != 'periodical':
one, two = self.oeb.spine[0], self.oeb.spine[1] one, two = self.oeb.spine[0], self.oeb.spine[1]
self.log('Converting TOC for MOBI periodical indexing...') self.log.info('Converting TOC for MOBI periodical indexing...')
articles = {} articles = {}
if toc.depth() < 3: if toc.depth() < 3:
@@ -206,7 +202,7 @@ class MOBIOutput(OutputFormatPlugin):
extract_mobi(output_path, opts) extract_mobi(output_path, opts)
return return
self.log('Creating MOBI 6 output') self.log.info('Creating MOBI 6 output')
self.write_mobi(input_plugin, output_path, kf8, resources) self.write_mobi(input_plugin, output_path, kf8, resources)
def create_kf8(self, resources, for_joint=False): def create_kf8(self, resources, for_joint=False):
@@ -232,7 +228,8 @@ class MOBIOutput(OutputFormatPlugin):
rasterizer = SVGRasterizer() rasterizer = SVGRasterizer()
rasterizer(oeb, opts) rasterizer(oeb, opts)
except Unavailable: except Unavailable:
self.log.warn('SVG rasterizer unavailable, SVG will not be converted') self.log.warning('SVG rasterizer unavailable, SVG will not be '
'converted')
else: else:
# Add rasterized SVG images # Add rasterized SVG images
resources.add_extra_images() resources.add_extra_images()

View File

@@ -34,13 +34,16 @@ class OEBOutput(OutputFormatPlugin):
try: try:
self.workaround_nook_cover_bug(root) self.workaround_nook_cover_bug(root)
except: except:
self.log.exception('Something went wrong while trying to' self.log.exception('Something went wrong while '
' workaround Nook cover bug, ignoring') 'trying to workaround Nook '
'cover bug, ignoring')
try: try:
self.workaround_pocketbook_cover_bug(root) self.workaround_pocketbook_cover_bug(root)
except: except:
self.log.exception('Something went wrong while trying to' self.log.exception('Something went wrong while '
' workaround Pocketbook cover bug, ignoring') 'trying to workaround '
'Pocketbook cover bug, '
'ignoring')
self.migrate_lang_code(root) self.migrate_lang_code(root)
raw = etree.tostring(root, pretty_print=True, raw = etree.tostring(root, pretty_print=True,
encoding='utf-8', xml_declaration=True) encoding='utf-8', xml_declaration=True)
@@ -80,9 +83,10 @@ class OEBOutput(OutputFormatPlugin):
manifest_item = manifest_items_with_id(covid) manifest_item = manifest_items_with_id(covid)
if len(manifest_item) == 1 and \ if len(manifest_item) == 1 and \
manifest_item[0].get('media-type', manifest_item[0].get('media-type',
'').startswith('image/'): '').startswith('image/'):
self.log.warn('The cover image has an id != "cover". Renaming' self.log.warning('The cover image has an id != "cover". '
' to work around bug in Nook Color') 'Renaming to work around bug in Nook '
'Color')
from ebook_converter.ebooks.oeb.base import uuid_id from ebook_converter.ebooks.oeb.base import uuid_id
newid = uuid_id() newid = uuid_id()

View File

@@ -25,8 +25,8 @@ class PDBInput(InputFormatPlugin):
(header.ident, (header.ident,
IDENTITY_TO_NAME.get(header.ident, 'Unknown'))) IDENTITY_TO_NAME.get(header.ident, 'Unknown')))
log.debug('Detected ebook format as: %s with identity: %s' % log.debug('Detected ebook format as: %s with identity: %s',
(IDENTITY_TO_NAME[header.ident], header.ident)) IDENTITY_TO_NAME[header.ident], header.ident)
reader = Reader(header, stream, log, options) reader = Reader(header, stream, log, options)
opf = reader.extract_content(os.getcwd()) opf = reader.extract_content(os.getcwd())

View File

@@ -6,11 +6,6 @@ from ebook_converter.customize.conversion import InputFormatPlugin
from ebook_converter.ptempfile import TemporaryDirectory from ebook_converter.ptempfile import TemporaryDirectory
__license__ = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
class PMLInput(InputFormatPlugin): class PMLInput(InputFormatPlugin):
name = 'PML Input' name = 'PML Input'
@@ -107,7 +102,7 @@ class PMLInput(InputFormatPlugin):
html_path = os.path.join(os.getcwd(), html_name) html_path = os.path.join(os.getcwd(), html_name)
pages.append(html_name) pages.append(html_name)
log.debug('Processing PML item %s...' % pml) log.debug('Processing PML item %s...', pml)
ttoc = self.process_pml(pml, html_path) ttoc = self.process_pml(pml, html_path)
toc += ttoc toc += ttoc
images = self.get_images(stream, tdir, True) images = self.get_images(stream, tdir, True)

View File

@@ -63,7 +63,7 @@ class RecipeInput(InputFormatPlugin):
if os.environ.get('CALIBRE_RECIPE_URN'): if os.environ.get('CALIBRE_RECIPE_URN'):
from ebook_converter.web.feeds.recipes.collection import get_custom_recipe, get_builtin_recipe_by_id from ebook_converter.web.feeds.recipes.collection import get_custom_recipe, get_builtin_recipe_by_id
urn = os.environ['CALIBRE_RECIPE_URN'] urn = os.environ['CALIBRE_RECIPE_URN']
log('Downloading recipe urn: ' + urn) log.info('Downloading recipe urn: %s', urn)
rtype, recipe_id = urn.partition(':')[::2] rtype, recipe_id = urn.partition(':')[::2]
if not recipe_id: if not recipe_id:
raise ValueError('Invalid recipe urn: ' + urn) raise ValueError('Invalid recipe urn: ' + urn)
@@ -80,7 +80,7 @@ class RecipeInput(InputFormatPlugin):
with open(recipe_or_file, 'rb') as f: with open(recipe_or_file, 'rb') as f:
self.recipe_source = f.read() self.recipe_source = f.read()
recipe = compile_recipe(self.recipe_source) recipe = compile_recipe(self.recipe_source)
log('Using custom recipe') log.info('Using custom recipe')
else: else:
from ebook_converter.web.feeds.recipes.collection import ( from ebook_converter.web.feeds.recipes.collection import (
get_builtin_recipe_by_title, get_builtin_recipe_titles) get_builtin_recipe_by_title, get_builtin_recipe_titles)
@@ -98,16 +98,16 @@ class RecipeInput(InputFormatPlugin):
recipe = compile_recipe(raw) recipe = compile_recipe(raw)
self.recipe_source = raw self.recipe_source = raw
if recipe.requires_version > numeric_version: if recipe.requires_version > numeric_version:
log.warn( log.warning('Downloaded recipe needs calibre version '
'Downloaded recipe needs calibre version at least: %s' % 'at least: %s',
('.'.join(recipe.requires_version))) '.'.join(recipe.requires_version))
builtin = True builtin = True
except: except:
log.exception('Failed to compile downloaded recipe. Falling ' log.exception('Failed to compile downloaded recipe. '
'back to builtin one') 'Falling back to builtin one')
builtin = True builtin = True
if builtin: if builtin:
log('Using bundled builtin recipe') log.info('Using bundled builtin recipe')
raw = get_builtin_recipe_by_title(title, log=log, raw = get_builtin_recipe_by_title(title, log=log,
download_recipe=False) download_recipe=False)
if raw is None: if raw is None:
@@ -115,7 +115,7 @@ class RecipeInput(InputFormatPlugin):
recipe = compile_recipe(raw) recipe = compile_recipe(raw)
self.recipe_source = raw self.recipe_source = raw
else: else:
log('Using downloaded builtin recipe') log.info('Using downloaded builtin recipe')
if recipe is None: if recipe is None:
raise ValueError('%r is not a valid recipe file or builtin recipe' % raise ValueError('%r is not a valid recipe file or builtin recipe' %

View File

@@ -64,9 +64,9 @@ class RTFInput(InputFormatPlugin):
debug_dir = u'rtfdebug' debug_dir = u'rtfdebug'
run_lev = 4 run_lev = 4
indent_out = 1 indent_out = 1
self.log('Running RTFParser in debug mode') self.log.info('Running RTFParser in debug mode')
except Exception: except Exception:
self.log.warn('Impossible to run RTFParser in debug mode') self.log.warning('Impossible to run RTFParser in debug mode')
parser = ParseRtf( parser = ParseRtf(
in_file=stream, in_file=stream,
out_file=ofile, out_file=ofile,
@@ -122,7 +122,7 @@ class RTFInput(InputFormatPlugin):
def extract_images(self, picts): def extract_images(self, picts):
from ebook_converter.utils.imghdr import what from ebook_converter.utils.imghdr import what
from binascii import unhexlify from binascii import unhexlify
self.log('Extracting images...') self.log.info('Extracting images...')
with open(picts, 'rb') as f: with open(picts, 'rb') as f:
raw = f.read() raw = f.read()
@@ -154,7 +154,7 @@ class RTFInput(InputFormatPlugin):
try: try:
imap[count] = self.convert_image(val) imap[count] = self.convert_image(val)
except Exception: except Exception:
self.log.exception('Failed to convert', val) self.log.exception('Failed to convert %s', val)
return imap return imap
def convert_image(self, name): def convert_image(self, name):
@@ -163,7 +163,7 @@ class RTFInput(InputFormatPlugin):
try: try:
return self.rasterize_wmf(name) return self.rasterize_wmf(name)
except Exception: except Exception:
self.log.exception('Failed to convert WMF image %r' % name) self.log.exception('Failed to convert WMF image %r', name)
return self.replace_wmf(name) return self.replace_wmf(name)
def replace_wmf(self, name): def replace_wmf(self, name):
@@ -258,7 +258,7 @@ class RTFInput(InputFormatPlugin):
from ebook_converter.ebooks.rtf.input import InlineClass from ebook_converter.ebooks.rtf.input import InlineClass
self.opts = options self.opts = options
self.log = log self.log = log
self.log('Converting RTF to XML...') self.log.info('Converting RTF to XML...')
try: try:
xml = self.generate_xml(stream.name) xml = self.generate_xml(stream.name)
except RtfInvalidCodeException as e: except RtfInvalidCodeException as e:
@@ -275,7 +275,7 @@ class RTFInput(InputFormatPlugin):
except Exception: except Exception:
self.log.exception('Failed to extract images...') self.log.exception('Failed to extract images...')
self.log('Parsing XML...') self.log.info('Parsing XML...')
doc = etree.fromstring(xml) doc = etree.fromstring(xml)
border_styles = self.convert_borders(doc) border_styles = self.convert_borders(doc)
for pict in doc.xpath('//rtf:pict[@num]', for pict in doc.xpath('//rtf:pict[@num]',
@@ -286,7 +286,7 @@ class RTFInput(InputFormatPlugin):
if name is not None: if name is not None:
pict.set('num', name) pict.set('num', name)
self.log('Converting XML to HTML...') self.log.info('Converting XML to HTML...')
inline_class = InlineClass(self.log) inline_class = InlineClass(self.log)
with open(pkg_resources.resource_filename('ebook_converter', with open(pkg_resources.resource_filename('ebook_converter',
'data/rtf.xsl')) as fobj: 'data/rtf.xsl')) as fobj:

View File

@@ -5,11 +5,6 @@ from ebook_converter.ptempfile import TemporaryDirectory
from ebook_converter.constants_old import __appname__, __version__ from ebook_converter.constants_old import __appname__, __version__
__license__ = 'GPL 3'
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
__docformat__ = 'restructuredtext en'
class SNBOutput(OutputFormatPlugin): class SNBOutput(OutputFormatPlugin):
name = 'SNB Output' name = 'SNB Output'
@@ -57,7 +52,8 @@ class SNBOutput(OutputFormatPlugin):
rasterizer = SVGRasterizer() rasterizer = SVGRasterizer()
rasterizer(oeb_book, opts) rasterizer(oeb_book, opts)
except Unavailable: except Unavailable:
log.warn('SVG rasterizer unavailable, SVG will not be converted') log.warning('SVG rasterizer unavailable, SVG will not be '
'converted')
# Create temp dir # Create temp dir
with TemporaryDirectory('_snb_output') as tdir: with TemporaryDirectory('_snb_output') as tdir:
@@ -120,8 +116,8 @@ class SNBOutput(OutputFormatPlugin):
tocBody = etree.SubElement(tocInfoTree, "body") tocBody = etree.SubElement(tocInfoTree, "body")
outputFiles = {} outputFiles = {}
if oeb_book.toc.count() == 0: if oeb_book.toc.count() == 0:
log.warn('This SNB file has no Table of Contents. ' log.warning('This SNB file has no Table of Contents. Creating '
'Creating a default TOC') 'a default TOC')
first = next(iter(oeb_book.spine)) first = next(iter(oeb_book.spine))
oeb_book.toc.add('Start page', first.href) oeb_book.toc.add('Start page', first.href)
else: else:
@@ -141,7 +137,7 @@ class SNBOutput(OutputFormatPlugin):
if tocitem.href.find('#') != -1: if tocitem.href.find('#') != -1:
item = tocitem.href.split('#') item = tocitem.href.split('#')
if len(item) != 2: if len(item) != 2:
log.error('Error in TOC item: %s' % tocitem) log.error('Error in TOC item: %s', tocitem)
else: else:
if item[0] in outputFiles: if item[0] in outputFiles:
outputFiles[item[0]].append((item[1], tocitem.title)) outputFiles[item[0]].append((item[1], tocitem.title))
@@ -178,16 +174,18 @@ class SNBOutput(OutputFormatPlugin):
from ebook_converter.ebooks.oeb.base import OEB_DOCS, OEB_IMAGES from ebook_converter.ebooks.oeb.base import OEB_DOCS, OEB_IMAGES
if m.hrefs[item.href].media_type in OEB_DOCS: if m.hrefs[item.href].media_type in OEB_DOCS:
if item.href not in outputFiles: if item.href not in outputFiles:
log.debug('File %s is unused in TOC. Continue in last chapter' % item.href) log.debug('File %s is unused in TOC. Continue in last '
'chapter', item.href)
mergeLast = True mergeLast = True
else: else:
if oldTree is not None and mergeLast: if oldTree is not None and mergeLast:
log.debug('Output the modified chapter again: %s' % lastName) log.debug('Output the modified chapter again: %s',
lastName)
with open(os.path.join(snbcDir, lastName), 'wb') as f: with open(os.path.join(snbcDir, lastName), 'wb') as f:
f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
mergeLast = False mergeLast = False
log.debug('Converting %s to snbc...' % item.href) log.debug('Converting %s to snbc...', item.href)
snbwriter = SNBMLizer(log) snbwriter = SNBMLizer(log)
snbcTrees = None snbcTrees = None
if not mergeLast: if not mergeLast:
@@ -201,12 +199,12 @@ class SNBOutput(OutputFormatPlugin):
with open(os.path.join(snbcDir, lastName), 'wb') as f: with open(os.path.join(snbcDir, lastName), 'wb') as f:
f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
else: else:
log.debug('Merge %s with last TOC item...' % item.href) log.debug('Merge %s with last TOC item...', item.href)
snbwriter.merge_content(oldTree, oeb_book, item, snbwriter.merge_content(oldTree, oeb_book, item,
[('', "Start")], opts) [('', "Start")], opts)
# Output the last one if needed # Output the last one if needed
log.debug('Output the last modified chapter again: %s' % lastName) log.debug('Output the last modified chapter again: %s', lastName)
if oldTree is not None and mergeLast: if oldTree is not None and mergeLast:
with open(os.path.join(snbcDir, lastName), 'wb') as f: with open(os.path.join(snbcDir, lastName), 'wb') as f:
f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
@@ -214,7 +212,7 @@ class SNBOutput(OutputFormatPlugin):
for item in m: for item in m:
if m.hrefs[item.href].media_type in OEB_IMAGES: if m.hrefs[item.href].media_type in OEB_IMAGES:
log.debug('Converting image: %s ...' % item.href) log.debug('Converting image: %s ...', item.href)
content = m.hrefs[item.href].data content = m.hrefs[item.href].data
# Convert & Resize image # Convert & Resize image
self.HandleImage(content, os.path.join(snbiDir, ProcessFileName(item.href))) self.HandleImage(content, os.path.join(snbiDir, ProcessFileName(item.href)))

View File

@@ -167,13 +167,14 @@ class TXTInput(InputFormatPlugin):
if file_ext in {'md', 'textile', 'markdown'}: if file_ext in {'md', 'textile', 'markdown'}:
options.formatting_type = {'md': 'markdown'}.get(file_ext, file_ext) options.formatting_type = {'md': 'markdown'}.get(file_ext, file_ext)
log.info('File extension indicates particular formatting. ' log.info('File extension indicates particular formatting. '
'Forcing formatting type to: %s'%options.formatting_type) 'Forcing formatting type to: %s',
options.formatting_type)
options.paragraph_type = 'off' options.paragraph_type = 'off'
# Get the encoding of the document. # Get the encoding of the document.
if options.input_encoding: if options.input_encoding:
ienc = options.input_encoding ienc = options.input_encoding
log.debug('Using user specified input encoding of %s' % ienc) log.debug('Using user specified input encoding of %s', ienc)
else: else:
det_encoding = detect(txt[:4096]) det_encoding = detect(txt[:4096])
det_encoding, confidence = det_encoding['encoding'], det_encoding['confidence'] det_encoding, confidence = det_encoding['encoding'], det_encoding['confidence']
@@ -184,10 +185,12 @@ class TXTInput(InputFormatPlugin):
# gb2312 instead of gbk. gbk is a superset of gb2312, anyway. # gb2312 instead of gbk. gbk is a superset of gb2312, anyway.
det_encoding = 'gbk' det_encoding = 'gbk'
ienc = det_encoding ienc = det_encoding
log.debug('Detected input encoding as %s with a confidence of %s%%' % (ienc, confidence * 100)) log.debug('Detected input encoding as %s with a confidence of '
'%s%%', ienc, confidence * 100)
if not ienc: if not ienc:
ienc = 'utf-8' ienc = 'utf-8'
log.debug('No input encoding specified and could not auto detect using %s' % ienc) log.debug('No input encoding specified and could not auto detect '
'using %s', ienc)
# Remove BOM from start of txt as its presence can confuse markdown # Remove BOM from start of txt as its presence can confuse markdown
import codecs import codecs
for bom in (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE, codecs.BOM_UTF8, codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE): for bom in (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE, codecs.BOM_UTF8, codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE):
@@ -206,15 +209,18 @@ class TXTInput(InputFormatPlugin):
if options.paragraph_type == 'auto': if options.paragraph_type == 'auto':
options.paragraph_type = detect_paragraph_type(txt) options.paragraph_type = detect_paragraph_type(txt)
if options.paragraph_type == 'unknown': if options.paragraph_type == 'unknown':
log.debug('Could not reliably determine paragraph type using block') log.debug('Could not reliably determine paragraph type using '
'block')
options.paragraph_type = 'block' options.paragraph_type = 'block'
else: else:
log.debug('Auto detected paragraph type as %s' % options.paragraph_type) log.debug('Auto detected paragraph type as %s',
options.paragraph_type)
# Detect formatting # Detect formatting
if options.formatting_type == 'auto': if options.formatting_type == 'auto':
options.formatting_type = detect_formatting_type(txt) options.formatting_type = detect_formatting_type(txt)
log.debug('Auto detected formatting as %s' % options.formatting_type) log.debug('Auto detected formatting as %s',
options.formatting_type)
if options.formatting_type == 'heuristic': if options.formatting_type == 'heuristic':
setattr(options, 'enable_heuristics', True) setattr(options, 'enable_heuristics', True)