#!/usr/bin/env python # The Ripoff command-line interface, implemented with Albert Hofkamp's # argtools.py. import sys, os import ripoff from ripoff import cdrom from argtools import OptionParser, \ VoidOption, NumberOption, StringOption, HelpOption, VersionOption # Create the option class. class MyParser (OptionParser): def __init__ (self, mix=1): OptionParser.__init__(self, mix) device = cdrom.get_default_device() self.help = self.AddOption(HelpOption()) self.version = self.AddOption(VersionOption()) # XXX how to do -v as a count option? (is that even desirable? # or is this really the better UI) self.verbose = self.AddOption(NumberOption("v verbose", optionalarg=1, defaultval=1)) # XXX is this the best way to have an option that sets verbose # to 0? how do I make it so "-q -v3" just means "-v3" ? self.quiet = self.AddOption(VoidOption("q quiet")) self.device = self.AddOption(StringOption("d device", defaultval=device)) self.output_base = self.AddOption(StringOption("b output-base")) self.tracks = self.AddOption(StringOption("t tracks")) self.use_pipes = self.AddOption(VoidOption("p use-pipes " "synchronous")) self.use_files = self.AddOption(VoidOption("f use-files " "asynchronous")) self.keep_tmp = self.AddOption(VoidOption("k keep-tmp")) self.rip_only = self.AddOption(VoidOption("R rip-only")) self.playback = self.AddOption(VoidOption("p playback")) self.eject = self.AddOption(VoidOption("e eject")) self.artist = self.AddOption(StringOption("A artist")) self.album = self.AddOption(StringOption("L album")) self.disc = self.AddOption(NumberOption("d disc")) self.year = self.AddOption(StringOption("y year")) self.offset = self.AddOption(NumberOption("t offset")) # Dummy class to put option values in. No point setting default values, # since they would just be overridden when I copy the option values into # the Options object after parsing the command-line. class Options: pass # Usage/help text. prog = os.path.basename(sys.argv[0]) usage = "usage: %s [options]" % prog help = usage + """ options: -h, --help show this help message and exit --version show program's version number and exit -v increase verbosity level --verbose=V set verbosity level to V -q, --quiet run silently (verbosity = 0) -dDEVICE, --device=DEVICE device file to open (default: /dev/cdrom on this platform) -bDIR, --output-base=DIR base output dir (default: /music/ogg) -tTRACKS, --tracks=TRACKS tracks to rip (default: all) (example: 1,2-5,8) -p, --use-pipes, --synchronous use named pipes for intermediate output (uses less temporary disk space, works great if encoding is faster than ripping) [default] -f, --use-files, --asynchronous use real files for intermediate output (uses more temporary disk space, but should be more reliable if ripping is faster than encoding, and will free up your CD-ROM drive sooner) -k, --keep-tmp put temporary (.wav) files in output dir, and don't delete them when done with them (implies -f) -R, --rip-only just do digital audio extraction -- no encoding (implies -k, -f) -P, --playback play encoded audio files as encoding proceeds -e, --eject eject CD when finished -AARTIST, --artist=ARTIST use ARTIST for tagging and file naming (overrides CDDB/CDindex) -LALBUM, --album=ALBUM use ALBUM for tagging and file naming -DDISC, --disc=DISC set disc number to DISC (for multi-disc sets) -yYEAR, --year=YEAR use YEAR for tagging --offset=OFFSET add OFFSET to track numbers for tagging and file naming """ # Set up the option parser and parse the command-line. parser = MyParser() if parser.ProcessCommandLine(): # true means error (?!) print >>sys.stderr, usage sys.exit(1) # Check for --help or --version options. if parser.help.Used(): print help sys.exit() if parser.version.Used(): print "ripoff %s" % ripoff.__version__ sys.exit() # Now we have to transfer the values from the command-line to # a more usable form (and collapse the options that affect # the same value, eg. -q and -v). options = Options() # XXX this means -q *anywhere* on the command-line overrides -v, # which is Just Plain Wrong! How am I supposed to do this with # argtools? options.verbose = parser.verbose.Value() if parser.quiet.Used(): options.verbose = 0 options.device = parser.device.Value() options.output_base = parser.output_base.Value() options.tracks = parser.tracks.Value() options.use_pipes = parser.use_pipes.Used() if parser.use_files.Used(): # XXX again, this is Just Plain Wrong -- it means "-f -p" is # interpreted the same as "-p -f", even though these are # opposing flag options. options.use_pipes = 0 options.keep_tmp = parser.keep_tmp.Used() options.rip_only = parser.rip_only.Used() options.playback = parser.playback.Used() options.eject = parser.eject.Used() # XXX all of these string options default to the empty string rather # than None. What if I want to differentiate between (eg.) --album="" # and the total absence of -L/--album? options.artist = parser.artist.Value() options.album = parser.album.Value() options.disc = parser.disc.Value() options.year = parser.year.Value() options.track_offset = parser.offset.Value() # Print the option values. from pprint import pprint print "options:" pprint(vars(options))