Skip to content

Commit

Permalink
Added first portion of logging adaptation.
Browse files Browse the repository at this point in the history
Things may be broken at this point - there are still several modules to
be adapted.
  • Loading branch information
gryf committed Jun 22, 2021
1 parent 6f898ab commit 546cc26
Show file tree
Hide file tree
Showing 36 changed files with 326 additions and 316 deletions.
24 changes: 11 additions & 13 deletions ebook_converter/ebooks/conversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys

from ebook_converter.utils.config import OptionParser
from ebook_converter.utils.logging import Log
from ebook_converter.utils import logging
from ebook_converter.customize.conversion import OptionRecommendation


Expand Down Expand Up @@ -66,7 +66,7 @@ def check_command_line_options(parser, args, log):
if (not input_file.endswith('.recipe') and
not os.access(input_file, os.R_OK) and
not ('-h' in args or '--help' in args)):
log.error('Cannot read from', input_file)
log.error('Cannot read from %s', input_file)
raise SystemExit(1)
if input_file.endswith('.recipe') and not os.access(input_file, os.R_OK):
input_file = args[1]
Expand Down Expand Up @@ -267,28 +267,26 @@ def __init__(self, log):
def __call__(self, frac, msg=''):
if msg:
percent = int(frac*100)
self.log('%d%% %s' % (percent, msg))
self.log.info('%d%% %s' % (percent, msg))


def create_option_parser(args, log):
if '--version' in args:
from ebook_converter.constants_old import __appname__
from ebook_converter.constants_old import __author__
from ebook_converter.constants_old import __version__
log(os.path.basename(args[0]), '('+__appname__, __version__+')')
log('Created by:', __author__)
log.info("%s (%s, %s)", os.path.basename(args[0]), __appname__,
__version__)
log.info('Created by: %s', __author__)
raise SystemExit(0)
if '--list-recipes' in args:
from ebook_converter.web.feeds.recipes.collection import \
get_builtin_recipe_titles
log('Available recipes:')
log.info('Available recipes:')
titles = sorted(get_builtin_recipe_titles())
for title in titles:
try:
log('\t'+title)
except Exception:
log('\t'+repr(title))
log('%d recipes available' % len(titles))
log.info('\t%s', title)
log.info('%d recipes available', len(titles))
raise SystemExit(0)

parser = option_parser()
Expand Down Expand Up @@ -352,7 +350,7 @@ def read_sr_patterns(path, log=None):


def main(args=sys.argv):
log = Log()
log = logging.default_log
mimetypes.init([pkg_resources.resource_filename('ebook_converter',
'data/mime.types')])
parser, plumber = create_option_parser(args, log)
Expand Down Expand Up @@ -386,7 +384,7 @@ def main(args=sys.argv):

plumber.run()

log('Output saved to', ' ', plumber.output)
log.info('Output saved to %s', plumber.output)

return 0

Expand Down
8 changes: 4 additions & 4 deletions ebook_converter/ebooks/conversion/plugins/epub_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def rationalize_cover3(self, opf, log):
if len(spine) > 1:
for item in spine:
if item.get('idref') == titlepage_id:
log('Found HTML cover', titlepage_href)
log.info('Found HTML cover %s', titlepage_href)
if self.for_viewer:
item.attrib.pop('linear', None)
else:
Expand Down Expand Up @@ -192,7 +192,7 @@ def rationalize_cover2(self, opf, log):
elem = [x for x in manifest if x.get('id', '') == idref]
if not elem or elem[0].get('href', None) != guide_cover:
return
log('Found HTML cover', guide_cover)
log.info('Found HTML cover %s', guide_cover)

# Remove from spine as covers must be treated
# specially
Expand Down Expand Up @@ -272,8 +272,8 @@ def convert(self, stream, options, file_ext, log, accelerators):
zf = ZipFile(stream)
zf.extractall(os.getcwd())
except Exception:
log.exception('EPUB appears to be invalid ZIP file, trying a'
' more forgiving ZIP parser')
log.exception('EPUB appears to be invalid ZIP file, trying a '
'more forgiving ZIP parser')
from ebook_converter.utils.localunzip import extractall
stream.seek(0)
extractall(stream)
Expand Down
21 changes: 11 additions & 10 deletions ebook_converter/ebooks/conversion/plugins/epub_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def convert(self, oeb, output_path, input_plugin, opts, log):
self.workaround_sony_quirks()

if self.oeb.toc.count() == 0:
self.log.warn('This EPUB file has no Table of Contents. '
'Creating a default TOC')
self.log.warning('This EPUB file has no Table of Contents. '
'Creating a default TOC')
first = next(iter(self.oeb.spine))
self.oeb.toc.add('Start', first.href)

Expand All @@ -229,7 +229,7 @@ def convert(self, oeb, output_path, input_plugin, opts, log):
encrypted_fonts = getattr(input_plugin, 'encrypted_fonts', [])

if _uuid is None:
self.log.warn('No UUID identifier found')
self.log.warning('No UUID identifier found')
_uuid = str(uuid.uuid4())
oeb.metadata.add('identifier', _uuid, scheme='uuid', id=_uuid)

Expand Down Expand Up @@ -281,7 +281,7 @@ def convert(self, oeb, output_path, input_plugin, opts, log):
os.mkdir(opts.extract_to)
with ZipFile(output_path) as zf:
zf.extractall(path=opts.extract_to)
self.log.info('EPUB extracted to', opts.extract_to)
self.log.info('EPUB extracted to %s', opts.extract_to)

def upgrade_to_epub3(self, tdir, opf):
self.log.info('Upgrading to EPUB 3...')
Expand Down Expand Up @@ -323,7 +323,7 @@ def encrypt_fonts(self, uris, tdir, _uuid): # {{{
if not os.path.exists(path):
uris.pop(uri)
continue
self.log.debug('Encrypting font:', uri)
self.log.debug('Encrypting font: %s', uri)
with open(path, 'r+b') as f:
data = f.read(1024)
if len(data) >= 1024:
Expand All @@ -332,7 +332,7 @@ def encrypt_fonts(self, uris, tdir, _uuid): # {{{
f.write(bytes(bytearray(data[i] ^ key[i%16]
for i in range(1024))))
else:
self.log.warn('Font', path, 'is invalid, ignoring')
self.log.warning('Font %s is invalid, ignoring', path)
if not isinstance(uri, str):
uri = uri.decode('utf-8')
fonts.append('''
Expand Down Expand Up @@ -385,8 +385,9 @@ def workaround_ade_quirks(self): # {{{
_base, _, frag = href.partition('#')
frag = urllib.parse.unquote(frag)
if frag and frag_pat.match(frag) is None:
self.log.warn(
'Removing fragment identifier %r from TOC as Adobe Digital Editions cannot handle it'%frag)
self.log.warning('Removing fragment identifier %r from '
'TOC as Adobe Digital Editions cannot '
'handle it', frag)
node.href = _base

for x in self.oeb.spine:
Expand Down Expand Up @@ -530,8 +531,8 @@ def simplify_toc_entry(toc):
for x in self.oeb.spine:
if x.href == href:
if frag_is_at_top(x.data, frag):
self.log.debug('Removing anchor from TOC href:',
href+'#'+frag)
self.log.debug('Removing anchor from TOC '
'href: %s#%s', href, frag)
toc.href = href
break
for x in toc:
Expand Down
4 changes: 2 additions & 2 deletions ebook_converter/ebooks/conversion/plugins/lrf_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def convert(self, stream, options, file_ext, log,
from ebook_converter.ebooks.lrf.input import MediaType, Styles, \
TextBlock, Canvas, ImageBlock, RuledLine
self.log = log
self.log('Generating XML')
self.log.info('Generating XML')
from ebook_converter.ebooks.lrf.lrfparser import LRFDocument
d = LRFDocument(stream)
d.parse()
Expand Down Expand Up @@ -50,7 +50,7 @@ def convert(self, stream, options, file_ext, log,
if imgstr:
plot_map[ro] = imgstr[0].get('file')

self.log('Converting XML to HTML...')
self.log.info('Converting XML to HTML...')

with open(pkg_resources.
resource_filename('ebook_converter',
Expand Down
44 changes: 24 additions & 20 deletions ebook_converter/ebooks/conversion/plumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,8 @@ def opts_to_mi(self, mi):
try:
val = float(val)
except ValueError:
self.log.warn('Values of series index and rating must'
' be numbers. Ignoring', val)
self.log.warning('Values of series index and rating '
'must be numbers. Ignoring %s', val)
continue
elif x in ('timestamp', 'pubdate'):
try:
Expand All @@ -882,8 +882,8 @@ def read_user_metadata(self):
self.opts_to_mi(mi)
if mi.cover:
if mi.cover.startswith('http:') or mi.cover.startswith('https:'):
self.log.warn("TODO: Cover image is on remote server, "
"implement downloading using requests")
self.log.warning("TODO: Cover image is on remote server, "
"implement downloading using requests")
ext = mi.cover.rpartition('.')[-1].lower().strip()
if ext not in ('png', 'jpg', 'jpeg', 'gif'):
ext = 'jpg'
Expand All @@ -909,8 +909,8 @@ def set_profile(profiles, which):
if x.short_name == sval:
setattr(self.opts, attr, x)
return
self.log.warn(
'Profile (%s) %r is no longer available, using default'%(which, sval))
self.log.warning('Profile (%s) %r is no longer available, using '
'default', which, sval)
for x in profiles():
if x.short_name == 'default':
setattr(self.opts, attr, x)
Expand All @@ -925,14 +925,16 @@ def set_profile(profiles, which):
if self.opts.verbose:
self.log.filter_level = self.log.DEBUG
if self.changed_options:
self.log('Conversion options changed from defaults:')
self.log.info('Conversion options changed from defaults:')
for rec in self.changed_options:
if rec.option.name not in ('username', 'password'):
self.log(' ', '%s:' % rec.option.name, repr(rec.recommended_value))
self.log.info(' %s', rec.option.name,
repr(rec.recommended_value))
if self.opts.verbose > 1:
self.log.debug('Resolved conversion options')
try:
self.log.debug('ebook_converter version:', constants.VERSION)
self.log.debug('ebook_converter version: %s',
constants.VERSION)
odict = dict(self.opts.__dict__)
for x in ('username', 'password'):
odict.pop(x, None)
Expand Down Expand Up @@ -968,7 +970,7 @@ def dump_input(self, ret, output_dir):
self.input_plugin.save_download(zf)
zf.close()

self.log.info('Input debug saved to:', out_dir)
self.log.info('Input debug saved to: %s', out_dir)

def run(self):
'''
Expand Down Expand Up @@ -1022,7 +1024,8 @@ def run(self):
from ebook_converter.ebooks.azw4.reader import unwrap
unwrap(stream, self.output)
self.ui_reporter(1.)
self.log(self.output_fmt.upper(), 'output written to', self.output)
self.log.info('%s output written to %s', self.output_fmt.upper(),
self.output)
self.flush()
return

Expand Down Expand Up @@ -1056,7 +1059,7 @@ def run(self):
if self.opts.debug_pipeline is not None:
out_dir = os.path.join(self.opts.debug_pipeline, 'parsed')
self.dump_oeb(self.oeb, out_dir)
self.log('Parsed HTML written to:', out_dir)
self.log.info('Parsed HTML written to: %s', out_dir)
self.input_plugin.specialize(self.oeb, self.opts, self.log,
self.output_fmt)

Expand Down Expand Up @@ -1105,13 +1108,13 @@ def run(self):
try:
fkey = list(map(float, fkey.split(',')))
except Exception:
self.log.error('Invalid font size key: %r ignoring'%fkey)
self.log.error('Invalid font size key: %s ignoring', fkey)
fkey = self.opts.dest.fkey

if self.opts.debug_pipeline is not None:
out_dir = os.path.join(self.opts.debug_pipeline, 'structure')
self.dump_oeb(self.oeb, out_dir)
self.log('Structured HTML written to:', out_dir)
self.log.info('Structured HTML written to: %s', out_dir)

if self.opts.extra_css and os.path.exists(self.opts.extra_css):
with open(self.opts.extra_css, 'rb') as f:
Expand Down Expand Up @@ -1187,9 +1190,9 @@ def run(self):
if self.opts.debug_pipeline is not None:
out_dir = os.path.join(self.opts.debug_pipeline, 'processed')
self.dump_oeb(self.oeb, out_dir)
self.log('Processed HTML written to:', out_dir)
self.log.info('Processed HTML written to: %s', out_dir)

self.log.info('Creating %s...'%self.output_plugin.name)
self.log.info('Creating %s...', self.output_plugin.name)
our = CompositeProgressReporter(0.67, 1., self.ui_reporter)
self.output_plugin.report_progress = our
our(0., 'Running %s plugin' % self.output_plugin.name)
Expand All @@ -1200,7 +1203,8 @@ def run(self):
self.ui_reporter(1.)
run_plugins_on_postprocess(self.output, self.output_fmt)

self.log(self.output_fmt.upper(), 'output written to', self.output)
self.log.info('%s output written to %s', self.output_fmt.upper(),
self.output)
self.flush()


Expand Down Expand Up @@ -1230,7 +1234,7 @@ def create_oebbook(log, path_or_stream, opts, reader=None,
if specialize is not None:
oeb = specialize(oeb) or oeb
# Read OEB Book into OEBBook
log('Parsing all content...')
log.info('Parsing all content...')
oeb.removed_items_to_ignore = removed_items
if reader is None:
from ebook_converter.ebooks.oeb.reader import OEBReader
Expand All @@ -1241,11 +1245,11 @@ def create_oebbook(log, path_or_stream, opts, reader=None,


def create_dummy_plumber(input_format, output_format):
from ebook_converter.utils.logging import Log
from ebook_converter.utils import logging
input_format = input_format.lower()
output_format = output_format.lower()
output_path = 'dummy.'+output_format
log = Log()
log = logging.default_log
log.outputs = []
input_file = 'dummy.'+input_format
if input_format in ARCHIVE_FMTS:
Expand Down
Loading

0 comments on commit 546cc26

Please sign in to comment.