Skip to content

Commit

Permalink
Allow customization of maximum rip attempts value
Browse files Browse the repository at this point in the history
Add new `--max-retries` argument to allow users to specify maximum number
of attempts to try before giving up ripping a track. This value defaults to `5` while `0` means infinity.
Possible errors (negative number, string, etc) are also handled.

Co-authored-by: JoeLametta <JoeLametta@users.noreply.github.com>
Signed-off-by: JoeLametta <JoeLametta@users.noreply.github.com>
Signed-off-by: ABCbum <kimlong221002@gmail.com>
  • Loading branch information
ABCbum and JoeLametta committed Jan 29, 2020
1 parent 553a6de commit 9db3aa9
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions whipper/command/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


SILENT = 0
MAX_TRIES = 5
DEFAULT_MAX_RETRIES = 5

DEFAULT_TRACK_TEMPLATE = '%r/%A - %d/%t. %a - %n'
DEFAULT_DISC_TEMPLATE = '%r/%A - %d/%A - %d'
Expand Down Expand Up @@ -299,6 +299,13 @@ def add_arguments(self):
"complete option values respectively",
choices=['file', 'embed', 'complete'],
default=None)
self.parser.add_argument('r', '--max-retries',
action="store", dest="max_retries",
help="number of rip attempts before giving "
"up if can't rip a track. This defaults to "
"{}; 0 means "
"infinity.".format(DEFAULT_MAX_RETRIES),
default=DEFAULT_MAX_RETRIES)

def handle_arguments(self):
self.options.output_directory = os.path.expanduser(
Expand Down Expand Up @@ -329,6 +336,15 @@ def handle_arguments(self):
logger.critical(msg)
raise ValueError(msg)

try:
self.options.max_retries = int(self.options.max_retries)
except ValueError:
raise ValueError("max retries' value must be of integer type")
if self.options.max_retries == 0:
self.options.max_retries = float("inf")
elif self.options.max_retries < 0:
raise ValueError("number of max retries must be positive")

def doCommand(self):
self.program.setWorkingDirectory(self.options.working_directory)
self.program.outdir = self.options.output_directory
Expand Down Expand Up @@ -415,7 +431,7 @@ def _ripIfNotRipped(number):
trackResult.copyduration = 0.0
extra = ""
tries = 1
while tries <= MAX_TRIES:
while tries <= self.options.max_retries:
if tries > 1:
extra = " (try %d)" % tries
logger.info('ripping track %d of %d%s: %s',
Expand All @@ -441,13 +457,13 @@ def _ripIfNotRipped(number):
logger.debug('got exception %r on try %d', e, tries)
tries += 1

if tries > MAX_TRIES:
if tries > self.options.max_retries:
tries -= 1
logger.critical('giving up on track %d after %d times',
number, tries)
raise RuntimeError(
"track can't be ripped. "
"Rip attempts number is equal to 'MAX_TRIES'")
raise RuntimeError("track can't be ripped. "
"Rip attempts number is equal to %d",
self.options.max_retries)
if trackResult.testcrc == trackResult.copycrc:
logger.info('CRCs match for track %d', number)
else:
Expand Down

0 comments on commit 9db3aa9

Please sign in to comment.