view profilemanager/main.py @ 0:7301d534bc6c

initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
author Jeff Hammel <k0scist@gmail.com>
date Sun, 04 Apr 2010 18:49:55 -0400
parents
children 979315ed0816
line wrap: on
line source

#!/usr/bin/env python

import os
import sys

from manager import ProfileManager
from optparse import OptionGroup
from optparse import OptionParser
from command import commands, commandargs2str, command2parser

# could go in commands
def print_help(parser):
    parser.print_help()
    # short descriptions for commands
    command_descriptions = [dict(name=i,
                                 description = commands[i]['doc'].strip().split('\n',1)[0])
                            for i in sorted(commands.keys())]
    max_len = max([len(i['name']) for i in command_descriptions])
    description = "Commands: \n%s" % ('\n'.join(['  %s%s  %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description'])
                                                 for description in command_descriptions]))

    print
    print description

def main(args=sys.argv[1:]):

    # global option parsing
    usage = '%prog <options> command <command-options>'
    parser = OptionParser(usage, description='run `%prog help` to display commands')
    parser.add_option('-c', '--config', dest='config',
                      help="specify a profile.ini [default: $HOME/.mozilla/firefox/profiles.ini]")
    parser.disable_interspersed_args()
    options, args = parser.parse_args(args)

    # help/sanity check -- should probably be separated
    if not len(args):
        print_help(parser)
        sys.exit(0)
    if args[0] == 'help':
        if len(args) == 2:
            if args[1] in commands:
                name = args[1]
                commandparser = command2parser(name)
                commandparser.print_help()
            else:
                parser.error("No command '%s'" % args[1])
        else:
            print_help(parser)
        sys.exit(0)
    command = args[0]
    if command not in commands:
        parser.error("No command '%s'" % command)

    # XXX to move it its own method -- this is the only program-specific code
    if options.config is None:
        # XXX unix-specific
        options.config = os.path.join(os.environ['HOME'], '.mozilla/firefox/profiles.ini')
    if not os.path.exists(options.config):
        parser.error('%s does not exist' % options.config)
    manager = ProfileManager(options.config)

    # command specific args
    name = args[0]
    command = commands[name]
    commandparser = command2parser(name)
    command_options, command_args = commandparser.parse_args(args[1:])
    if len(command_args) < len(command['args']):
        commandparser.error("Not enough arguments given")
    if len(command_args) != len(command['args']) and not command['varargs']:
        commandparser.error("Too many arguments given")

    # invoke the command
    getattr(manager, name)(*command_args, **command_options.__dict__)

if __name__ == '__main__':
    main()