removed obsolete issues (many of them fixed with AE)
[docutils.git] / sandbox / paultremblay / rst_to_docbook / rst_to_docbook / options_trem.py
blob7176b165db2c6778d3f0b04a585ee46410738c68
1 # WARNING TO PAUL: DON'T EDIT THIS FILE use update.py instead.
2 import sys
3 class ParseOptions:
5 """
7 Requires:
9 system_string --The string from the command line
11 options_dict -- a dictionary with the key equal to the opition, and
12 a list describing that option. (See below)
15 Returns:
17 A tupple. The first item in the tupple is a dictionary containing
18 the arguments for each options. The second is a list of the
19 arguments.
21 If invalid options are passed to the module, 0,0 is returned.
23 Examples:
25 Your script has the option '--indents', and '--output=file'.
27 You want to give short option names as well:
29 --i and -o=file
31 Use this:
33 options_dict = {'output': [1, 'o'],
34 'indents': [0, 'i']
37 options_obj = ParseOptions(
38 system_string = sys.argv,
39 options_dict = options_dict
42 options, arguments = options_obj.parse_options()
43 print options
44 print arguments
46 The result will be:
49 {indents:None, output:'/home/paul/file'}, ['/home/paul/input']
52 """
53 def __init__(self, system_string, options_dict):
54 self.__system_string = system_string[1:]
55 long_list = self.__make_long_list_func(options_dict)
56 # # print long_list
57 short_list = self.__make_short_list_func(options_dict)
58 # # print short_list
59 self.__legal_options = long_list + short_list
60 # # print self.__legal_options
61 self.__short_long_dict = self.__make_short_long_dict_func(options_dict)
62 # # print self.__short_long_dict
63 self.__opt_with_args = self.__make_options_with_arg_list(options_dict)
64 # # print self.__opt_with_args
65 self.__options_okay = 1
69 def __make_long_list_func(self, options_dict):
70 """
71 Required:
73 options_dict -- the dictionary mapping options to a list
75 Returns:
77 a list of legal options
82 """
83 legal_list = []
84 keys = options_dict.keys()
85 for key in keys:
86 key = '--' + key
87 legal_list.append(key)
88 return legal_list
90 def __make_short_list_func(self, options_dict):
91 """
92 Required:
94 options_dict --the dictionary mapping options to a list
96 Returns:
98 a list of legal short options
101 legal_list = []
102 keys = options_dict.keys()
103 for key in keys:
104 values = options_dict[key]
105 try:
106 legal_list.append('-' + values[1])
107 except IndexError:
108 pass
110 return legal_list
114 def __make_short_long_dict_func(self, options_dict):
116 Required:
118 options_dict --the dictionary mapping options to a list
120 Returns:
122 a dictionary with keys of short options and values of long options
125 short_long_dict = {}
126 keys = options_dict.keys()
127 for key in keys:
128 values = options_dict[key]
129 try:
130 short = '-' + values[1]
131 long = '--' + key
132 short_long_dict[short] = long
133 except IndexError:
134 pass
136 return short_long_dict
138 def __make_options_with_arg_list(self, options_dict):
140 Required:
142 options_dict --the dictionary mapping options to a list
144 Returns:
146 a list of options that take arguments.
149 opt_with_arg = []
150 keys = options_dict.keys()
151 for key in keys:
152 values = options_dict[key]
153 try:
154 if values[0]:
155 opt_with_arg.append('--' + key)
156 except IndexError:
157 pass
159 return opt_with_arg
162 def __sub_short_with_long(self):
164 Required:
166 nothing
168 Returns:
170 a new system string
172 Logic:
174 iterate through the system string and replace short options with long options
178 new_string = []
179 sub_list = self.__short_long_dict.keys()
180 for item in self.__system_string:
181 if item in sub_list:
182 item = self.__short_long_dict[item]
183 new_string.append(item)
184 return new_string
188 def __pair_arg_with_option(self):
192 Required:
194 nothing
196 Returns
198 nothing (changes value of self.__system_string)
200 Logic:
202 iterate through the system string, and match arguments with options:
204 old_list = ['--foo', 'bar']
206 new_list = ['--foo=bar'
209 opt_len = len(self.__system_string)
210 new_system_string = []
211 counter = 0
212 slurp_value = 0
213 for arg in self.__system_string:
215 # previous value was an option with an argument, so this arg is
216 # actually an argument that has already been added
217 counter += 1
218 if slurp_value:
219 slurp_value = 0
220 continue
222 # not an option--an argument
223 if arg[0] != '-':
224 new_system_string.append(arg)
226 # option and argument already paired
227 elif '=' in arg:
228 new_system_string .append(arg)
229 else:
230 # this option takes an argument
231 if arg in self.__opt_with_args:
232 # option is the last in the list
233 if counter + 1 > opt_len:
234 sys.stderr.write('option "%s" must take an argument\n' % arg)
235 new_system_string.append(arg)
236 self.__options_okay = 0
237 else:
238 # the next item in list is also an option
239 if self.__system_string[counter][0] == '-':
240 sys.stderr.write('option "%s" must take an argument\n' % arg)
241 new_system_string.append(arg)
242 self.__options_okay = 0
243 # the next item in the list is the argument
244 else:
245 new_system_string.append(arg + '=' + self.__system_string[counter])
246 slurp_value = 1
247 # this option does not take an argument
248 else:
249 new_system_string.append(arg)
250 return new_system_string
253 def __get_just_options(self):
257 Requires:
259 nothing
261 Returns:
263 list of options
265 Logic:
267 Iterate through the self.__system string, looking for the last
268 option. The options are everything in the sysem string before the
269 last option.
271 Check to see that the options contain no arguments.
275 highest = 0
276 counter = 0
277 found_options = 0
278 for item in self.__system_string:
279 if item[0] == '-':
280 highest = counter
281 found_options = 1
282 counter += 1
284 if found_options:
285 just_options = self.__system_string[:highest + 1]
286 arguments = self.__system_string[highest + 1:]
287 else:
288 just_options = []
289 arguments = self.__system_string
291 if found_options:
292 for item in just_options:
293 if item[0] != '-':
294 sys.stderr.write('%s is an argument in an option list\n' % item)
295 self.__options_okay = 0
296 return just_options, arguments
299 def __is_legal_option_func(self):
302 Requires:
304 nothing
306 Returns:
308 nothing
310 Logic:
312 Check each value in the newly creatd options list to see if it
313 matches what the user describes as a legal option.
317 illegal_options = []
318 for arg in self.__system_string:
319 if '=' in arg:
320 temp_list = arg.split('=')
321 arg = temp_list[0]
322 if arg not in self.__legal_options and arg[0] == '-':
323 illegal_options.append(arg)
325 if illegal_options:
326 self.__options_okay = 0
327 sys.stderr.write('The following options are not permitted:\n')
328 for not_legal in illegal_options:
329 sys.stderr.write('%s\n' % not_legal)
332 def __make_options_dict(self, options):
333 options_dict = {}
334 for item in options:
335 if '=' in item:
336 option, arg = item.split('=')
337 else:
338 option = item
339 arg = None
341 if option[0] == '-':
342 option = option[1:]
343 if option[0] == '-':
344 option = option[1:]
346 options_dict[option] = arg
348 return options_dict
350 def parse_options(self):
351 self.__system_string = self.__sub_short_with_long()
352 # # print 'subbed list is %s' % self.__system_string
353 self.__system_string = self.__pair_arg_with_option()
354 # # print 'list with pairing is %s' % self.__system_string
355 options, arguments = self.__get_just_options()
356 # # print 'options are %s ' % options
357 # # print 'arguments are %s ' % arguments
358 self.__is_legal_option_func()
359 if self.__options_okay:
360 options_dict = self.__make_options_dict(options)
361 # # print options_dict
362 return options_dict, arguments
364 else:
365 return 0,0
370 if __name__ == '__main__':
371 this_dict = {
372 'indents': [0, 'i'],
373 'output': [1, 'o'],
374 'test3': [1, 't'],
376 test_obj = ParseOptions(system_string = sys.argv,
377 options_dict = this_dict
380 options, the_args = test_obj.parse_options()
381 print options, the_args
384 this_options = ['--foo', '-o']
385 this_opt_with_args = ['--foo']