comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7301d534bc6c
1 #!/usr/bin/env python
2
3 import os
4 import sys
5
6 from manager import ProfileManager
7 from optparse import OptionGroup
8 from optparse import OptionParser
9 from command import commands, commandargs2str, command2parser
10
11 # could go in commands
12 def print_help(parser):
13 parser.print_help()
14 # short descriptions for commands
15 command_descriptions = [dict(name=i,
16 description = commands[i]['doc'].strip().split('\n',1)[0])
17 for i in sorted(commands.keys())]
18 max_len = max([len(i['name']) for i in command_descriptions])
19 description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description'])
20 for description in command_descriptions]))
21
22 print
23 print description
24
25 def main(args=sys.argv[1:]):
26
27 # global option parsing
28 usage = '%prog <options> command <command-options>'
29 parser = OptionParser(usage, description='run `%prog help` to display commands')
30 parser.add_option('-c', '--config', dest='config',
31 help="specify a profile.ini [default: $HOME/.mozilla/firefox/profiles.ini]")
32 parser.disable_interspersed_args()
33 options, args = parser.parse_args(args)
34
35 # help/sanity check -- should probably be separated
36 if not len(args):
37 print_help(parser)
38 sys.exit(0)
39 if args[0] == 'help':
40 if len(args) == 2:
41 if args[1] in commands:
42 name = args[1]
43 commandparser = command2parser(name)
44 commandparser.print_help()
45 else:
46 parser.error("No command '%s'" % args[1])
47 else:
48 print_help(parser)
49 sys.exit(0)
50 command = args[0]
51 if command not in commands:
52 parser.error("No command '%s'" % command)
53
54 # XXX to move it its own method -- this is the only program-specific code
55 if options.config is None:
56 # XXX unix-specific
57 options.config = os.path.join(os.environ['HOME'], '.mozilla/firefox/profiles.ini')
58 if not os.path.exists(options.config):
59 parser.error('%s does not exist' % options.config)
60 manager = ProfileManager(options.config)
61
62 # command specific args
63 name = args[0]
64 command = commands[name]
65 commandparser = command2parser(name)
66 command_options, command_args = commandparser.parse_args(args[1:])
67 if len(command_args) < len(command['args']):
68 commandparser.error("Not enough arguments given")
69 if len(command_args) != len(command['args']) and not command['varargs']:
70 commandparser.error("Too many arguments given")
71
72 # invoke the command
73 getattr(manager, name)(*command_args, **command_options.__dict__)
74
75 if __name__ == '__main__':
76 main()