Ticket #5059: 0003-use-argparse-instead-of-optparse.patch

File 0003-use-argparse-instead-of-optparse.patch, 3.4 KB (added by gandaro_, 9 years ago)
  • mediagoblin/media_types/video/transcoders.py

    From 9fb7ef927e23f2f186678418fc93c1fee57e9176 Mon Sep 17 00:00:00 2001
    From: Jakob Kramer <jakob.kramer@gmx.de>
    Date: Thu, 5 Mar 2015 22:50:40 +0100
    Subject: [PATCH 3/3] use argparse instead of optparse
    
    ---
     mediagoblin/media_types/video/transcoders.py | 72 +++++++++++++---------------
     1 file changed, 32 insertions(+), 40 deletions(-)
    
    diff --git a/mediagoblin/media_types/video/transcoders.py b/mediagoblin/media_types/video/transcoders.py
    index 92952d0..784a070 100644
    a b class VideoTranscoder(object):  
    384384
    385385if __name__ == '__main__':
    386386    os.nice(19)
    387     from optparse import OptionParser
    388 
    389     parser = OptionParser(
    390         usage='%prog [-v] -a [ video | thumbnail | discover ] SRC [ DEST ]')
    391 
    392     parser.add_option('-a', '--action',
    393                       dest='action',
    394                       help='One of "video", "discover" or "thumbnail"')
    395 
    396     parser.add_option('-v',
    397                       dest='verbose',
    398                       action='store_true',
    399                       help='Output debug information')
    400 
    401     parser.add_option('-q',
    402                       dest='quiet',
    403                       action='store_true',
    404                       help='Dear program, please be quiet unless *error*')
    405 
    406     parser.add_option('-w', '--width',
    407                       type=int,
    408                       default=180)
    409 
    410     (options, args) = parser.parse_args()
    411 
    412     if options.verbose:
     387    from argparse import ArgumentParser
     388
     389    parser = ArgumentParser()
     390    parser.add_argument('--action', '-a',
     391                        required=True,
     392                        choices=['video', 'discover', 'thumbnail'])
     393    parser.add_argument('--verbose', '-v',
     394                        action='store_true',
     395                        help='Output debug information')
     396    parser.add_argument('--quiet', '-q',
     397                        action='store_true',
     398                        help='Dear program, please be quiet unless *error*')
     399    parser.add_argument('--width', '-w',
     400                        type=int,
     401                        default=180)
     402    parser.add_argument('source')
     403    parser.add_argument('dest', nargs='?', default=None)
     404
     405    args = parser.parse_args()
     406
     407    if args.verbose:
    413408        _log.setLevel(logging.DEBUG)
     409    elif args.quiet:
     410        _log.setLevel(logging.ERROR)
    414411    else:
    415412        _log.setLevel(logging.INFO)
    416413
    417     if options.quiet:
    418         _log.setLevel(logging.ERROR)
    419 
    420414    _log.debug(args)
    421415
    422     if not len(args) == 2 and not options.action == 'discover':
    423         parser.print_help()
    424         sys.exit()
     416    if args.dest is None and args.action != 'discover':
     417        parser.error('too few arguments')
    425418
    426419    transcoder = VideoTranscoder()
    427420
    428     if options.action == 'thumbnail':
    429         args.append(options.width)
    430         capture_thumb(*args)
    431     elif options.action == 'video':
     421    if args.action == 'thumbnail':
     422        capture_thumb(args.source, args.dest, args.width)
     423    elif args.action == 'video':
    432424        def cb(data):
    433             print('I\'m a callback!')
    434         transcoder.transcode(*args, progress_callback=cb)
    435     elif options.action == 'discover':
    436         print transcoder.discover(*args)
     425            print("I'm a callback!")
     426        transcoder.transcode(args.source, args.dest, progress_callback=cb)
     427    elif args.action == 'discover':
     428        print transcoder.discover(args.source)