Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read command parameters from config sections #240

Merged
merged 5 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,42 +157,45 @@ The simplest way to get started making accurate rips is:

## Configuration file documentation

The configuration file is stored according to the [XDG Base Directory Specification](
http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html)
when possible.

It lives in `$XDG_CONFIG_HOME/whipper/whipper.conf` (or `$HOME/.config/whipper/whipper.conf`).

The configuration file follows python's [ConfigParser](https://docs.python.org/2/library/configparser.html) syntax.

The possible sections are:

- Main section: `[main]`
- `path_filter_fat`: whether to filter path components for FAT file systems
- `path_filter_special`: whether to filter path components for special characters

- MusicBrainz section: `[musicbrainz]`
- `server`: the MusicBrainz server to connect to, in `host:[port]` format. Defaults to `musicbrainz.org`.

- Drive section: `[drive:IDENTIFIER]`, one for each configured drive. All these values are probed by whipper and should not be edited by hand.
- `defeats_cache`: whether this drive can defeat the audio cache
- `read_offset`: the read offset of the drive

- Rip command section: `[rip.COMMAND.SUBCOMMAND]`. Can be used to change the command options default values.
**Please note that this feature is currently broken (being this way since [PR #122](https://github.com/JoeLametta/whipper/pull/92) / whipper [v0.4.1](https://github.com/JoeLametta/whipper/releases/tag/v0.4.1)).**

Example section to configure `whipper cd rip` defaults:

```Python
[rip.cd.rip]
The configuration file is stored in
`$XDG_CONFIG_HOME/whipper/whipper.conf`, or
`$HOME/.config/whipper/whipper.conf` if `$XDG_CONFIG_HOME` is undefined.
Copy link
Member

@Freso Freso Mar 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably separate the two possibilities a bit stronger. Maybe a comma (,) or (en‐)hyphen () before the or?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in, '"XDG...", or "HOME" if'?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly.


See [XDG Base Directory
Specification](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html)
and [ConfigParser](https://docs.python.org/2/library/configparser.html).

The configuration file consists of newline-deliniated `[sections]`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line has a typo: newline-deliniated -> newline-delineated.

containing `key = value` pairs. The sections `[main]` and
`[musicbrainz]` are special config sections for options not accessible
from the command line interface. Sections beginning with `drive` are
written by whipper; certain values should not be edited.

Example configuration demonstrating all `[main]` and `[musicbrainz]`
options:

```INI
[main]
path_filter_fat = True ; replace FAT file system unsafe characters in filenames with _
path_filter_special = False ; replace special characters in filenames with _

[musicbrainz]
server = musicbrainz.org:80 ; use musicbrainz server at host[:port]

[drive:HL-20]
defeats_cache = True ; whether the drive is capable of defeating the audio cache
read_offset = 6 ; drive read offset in positive/negative frames (no leading +)
# do not edit the values 'vendor', 'model', and 'release'; they are used by whipper to match the drive

# command line defaults for `whipper cd rip`
[whipper.cd.rip]
unknown = True
output_directory = ~/My Music
track_template = new/%%A/%%y - %%d/%%t - %%n
track_template = new/%%A/%%y - %%d/%%t - %%n ; note: the format char '%' must be represented '%%'
disc_template = %(track_template)s
# ...
```

Note: to get a literal `%` character it must be doubled.

## Backward incompatible changes

- Rely on `cd-paranoia` (`libcdio-cdparanoia`) instead of `cdparanoia` (Xiph): changed dependency ([PR #213](https://github.com/JoeLametta/whipper/pull/213) / whipper [v0.6.0](https://github.com/JoeLametta/whipper/releases/tag/v0.6.0))
Expand Down
25 changes: 20 additions & 5 deletions whipper/command/basecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import sys

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

import logging
logger = logging.getLogger(__name__)
Expand All @@ -27,11 +27,13 @@

class BaseCommand():
"""
A base command class for whipper commands.
Register and handle whipper command arguments with ArgumentParser.

Creates an argparse.ArgumentParser.
Override add_arguments() and handle_arguments() to register
and process arguments before & after argparse.parse_args().
Register arguments by overriding `add_arguments()` and modifying
`self.parser`. Option defaults are read from the dot-separated
`prog_name` section of the config file (e.g., 'whipper cd rip'
options are read from '[whipper.cd.rip]'). Runs
`argparse.parse_args()` then calls `handle_arguments()`.

Provides self.epilog() formatting command for argparse.

Expand All @@ -57,6 +59,19 @@ def __init__(self, argv, prog_name, opts):
self.init_parser()
self.add_arguments()

config_section = prog_name.replace(' ', '.')
defaults = {}
for action in self.parser._actions:
val = None
if isinstance(action, argparse._StoreAction):
val = config.Config().get(config_section, action.dest)
elif isinstance(action, (argparse._StoreTrueAction,
argparse._StoreFalseAction)):
val = config.Config().getboolean(config_section, action.dest)
if val is not None:
defaults[action.dest] = val
self.parser.set_defaults(**defaults)

if hasattr(self, 'subcommands'):
self.parser.add_argument('remainder',
nargs=argparse.REMAINDER,
Expand Down