Skip to content

Commit

Permalink
introduce logcommand.Lager, Whipper(); use argparse for whipper image…
Browse files Browse the repository at this point in the history
… commands, stub logging
  • Loading branch information
RecursiveForest committed Nov 11, 2016
1 parent 8fdb452 commit b9cf34b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 37 deletions.
18 changes: 17 additions & 1 deletion morituri/common/logcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,24 @@
"""

from morituri.extern.command import command
from morituri.common import log
from morituri.common import log, config
import logging

class Lager():
"""
Provides self.debug() logging facility for existing commands.
Provides self.epilog() formatting command for argparse.
"""
config = config.Config()
def debug(self, format, *args):
kwargs = {}
pass

def epilog(self):
s = "commands:\n"
for com in sorted(self.subcommands.keys()):
s += " %s %s\n" % (com.ljust(8), self.subcommands[com].summary)
return s

class LogCommand(command.Command, log.Loggable):

Expand Down
2 changes: 1 addition & 1 deletion morituri/rip/accurip.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ def do(self, args):


class AccuRip(logcommand.LogCommand):
description = "Handle AccurateRip information."
summary = "handle AccurateRip information"

subCommandClasses = [Show, ]
2 changes: 1 addition & 1 deletion morituri/rip/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def writeFile(handle, path, length):

class CD(logcommand.LogCommand):

summary = "handle CD's"
summary = "handle CDs"

subCommandClasses = [Info, Rip, ]

Expand Down
97 changes: 72 additions & 25 deletions morituri/rip/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
# You should have received a copy of the GNU General Public License
# along with morituri. If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import sys

from morituri.common import logcommand, accurip, program
import logging

from morituri.common import logcommand, accurip, program, config
from morituri.image import image
from morituri.result import result

Expand Down Expand Up @@ -99,27 +103,43 @@ def do(self, args):
outm3u.close()


class Retag(logcommand.LogCommand):

class Retag(logcommand.Lager):
summary = "retag image files"

def addOptions(self):
self.parser.add_option('-R', '--release-id',
description = """
Retags the image from the given .cue files with tags obtained from MusicBrainz.
"""
stdout = sys.stdout

def __init__(self, argv, prog=None):
parser = argparse.ArgumentParser(
prog=prog,
description=self.description
)
parser.add_argument('cuefile', nargs='+', action='store',
help="cue file to load rip image from")
parser.add_argument(
'-R', '--release-id',
action="store", dest="release_id",
help="MusicBrainz release id to match to (if there are multiple)")
self.parser.add_option('-p', '--prompt',
help="MusicBrainz release id to match to (if there are multiple)"
)
parser.add_argument(
'-p', '--prompt',
action="store_true", dest="prompt",
help="Prompt if there are multiple matching releases")
self.parser.add_option('-c', '--country',
help="Prompt if there are multiple matching releases"
)
parser.add_argument(
'-c', '--country',
action="store", dest="country",
help="Filter releases by country")

help="Filter releases by country"
)
self.options = parser.parse_args(argv)
return self.do(self.options.cuefile)

def do(self, args):
# here to avoid import gst eating our options
from morituri.common import encode

prog = program.Program(self.getRootCommand().config, stdout=self.stdout)
prog = program.Program(self.config, stdout=self.stdout)
runner = task.SyncRunner()

for arg in args:
Expand Down Expand Up @@ -164,17 +184,24 @@ def do(self, args):
print


class Verify(logcommand.LogCommand):

usage = '[CUEFILE]...'
class Verify(logcommand.Lager):
summary = "verify image"

description = '''
description = """
Verifies the image from the given .cue files against the AccurateRip database.
'''
"""

def __init__(self, argv, prog=None):
parser = argparse.ArgumentParser(
prog=prog,
description=self.description
)
parser.add_argument('cuefile', nargs='+', action='store',
help="cue file to load rip image from")
options = parser.parse_args(argv)
return self.do(options.cuefile)

def do(self, args):
prog = program.Program(self.getRootCommand().config)
prog = program.Program(self.config)
runner = task.SyncRunner()
cache = accurip.AccuCache()

Expand All @@ -199,14 +226,34 @@ def do(self, args):
print "\n".join(prog.getAccurateRipResults()) + "\n"


class Image(logcommand.LogCommand):

class Image(logcommand.Lager):
summary = "handle images"

description = """
Handle disc images. Disc images are described by a .cue file.
Disc images can be encoded to another format (for example, to make a
compressed encoding), retagged and verified.
"""

subCommandClasses = [Encode, Retag, Verify, ]
subcommands = {
'verify': Verify,
'retag': Retag
}

def __init__(self, argv, prog=None):
parser = argparse.ArgumentParser(
prog=prog,
description=self.description,
epilog=self.epilog(),
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('remainder', nargs=argparse.REMAINDER,
help=argparse.SUPPRESS)
opt = parser.parse_args(argv)
if not opt.remainder:
parser.print_help()
sys.exit(0)
if not opt.remainder[0] in self.subcommands:
sys.stderr.write("incorrect subcommand: %s" % opt.remainder[0])
sys.exit(1)
return self.subcommands[opt.remainder[0]](
opt.remainder[1:], prog=prog + " " + opt.remainder[0]
)
57 changes: 48 additions & 9 deletions morituri/rip/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

import argparse
import os
import sys
import pkg_resources
Expand All @@ -22,9 +23,8 @@ def main():
pkg_resources.Environment([directory.data_path('plugins')])
)
map(pkg_resources.working_set.add, distributions)
c = Rip()
try:
ret = c.parse(sys.argv[1:])
ret = Whipper(sys.argv[1:], prog=os.path.basename(sys.argv[0]))
except SystemError, e:
sys.stderr.write('rip: error: %s\n' % e.args)
return 255
Expand All @@ -46,15 +46,54 @@ def main():
return 255

raise
except command.CommandError, e:
sys.stderr.write('rip: error: %s\n' % e.output)
return e.status
return ret if ret else 0

if ret is None:
return 0

return ret
class Whipper(logcommand.Lager):
usage = "%prog %command"
description = """whipper is a CD ripping utility focusing on accuracy over speed.
whipper gives you a tree of subcommands to work with.
You can get help on subcommands by using the -h option to the subcommand.
"""
subcommands = {
'accurip': accurip.AccuRip,
'cd': cd.CD,
'debug': debug.Debug,
'drive': drive.Drive,
'offset': offset.Offset,
'image': image.Image
}

def __init__(self, argv, prog=None):
parser = argparse.ArgumentParser(
prog=prog,
add_help=False,
description=self.description,
epilog=self.epilog(),
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('-R', '--record',
action='store_true', dest='record',
help="record API requests for playback")
parser.add_argument('-v', '--version',
action="store_true", dest="version",
help="show version information")
parser.add_argument('-h', '--help',
action="store_true", dest="help",
help="show this help message and exit")
parser.add_argument('remainder', nargs=argparse.REMAINDER,
help=argparse.SUPPRESS)
opt = parser.parse_args(argv)
if opt.help or not opt.remainder:
parser.print_help()
sys.exit(0)
if opt.version:
print "whipper %s" % configure.version
sys.stderr.write("incorrect subcommand: %s" % opt.remainder[0])
sys.exit(1)
return self.subcommands[opt.remainder[0]](
opt.remainder[1:], prog=prog + " " + opt.remainder[0]
)

class Rip(logcommand.LogCommand):
usage = "%prog %command"
Expand Down

0 comments on commit b9cf34b

Please sign in to comment.