Allow customization of maximum rip attempts value

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>
This commit is contained in:
ABCbum
2020-01-24 01:10:31 +07:00
committed by JoeLametta
parent 553a6de88f
commit 9db3aa9247

View File

@@ -36,7 +36,7 @@ logger = logging.getLogger(__name__)
SILENT = 0 SILENT = 0
MAX_TRIES = 5 DEFAULT_MAX_RETRIES = 5
DEFAULT_TRACK_TEMPLATE = '%r/%A - %d/%t. %a - %n' DEFAULT_TRACK_TEMPLATE = '%r/%A - %d/%t. %a - %n'
DEFAULT_DISC_TEMPLATE = '%r/%A - %d/%A - %d' DEFAULT_DISC_TEMPLATE = '%r/%A - %d/%A - %d'
@@ -299,6 +299,13 @@ Log files will log the path to tracks relative to this directory.
"complete option values respectively", "complete option values respectively",
choices=['file', 'embed', 'complete'], choices=['file', 'embed', 'complete'],
default=None) 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): def handle_arguments(self):
self.options.output_directory = os.path.expanduser( self.options.output_directory = os.path.expanduser(
@@ -329,6 +336,15 @@ Log files will log the path to tracks relative to this directory.
logger.critical(msg) logger.critical(msg)
raise ValueError(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): def doCommand(self):
self.program.setWorkingDirectory(self.options.working_directory) self.program.setWorkingDirectory(self.options.working_directory)
self.program.outdir = self.options.output_directory self.program.outdir = self.options.output_directory
@@ -415,7 +431,7 @@ Log files will log the path to tracks relative to this directory.
trackResult.copyduration = 0.0 trackResult.copyduration = 0.0
extra = "" extra = ""
tries = 1 tries = 1
while tries <= MAX_TRIES: while tries <= self.options.max_retries:
if tries > 1: if tries > 1:
extra = " (try %d)" % tries extra = " (try %d)" % tries
logger.info('ripping track %d of %d%s: %s', logger.info('ripping track %d of %d%s: %s',
@@ -441,13 +457,13 @@ Log files will log the path to tracks relative to this directory.
logger.debug('got exception %r on try %d', e, tries) logger.debug('got exception %r on try %d', e, tries)
tries += 1 tries += 1
if tries > MAX_TRIES: if tries > self.options.max_retries:
tries -= 1 tries -= 1
logger.critical('giving up on track %d after %d times', logger.critical('giving up on track %d after %d times',
number, tries) number, tries)
raise RuntimeError( raise RuntimeError("track can't be ripped. "
"track can't be ripped. " "Rip attempts number is equal to %d",
"Rip attempts number is equal to 'MAX_TRIES'") self.options.max_retries)
if trackResult.testcrc == trackResult.copycrc: if trackResult.testcrc == trackResult.copycrc:
logger.info('CRCs match for track %d', number) logger.info('CRCs match for track %d', number)
else: else: