annotate flagReader.py @ 32:e72eb9137a87

trying fix for IOError crash in redirect
author catherine.devlin@gmail.com
date Fri, 16 May 2008 10:40:15 -0400
parents 47af95ad83c7
children
rev   line source
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
1 """Defines and parses UNIX-style flags to modify command arguments.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
2
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
3 Use of flagReader is DEPRECATED in favor of optparse from the
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
4 Python standard library. For backwards compatibility, flagReader
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
5 has been re-implemented as a wrapper around optparse.
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
6
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
7 print flagReader.FlagSet.parse.__doc__ for usage examples.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
8 """
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
9
10
47af95ad83c7 working the refactor still
catherine@localhost
parents: 9
diff changeset
10 import re, optparse, warnings
47af95ad83c7 working the refactor still
catherine@localhost
parents: 9
diff changeset
11 warnings.warn("""flagReader has been deprecated. Use optparse instead.""", DeprecationWarning)
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
12
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
13 class Flag(object):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
14 def __init__(self, name, abbrev=None, nargs=0):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
15 """Flag(name, abbrev=None, nargs=0) : Defines a flag.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
16
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
17 name: the full name of the flag (double-dash form)
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
18 abbrev: the single-letter abbreviated form of the flag; defaults to
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
19 nargs: number of arguments expected after the flag"""
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
20
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
21 self.name = name
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
22 self.abbrev = abbrev or name[0]
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
23 self.fullabbrev = '-%s' % (self.abbrev)
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
24 self.fullname = '--%s' % (name)
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
25 self.nargs = nargs
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
26
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
27 class FlagSet(object):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
28 def __init__(self, flags):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
29 if not issubclass(type(flags), list):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
30 raise TypeError, 'Argument must be a list'
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
31 self.flags = flags
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
32 self.lookup = {}
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
33 for flag in self.flags:
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
34 self.lookup[flag.abbrev] = flag
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
35 self.lookup[flag.fullabbrev] = flag
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
36 self.lookup[flag.fullname] = flag
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
37 self.abbrevPattern = re.compile('^-([%s]+)$' % (''.join(f.abbrev for f in flags)))
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
38 def parse(self, arg):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
39 """
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
40 Finds flags; returns {flag: (values, if any)} and the remaining argument.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
41
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
42 >>> f = FlagSet([Flag('foo'), Flag('bar'), Flag('gimmea', nargs=1)])
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
43 >>> f.parse('-fb')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
44 ({'foo': [], 'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
45 >>> f.parse('no flags')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
46 ({}, 'no flags')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
47 >>> f.parse('-f blah')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
48 ({'foo': []}, 'blah')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
49 >>> f.parse('--bar')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
50 ({'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
51 >>> f.parse('--bar -f')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
52 ({'foo': [], 'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
53 >>> f.parse('--notaflag')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
54 ({}, '--notaflag')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
55 >>> f.parse('')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
56 ({}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
57 >>> f.parse('--gimmea bee -f and then some other stuff')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
58 ({'gimmea': ['bee'], 'foo': []}, 'and then some other stuff')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
59 >>> f.parse('hidden -bar')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
60 ({}, 'hidden -bar')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
61 >>> f.parse('-g myarg -b')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
62 ({'gimmea': ['myarg'], 'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
63 """
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
64 parser = optparse.OptionParser()
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
65 for flag in self.flags:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
66 if flag.nargs:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
67 parser.add_option(flag.fullabbrev, flag.fullname, action="store",
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
68 type="string", dest=flag.name)
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
69 else:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
70 parser.add_option(flag.fullabbrev, flag.fullname, action="store_true",
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
71 dest=flag.name)
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
72 try:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
73 (options, args) = parser.parse_args(arg.split())
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
74 except SystemExit, e:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
75 return {}, arg
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
76
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
77 result = {}
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
78 for (k,v) in options.__dict__.items():
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
79 if v == True:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
80 result[k] = []
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
81 elif v:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
82 result[k] = [v]
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
83 return result, ' '.join(args)
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
84
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
85 def _test():
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
86 import doctest
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
87 doctest.testmod()
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
88
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
89 if __name__ == '__main__':
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
90 _test()