annotate corebio/utils/deoptparse.py @ 15:981eb8c3a756 default tip

Uploaded
author davidmurphy
date Sat, 31 Mar 2012 16:07:07 -0400
parents c55bdc2fb9fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1 # Copyright (c) 2004 Gavin E. Crooks <gec@compbio.berkeley.edu>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
2 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
3 # This software is distributed under the MIT Open Source License.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
4 # <http://www.opensource.org/licenses/mit-license.html>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
5 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
6 # Permission is hereby granted, free of charge, to any person obtaining a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
7 # copy of this software and associated documentation files (the "Software"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
8 # to deal in the Software without restriction, including without limitation
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
10 # and/or sell copies of the Software, and to permit persons to whom the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
11 # Software is furnished to do so, subject to the following conditions:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
12 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
13 # The above copyright notice and this permission notice shall be included
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
14 # in all copies or substantial portions of the Software.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
15 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
22 # THE SOFTWARE.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
23 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
24
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
25 """Custom extensions to OptionParse for parsing command line options."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
26 # FIXME: Docstring
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
27
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
28 # TODO: Add profiling option
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
29
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
30 # DeOptionParser :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
31 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
32 # http://docs.python.org/lib/module-optparse.html
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
33 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
34 # Random_options :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
35 # Set random generator and seed. Use options.random as
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
36 # source of random numbers
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
37 # Copyright :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
38 # print copyright information
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
39
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
40 # Documentation :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
41 # print extended document information
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
42 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
43 # Additional file_in and file_out types
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
44
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
45 import sys
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
46 from copy import copy
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
47 from optparse import Option
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
48 from optparse import OptionParser
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
49 from optparse import IndentedHelpFormatter
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
50 from optparse import OptionValueError
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
51 import random
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
52
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
53
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
54
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
55 def _copyright_callback(option, opt, value, parser):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
56 if option or opt or value or parser: pass # Shut up lint checker
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
57 print parser.copyright
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
58 sys.exit()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
59
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
60 def _doc_callback(option, opt, value, parser):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
61 if option or opt or value or parser: pass # Shut up lint checker
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
62 print parser.long_description
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
63 sys.exit()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
64
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
65
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
66 class DeHelpFormatter(IndentedHelpFormatter) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
67 def __init__ (self,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
68 indent_increment=2,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
69 max_help_position=32,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
70 width=78,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
71 short_first=1):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
72 IndentedHelpFormatter.__init__(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
73 self, indent_increment, max_help_position,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
74 width, short_first)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
75
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
76 def format_option_strings (self, option):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
77 """Return a comma-separated list of option strings & metavariables."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
78 if option.takes_value():
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
79 metavar = option.metavar or option.dest.upper()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
80 short_opts = option._short_opts
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
81 long_opts = [lopt + " " + metavar for lopt in option._long_opts]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
82 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
83 short_opts = option._short_opts
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
84 long_opts = option._long_opts
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
85
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
86 if not short_opts : short_opts = [" ",]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
87
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
88 if self.short_first:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
89 opts = short_opts + long_opts
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
90 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
91 opts = long_opts + short_opts
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
92
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
93 return " ".join(opts)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
94
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
95
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
96
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
97 def _check_file_in(option, opt, value):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
98 if option or opt or value : pass # Shut up lint checker
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
99 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
100 return file(value, "r")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
101 except IOError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
102 raise OptionValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
103 "option %s: cannot open file: %s" % (opt, value) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
104
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
105 def _check_file_out(option, opt, value):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
106 if option or opt or value : pass # Shut up lint checker
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
107 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
108 return file(value, "w+")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
109 except IOError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
110 raise OptionValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
111 "option %s: cannot open file: %s" % (opt, value) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
112
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
113 def _check_boolean(option, opt, value) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
114 if option or opt or value : pass # Shut up lint checker
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
115 v = value.lower()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
116 choices = {'no': False, 'false':False, '0': False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
117 'yes': True, 'true': True, '1':True }
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
118 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
119 return choices[v]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
120 except KeyError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
121 raise OptionValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
122 "option %s: invalid choice: '%s' " \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
123 "(choose from 'yes' or 'no', 'true' or 'false')" % (opt, value))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
124
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
125 def _check_dict(option, opt, value) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
126 if option or opt or value : pass # Shut up lint checker
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
127 v = value.lower()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
128 choices = option.choices
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
129 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
130 return choices[v]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
131 except KeyError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
132 raise OptionValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
133 "option %s: invalid choice: '%s' " \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
134 "(choose from '%s')" % (opt, value, "', '".join(choices)))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
135
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
136
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
137
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
138 class DeOption(Option):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
139 TYPES = Option.TYPES + ("file_in","file_out", "boolean", "dict")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
140 TYPE_CHECKER = copy(Option.TYPE_CHECKER)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
141 TYPE_CHECKER["file_in"] = _check_file_in
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
142 TYPE_CHECKER["file_out"] = _check_file_out
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
143 TYPE_CHECKER["boolean"] = _check_boolean
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
144 TYPE_CHECKER["dict"] = _check_dict
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
145 choices = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
146
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
147 def _new_check_choice(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
148 if self.type == "dict":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
149 if self.choices is None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
150 raise OptionValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
151 "must supply a dictionary of choices for type 'dict'")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
152 elif not isinstance(self.choices, dict):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
153 raise OptionValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
154 "choices must be a dictinary ('%s' supplied)"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
155 % str(type(self.choices)).split("'")[1])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
156 return
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
157 self._check_choice()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
158
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
159 # Have to override _check_choices so that we can parse
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
160 # a dict through to check_dict
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
161 CHECK_METHODS = Option.CHECK_METHODS
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
162 CHECK_METHODS[2] = _new_check_choice
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
163
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
164
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
165
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
166
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
167
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
168 class DeOptionParser(OptionParser) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
169 def __init__(self,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
170 usage=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
171 option_list=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
172 option_class=DeOption,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
173 version=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
174 conflict_handler="error",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
175 description=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
176 long_description = None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
177 formatter=DeHelpFormatter(),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
178 add_help_option=True,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
179 prog=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
180 copyright=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
181 add_verbose_options=True,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
182 add_random_options=False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
183 ):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
184
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
185 OptionParser.__init__(self,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
186 usage,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
187 option_list,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
188 option_class,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
189 version,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
190 conflict_handler,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
191 description,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
192 formatter,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
193 add_help_option,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
194 prog )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
195
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
196 if long_description :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
197 self.long_description = long_description
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
198 self.add_option("--doc",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
199 action="callback",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
200 callback=_doc_callback,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
201 help="Detailed documentation")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
202
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
203 if copyright :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
204 self.copyright = copyright
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
205 self.add_option("--copyright",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
206 action="callback",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
207 callback=_copyright_callback,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
208 help="")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
209
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
210 if add_verbose_options :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
211 self.add_option("-q", "--quite",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
212 action="store_false",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
213 dest="verbose",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
214 default=False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
215 help="Run quietly (default)")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
216
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
217 self.add_option("-v", "--verbose",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
218 action="store_true",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
219 dest="verbose",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
220 default=False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
221 help="Verbose output (Not quite)")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
222
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
223 self.random_options = False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
224 if add_random_options :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
225 self.random_options = True
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
226 self.add_option("--seed",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
227 action="store",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
228 type = "int",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
229 dest="random_seed",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
230 help="Initial seed for pseudo-random number generator. (default: System time)",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
231 metavar="INTEGER" )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
232
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
233 self.add_option("--generator",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
234 action="store",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
235 dest="random_generator",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
236 default="MersenneTwister",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
237 help="Select MersenneTwister (default) or WichmannHill pseudo-random number generator",
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
238 metavar="TYPE" )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
239
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
240 def parse_args(self,args, values=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
241 (options, args) = OptionParser.parse_args(self, args, values)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
242
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
243 if self.random_options :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
244 if options.random_generator is None or options.random_generator =="MersenneTwister" :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
245 r = random.Random()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
246 elif options.random_generator == "WichmannHill" :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
247 r = random.WichmannHill()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
248 else :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
249 self.error("Acceptible generators are MersenneTwister (default) or WichmannHill")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
250 if options.random_seed :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
251 r.seed(options.random_seed)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
252
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
253 options.__dict__["random"] = r
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
254
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
255
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
256 return (options, args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
257
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
258