Skip to content

Commit

Permalink
Issue whipper-team#99 - Fix support for whipper.conf (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
calumchisholm committed Feb 27, 2018
1 parent ff37d6f commit 723c63a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 36 deletions.
56 changes: 55 additions & 1 deletion whipper/command/basecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import os
import sys

from whipper.common import drive
from whipper.common import (
drive, config
)

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,6 +51,8 @@ class BaseCommand():
device_option = False
no_add_help = False # for rip.main.Whipper
formatter_class = argparse.RawDescriptionHelpFormatter
config = config.Config()
config_section = None

def __init__(self, argv, prog_name, opts):
self.opts = opts # for Rip.add_arguments()
Expand Down Expand Up @@ -128,3 +132,53 @@ def epilog(self):
for com in sorted(self.subcommands.keys()):
s += " %s %s\n" % (com.ljust(8), self.subcommands[com].summary)
return s

# Wrapper around ArgumentParser.add_argument to add config file support.
# We prioritise these settings in the following order:
# command-line arg. > config file setting > default value (in code)
def add_argument_and_config(self, *args, **kwargs):
section = None
if ("section" in kwargs):
# Section name argument was passed.
section = kwargs["section"]
kwargs.pop("section")
else:
# Use the current sub-command's default config section name.
if (self.config_section is None):
section = "main"
else:
section = self.config_section

# Don't pass the default value to the argument parser.
# Use it only if the config value isn't found.
default = None
if ("default" in kwargs):
default = kwargs["default"]
kwargs.pop("default")

# We use the same key to identify both the argument destination
# and the config file key.
key = None
if ("dest" in kwargs):
key = kwargs["dest"]
else:
raise SyntaxError("parser requires a 'dest' argument")

# Defaults to string if not specified.
typesuffix = ''
if ("type" in kwargs):
if kwargs["type"] in (int, float):
typesuffix = kwargs["type"].__name__
else if kwargs["type"] = bool:
typesuffix = "boolean"

# Add the argument.
# We have 2 levels of default value. When an option is not set on the
# command line, we default to using the value from the config file. If
# that doesn't exist, we use the default as passed by the caller.
self.parser.add_argument(default=self.config.getter_or_default(
typesuffix,
section,
key,
default),
*args, **kwargs)
75 changes: 40 additions & 35 deletions whipper/command/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def doCommand(self):


class Info(_CD):
config_section = "whipper.cd.info"
summary = "retrieve information about the currently inserted CD"
description = ("Display MusicBrainz, CDDB/FreeDB, and AccurateRip"
"information for the currently inserted CD.")
Expand All @@ -214,6 +215,7 @@ def handle_arguments(self):


class Rip(_CD):
config_section = "whipper.cd.rip"
summary = "rip CD"
# see whipper.common.program.Program.getPath for expansion
description = """
Expand All @@ -233,29 +235,32 @@ class Rip(_CD):
# Requires opts.device

def add_arguments(self):
self.config = config.Config()
# section_name="whipper.cd.rip"

loggers = result.getLoggers().keys()
default_offset = None
info = drive.getDeviceInfo(self.opts.device)
if info:
try:
default_offset = config.Config().getReadOffset(*info)
default_offset = self.config.getReadOffset(*info)
sys.stdout.write("Using configured read offset %d\n" %
default_offset)
except KeyError:
pass

_CD.add_arguments(self.parser)

self.parser.add_argument('-L', '--logger',
action="store", dest="logger",
default='whipper',
help="logger to use (choose from '"
"', '".join(loggers) + "')")
# FIXME: get from config
self.add_argument_and_config('-L', '--logger',
action="store", dest="logger",
default='whipper',
help="logger to use (choose from '"
"', '".join(loggers) + "')")
self.parser.add_argument('-o', '--offset',
action="store", dest="offset",
default=default_offset,
help="sample read offset")
# TODO: Should this setting have a config setting under [drive:]?
self.parser.add_argument('-x', '--force-overread',
action="store_true", dest="overread",
default=False,
Expand All @@ -264,34 +269,34 @@ def add_arguments(self):
"if the patched cdparanoia package is "
"installed and the drive "
"supports this feature. ")
self.parser.add_argument('-O', '--output-directory',
action="store", dest="output_directory",
default=os.path.relpath(os.getcwd()),
help="output directory; will be included "
"in file paths in log")
self.parser.add_argument('-W', '--working-directory',
action="store", dest="working_directory",
help="working directory; whipper will "
"change to this directory "
"and files will be created relative to "
"it when not absolute")
self.parser.add_argument('--track-template',
action="store", dest="track_template",
default=DEFAULT_TRACK_TEMPLATE,
help="template for track file naming")
self.parser.add_argument('--disc-template',
action="store", dest="disc_template",
default=DEFAULT_DISC_TEMPLATE,
help="template for disc file naming")
self.parser.add_argument('-U', '--unknown',
action="store_true", dest="unknown",
help="whether to continue ripping if "
"the CD is unknown", default=False)
self.parser.add_argument('--cdr',
action="store_true", dest="cdr",
help="whether to continue ripping if "
"the disc is a CD-R",
default=False)
self.add_argument_and_config('-O', '--output-directory',
action="store", dest="output_directory",
default=os.path.relpath(os.getcwd()),
help="output directory; will be included "
"in file paths in log")
self.add_argument_and_config('-W', '--working-directory',
action="store", dest="working_directory",
help="working directory; whipper will "
"change to this directory "
"and files will be created relative to "
"it when not absolute")
self.add_argument_and_config('--track-template',
action="store", dest="track_template",
default=DEFAULT_TRACK_TEMPLATE,
help="template for track file naming")
self.add_argument_and_config('--disc-template',
action="store", dest="disc_template",
default=DEFAULT_DISC_TEMPLATE,
help="template for disc file naming")
self.add_argument_and_config('-U', '--unknown',
action="store_true", dest="unknown",
help="whether to continue ripping if "
"the CD is unknown", default=False)
self.add_argument_and_config('--cdr',
action="store_true", dest="cdr",
help="whether to continue ripping if "
"the disc is a CD-R",
default=False)

def handle_arguments(self):
self.options.output_directory = os.path.expanduser(
Expand Down
4 changes: 4 additions & 0 deletions whipper/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def _getter(self, suffix, section, option):
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
return None

def getter_or_default(self, suffix, section, option, default):
value = self._getter(suffix, section, option)
return default if (value is None) else value

def get(self, section, option):
return self._getter('', section, option)

Expand Down

0 comments on commit 723c63a

Please sign in to comment.