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

Fix some flake8 findings on config module.

This commit is contained in:
2020-09-06 18:29:33 +02:00
parent 11275651a2
commit 2c787fd5bf

View File

@@ -1,9 +1,10 @@
""" """
Manage application-wide preferences. Manage application-wide preferences.
""" """
import copy
import optparse import optparse
import os import os
from copy import deepcopy import traceback
from ebook_converter import constants from ebook_converter import constants
from ebook_converter import constants_old from ebook_converter import constants_old
@@ -28,7 +29,7 @@ class CustomHelpFormatter(optparse.IndentedHelpFormatter):
def format_heading(self, heading): def format_heading(self, heading):
from ebook_converter.utils.terminal import colored from ebook_converter.utils.terminal import colored
return "%*s%s:\n" % (self.current_indent, '', return "%*s%s:\n" % (self.current_indent, '',
colored(heading, fg='blue', bold=True)) colored(heading, fg='blue', bold=True))
def format_option(self, option): def format_option(self, option):
import textwrap import textwrap
@@ -39,11 +40,12 @@ class CustomHelpFormatter(optparse.IndentedHelpFormatter):
opt_width = self.help_position - self.current_indent - 2 opt_width = self.help_position - self.current_indent - 2
if len(opts) > opt_width: if len(opts) > opt_width:
opts = "%*s%s\n" % (self.current_indent, "", opts = "%*s%s\n" % (self.current_indent, "",
colored(opts, fg='green')) colored(opts, fg='green'))
indent_first = self.help_position indent_first = self.help_position
else: # start help on same line as opts else: # start help on same line as opts
opts = "%*s%-*s " % (self.current_indent, "", opt_width + opts = "%*s%-*s " % (self.current_indent, "", opt_width +
len(colored('', fg='green')), colored(opts, fg='green')) len(colored('', fg='green')),
colored(opts, fg='green'))
indent_first = 0 indent_first = 0
result.append(opts) result.append(opts)
if option.help: if option.help:
@@ -82,9 +84,11 @@ class OptionParser(optparse.OptionParser):
if version is None: if version is None:
version = '%%prog (%s %s)' % (constants_old.__appname__, version = '%%prog (%s %s)' % (constants_old.__appname__,
constants.VERSION) constants.VERSION)
optparse.OptionParser.__init__(self, usage=usage, version=version, epilog=epilog, optparse.OptionParser.__init__(self, usage=usage, version=version,
formatter=CustomHelpFormatter(), epilog=epilog,
conflict_handler=conflict_handler, **kwds) formatter=CustomHelpFormatter(),
conflict_handler=conflict_handler,
**kwds)
self.gui_mode = gui_mode self.gui_mode = gui_mode
def print_usage(self, file=None): def print_usage(self, file=None):
@@ -108,15 +112,15 @@ class OptionParser(optparse.OptionParser):
optparse.OptionParser.error(self, msg) optparse.OptionParser.error(self, msg)
def merge(self, parser): def merge(self, parser):
''' """
Add options from parser to self. In case of conflicts, conflicting options from Add options from parser to self. In case of conflicts, conflicting
parser are skipped. options from parser are skipped.
''' """
opts = list(parser.option_list) opts = list(parser.option_list)
groups = list(parser.option_groups) groups = list(parser.option_groups)
def merge_options(options, container): def merge_options(options, container):
for opt in deepcopy(options): for opt in copy.deepcopy(options):
if not self.has_option(opt.get_opt_string()): if not self.has_option(opt.get_opt_string()):
container.add_option(opt) container.add_option(opt)
@@ -127,11 +131,12 @@ class OptionParser(optparse.OptionParser):
merge_options(group.option_list, g) merge_options(group.option_list, g)
def subsume(self, group_name, msg=''): def subsume(self, group_name, msg=''):
''' """
Move all existing options into a subgroup named Move all existing options into a subgroup named
C{group_name} with description C{msg}. C{group_name} with description C{msg}.
''' """
opts = [opt for opt in self.options_iter() if opt.get_opt_string() not in ('--version', '--help')] opts = [opt for opt in self.options_iter()
if opt.get_opt_string() not in ('--version', '--help')]
self.option_groups = [] self.option_groups = []
subgroup = self.add_option_group(group_name, msg) subgroup = self.add_option_group(group_name, msg)
for opt in opts: for opt in opts:
@@ -153,11 +158,11 @@ class OptionParser(optparse.OptionParser):
return opt return opt
def merge_options(self, lower, upper): def merge_options(self, lower, upper):
''' """
Merge options in lower and upper option lists into upper. Merge options in lower and upper option lists into upper.
Default values in upper are overridden by Default values in upper are overridden by
non default values in lower. non default values in lower.
''' """
for dest in lower.__dict__.keys(): for dest in lower.__dict__.keys():
if dest not in upper.__dict__: if dest not in upper.__dict__:
continue continue
@@ -327,8 +332,7 @@ class XMLConfig(dict):
d = self.raw_to_object(raw) if raw.strip() else {} d = self.raw_to_object(raw) if raw.strip() else {}
except SystemError: except SystemError:
pass pass
except: except Exception:
import traceback
traceback.print_exc() traceback.print_exc()
d = {} d = {}
if clear_current: if clear_current: