Show More
@@ -1,7 +1,7 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """DPyGetOpt -- Demiurge Python GetOptions Module |
|
2 | """DPyGetOpt -- Demiurge Python GetOptions Module | |
3 |
|
3 | |||
4 |
$Id: DPyGetOpt.py |
|
4 | $Id: DPyGetOpt.py 2872 2007-11-25 17:58:05Z fperez $ | |
5 |
|
5 | |||
6 | This module is modeled after perl's Getopt::Long module-- which |
|
6 | This module is modeled after perl's Getopt::Long module-- which | |
7 | is, in turn, modeled after GNU's extended getopt() function. |
|
7 | is, in turn, modeled after GNU's extended getopt() function. | |
@@ -33,7 +33,7 b' and -baz options that appear on within the parsed argument list' | |||||
33 | must have a real number argument and that the accumulated list |
|
33 | must have a real number argument and that the accumulated list | |
34 | of values will be available under the name 'foo' |
|
34 | of values will be available under the name 'foo' | |
35 |
|
35 | |||
36 |
$Id: DPyGetOpt.py |
|
36 | $Id: DPyGetOpt.py 2872 2007-11-25 17:58:05Z fperez $""" | |
37 |
|
37 | |||
38 | #***************************************************************************** |
|
38 | #***************************************************************************** | |
39 | # |
|
39 | # | |
@@ -74,9 +74,18 b' import string' | |||||
74 | import sys |
|
74 | import sys | |
75 | import types |
|
75 | import types | |
76 |
|
76 | |||
77 | arg_error = 'DPyGetOpt Argument Error' |
|
77 | class Error(Exception): | |
78 | spec_error = 'DPyGetOpt Specification Error' |
|
78 | """Base class for exceptions in the DPyGetOpt module.""" | |
79 | term_error = 'DPyGetOpt Termination Error' |
|
79 | ||
|
80 | class ArgumentError(Error): | |||
|
81 | """Exception indicating an error in the arguments passed to | |||
|
82 | DPyGetOpt.processArguments.""" | |||
|
83 | ||||
|
84 | class SpecificationError(Error): | |||
|
85 | """Exception indicating an error with an option specification.""" | |||
|
86 | ||||
|
87 | class TerminationError(Error): | |||
|
88 | """Exception indicating an error with an option processing terminator.""" | |||
80 |
|
89 | |||
81 | specificationExpr = re.compile('(?P<required>.)(?P<type>.)(?P<multi>@?)') |
|
90 | specificationExpr = re.compile('(?P<required>.)(?P<type>.)(?P<multi>@?)') | |
82 |
|
91 | |||
@@ -214,7 +223,7 b' class DPyGetOpt:' | |||||
214 | """ |
|
223 | """ | |
215 | Adds the option described by oTuple (name, (type, mode, |
|
224 | Adds the option described by oTuple (name, (type, mode, | |
216 | default), alias) to optionTuples. Adds index keyed under name |
|
225 | default), alias) to optionTuples. Adds index keyed under name | |
217 |
to optionNames. Raises |
|
226 | to optionNames. Raises SpecificationError if name already in | |
218 | optionNames |
|
227 | optionNames | |
219 | """ |
|
228 | """ | |
220 | (name, (type, mode, default, multi), realName) = oTuple |
|
229 | (name, (type, mode, default, multi), realName) = oTuple | |
@@ -222,11 +231,13 b' class DPyGetOpt:' | |||||
222 | # verify name and add to option names dictionary |
|
231 | # verify name and add to option names dictionary | |
223 | if self.optionNames.has_key(name): |
|
232 | if self.optionNames.has_key(name): | |
224 | if realName: |
|
233 | if realName: | |
225 |
raise |
|
234 | raise SpecificationError('Alias \'' + name + '\' for \'' + | |
226 | '\' already used for another option or alias.' |
|
235 | realName + | |
|
236 | '\' already used for another option or alias.') | |||
227 | else: |
|
237 | else: | |
228 |
raise |
|
238 | raise SpecificationError('Option named \'' + name + | |
229 |
'\' specified more than once. Specification: ' |
|
239 | '\' specified more than once. Specification: ' | |
|
240 | + option) | |||
230 |
|
241 | |||
231 | # validated. add to optionNames |
|
242 | # validated. add to optionNames | |
232 | self.optionNames[name] = self.tupleIndex |
|
243 | self.optionNames[name] = self.tupleIndex | |
@@ -244,11 +255,13 b' class DPyGetOpt:' | |||||
244 | # verify name and add to option names dictionary |
|
255 | # verify name and add to option names dictionary | |
245 | if self.optionNames.has_key(alias): |
|
256 | if self.optionNames.has_key(alias): | |
246 | if realName: |
|
257 | if realName: | |
247 |
raise |
|
258 | raise SpecificationError('Negated alias \'' + name + | |
248 |
'\' |
|
259 | '\' for \'' + realName + | |
|
260 | '\' already used for another option or alias.') | |||
249 | else: |
|
261 | else: | |
250 |
raise |
|
262 | raise SpecificationError('Negated option named \'' + name + | |
251 |
'\' specified more than once. Specification: ' |
|
263 | '\' specified more than once. Specification: ' | |
|
264 | + option) | |||
252 |
|
265 | |||
253 | # validated. add to optionNames |
|
266 | # validated. add to optionNames | |
254 | self.optionNames[alias] = self.tupleIndex |
|
267 | self.optionNames[alias] = self.tupleIndex | |
@@ -299,7 +312,8 b' class DPyGetOpt:' | |||||
299 | # break into names, specification |
|
312 | # break into names, specification | |
300 | match = splitExpr.match(option) |
|
313 | match = splitExpr.match(option) | |
301 | if match is None: |
|
314 | if match is None: | |
302 |
raise |
|
315 | raise SpecificationError('Invalid specification {' + option + | |
|
316 | '}') | |||
303 |
|
317 | |||
304 | names = match.group('names') |
|
318 | names = match.group('names') | |
305 | specification = match.group('spec') |
|
319 | specification = match.group('spec') | |
@@ -328,7 +342,8 b' class DPyGetOpt:' | |||||
328 | match = specificationExpr.match(specification) |
|
342 | match = specificationExpr.match(specification) | |
329 | if match is None: |
|
343 | if match is None: | |
330 | # failed to parse, die |
|
344 | # failed to parse, die | |
331 |
raise |
|
345 | raise SpecificationError('Invalid configuration for option \'' | |
|
346 | + option + '\'') | |||
332 |
|
347 | |||
333 | # determine mode |
|
348 | # determine mode | |
334 | required = match.group('required') |
|
349 | required = match.group('required') | |
@@ -337,7 +352,8 b' class DPyGetOpt:' | |||||
337 | elif required == ':': |
|
352 | elif required == ':': | |
338 | argMode = ArgOptional |
|
353 | argMode = ArgOptional | |
339 | else: |
|
354 | else: | |
340 |
raise |
|
355 | raise SpecificationError('Unknown requirement configuration \'' | |
|
356 | + required + '\'') | |||
341 |
|
357 | |||
342 | # determine type |
|
358 | # determine type | |
343 | type = match.group('type') |
|
359 | type = match.group('type') | |
@@ -351,7 +367,8 b' class DPyGetOpt:' | |||||
351 | argType = RealArgType |
|
367 | argType = RealArgType | |
352 | argDefault = 1 |
|
368 | argDefault = 1 | |
353 | else: |
|
369 | else: | |
354 |
raise |
|
370 | raise SpecificationError('Unknown type specifier \'' + | |
|
371 | type + '\'') | |||
355 |
|
372 | |||
356 | # determine quantity |
|
373 | # determine quantity | |
357 | if match.group('multi') == '@': |
|
374 | if match.group('multi') == '@': | |
@@ -425,7 +442,7 b' class DPyGetOpt:' | |||||
425 | terminator. If it is, sets self.terminator to the full name of |
|
442 | terminator. If it is, sets self.terminator to the full name of | |
426 | the terminator. |
|
443 | the terminator. | |
427 |
|
444 | |||
428 |
If more than one terminator matched, raises a |
|
445 | If more than one terminator matched, raises a TerminationError with a | |
429 | string describing the ambiguity. |
|
446 | string describing the ambiguity. | |
430 | """ |
|
447 | """ | |
431 |
|
448 | |||
@@ -445,8 +462,8 b' class DPyGetOpt:' | |||||
445 | if not len(terms): |
|
462 | if not len(terms): | |
446 | return None |
|
463 | return None | |
447 | elif len(terms) > 1: |
|
464 | elif len(terms) > 1: | |
448 |
raise |
|
465 | raise TerminationError('Ambiguous terminator \'' + optionName + | |
449 | '\' matches ' + repr(terms) |
|
466 | '\' matches ' + repr(terms)) | |
450 |
|
467 | |||
451 | self.terminator = terms[0] |
|
468 | self.terminator = terms[0] | |
452 | return self.terminator |
|
469 | return self.terminator | |
@@ -529,10 +546,11 b' class DPyGetOpt:' | |||||
529 | tuples = self._getArgTuple(optName) |
|
546 | tuples = self._getArgTuple(optName) | |
530 |
|
547 | |||
531 | if tuples == None: |
|
548 | if tuples == None: | |
532 |
raise |
|
549 | raise ArgumentError('Illegal option \'' + arg + '\'') | |
533 | elif len(tuples) > 1: |
|
550 | elif len(tuples) > 1: | |
534 |
raise |
|
551 | raise ArgumentError('Ambiguous option \'' + arg + | |
535 |
|
|
552 | '\'; matches ' + | |
|
553 | repr(map(lambda x: x[0], tuples))) | |||
536 | else: |
|
554 | else: | |
537 | config = tuples[0] |
|
555 | config = tuples[0] | |
538 |
|
556 | |||
@@ -545,8 +563,9 b' class DPyGetOpt:' | |||||
545 | if (optMode == ArgRequired): |
|
563 | if (optMode == ArgRequired): | |
546 | if (not nextArg) or self._isTerminator(nextArg): |
|
564 | if (not nextArg) or self._isTerminator(nextArg): | |
547 | # print nextArg |
|
565 | # print nextArg | |
548 |
raise |
|
566 | raise ArgumentError('Option \'' + arg + | |
549 |
'\' requires an argument of type ' + |
|
567 | '\' requires an argument of type ' + | |
|
568 | optType) | |||
550 |
|
569 | |||
551 | if (not optMode == None) and nextArg and (not self._isTerminator(nextArg)): |
|
570 | if (not optMode == None) and nextArg and (not self._isTerminator(nextArg)): | |
552 | # nextArg defined, option configured to possibly consume arg |
|
571 | # nextArg defined, option configured to possibly consume arg | |
@@ -559,15 +578,17 b' class DPyGetOpt:' | |||||
559 | except: |
|
578 | except: | |
560 | # only raise conversion error if REQUIRED to consume argument |
|
579 | # only raise conversion error if REQUIRED to consume argument | |
561 | if optMode == ArgRequired: |
|
580 | if optMode == ArgRequired: | |
562 |
raise |
|
581 | raise ArgumentError('Invalid argument to option \'' | |
563 |
'\'; should be \'' + |
|
582 | + arg + '\'; should be \'' + | |
|
583 | optType + '\'') | |||
564 | else: |
|
584 | else: | |
565 | optionValue = optDefault |
|
585 | optionValue = optDefault | |
566 |
except |
|
586 | except ArgumentError: | |
567 |
raise |
|
587 | raise | |
568 | except: |
|
588 | except: | |
569 |
raise |
|
589 | raise ArgumentError('(' + arg + | |
570 |
') Conversion function for \'' + |
|
590 | ') Conversion function for \'' + | |
|
591 | optType + '\' not found.') | |||
571 | else: |
|
592 | else: | |
572 | optionValue = optDefault |
|
593 | optionValue = optDefault | |
573 |
|
594 | |||
@@ -583,7 +604,8 b' class DPyGetOpt:' | |||||
583 | else: |
|
604 | else: | |
584 | # only one value per |
|
605 | # only one value per | |
585 | if self.isPosixCompliant and self.optionValues.has_key(realName): |
|
606 | if self.isPosixCompliant and self.optionValues.has_key(realName): | |
586 |
raise |
|
607 | raise ArgumentError('Argument \'' + arg + | |
|
608 | '\' occurs multiple times.') | |||
587 |
|
609 | |||
588 | self.optionValues[realName] = optionValue |
|
610 | self.optionValues[realName] = optionValue | |
589 |
|
611 | |||
@@ -610,25 +632,25 b' def _test():' | |||||
610 | """ |
|
632 | """ | |
611 | try: |
|
633 | try: | |
612 | DPyGetOpt(['foo', 'bar=s', 'foo']) |
|
634 | DPyGetOpt(['foo', 'bar=s', 'foo']) | |
613 | except: |
|
635 | except Error, exc: | |
614 |
print 'EXCEPTION (should be \'foo\' already used..): ' |
|
636 | print 'EXCEPTION (should be \'foo\' already used..): %s' % exc | |
615 |
|
637 | |||
616 | try: |
|
638 | try: | |
617 | DPyGetOpt(['foo|bar|apple=s@', 'baz|apple!']) |
|
639 | DPyGetOpt(['foo|bar|apple=s@', 'baz|apple!']) | |
618 | except: |
|
640 | except Error, exc: | |
619 |
print 'EXCEPTION (should be duplicate alias/name error): ' |
|
641 | print 'EXCEPTION (should be duplicate alias/name error): %s' % exc | |
620 |
|
642 | |||
621 | x = DPyGetOpt(['apple|atlas=i@', 'application|executable=f@']) |
|
643 | x = DPyGetOpt(['apple|atlas=i@', 'application|executable=f@']) | |
622 | try: |
|
644 | try: | |
623 | x.processArguments(['-app', '29.3']) |
|
645 | x.processArguments(['-app', '29.3']) | |
624 | except: |
|
646 | except Error, exc: | |
625 |
print 'EXCEPTION (should be ambiguous argument): ' |
|
647 | print 'EXCEPTION (should be ambiguous argument): %s' % exc | |
626 |
|
648 | |||
627 | x = DPyGetOpt(['foo'], ['antigravity', 'antithesis']) |
|
649 | x = DPyGetOpt(['foo'], ['antigravity', 'antithesis']) | |
628 | try: |
|
650 | try: | |
629 | x.processArguments(['-foo', 'anti']) |
|
651 | x.processArguments(['-foo', 'anti']) | |
630 | except: |
|
652 | except Error, exc: | |
631 |
print 'EXCEPTION (should be ambiguous terminator): ' |
|
653 | print 'EXCEPTION (should be ambiguous terminator): %s' % exc | |
632 |
|
654 | |||
633 | profile = ['plain-option', |
|
655 | profile = ['plain-option', | |
634 | 'boolean-option!', |
|
656 | 'boolean-option!', |
@@ -1,7 +1,7 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Magic functions for InteractiveShell. |
|
2 | """Magic functions for InteractiveShell. | |
3 |
|
3 | |||
4 |
$Id: Magic.py 28 |
|
4 | $Id: Magic.py 2872 2007-11-25 17:58:05Z fperez $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
@@ -1405,8 +1405,9 b' Currently the magic system has the following functions:\\n"""' | |||||
1405 |
|
1405 | |||
1406 | The file is executed in a namespace initially consisting only of |
|
1406 | The file is executed in a namespace initially consisting only of | |
1407 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
1407 | __name__=='__main__' and sys.argv constructed as indicated. It thus | |
1408 | sees its environment as if it were being run as a stand-alone |
|
1408 | sees its environment as if it were being run as a stand-alone program | |
1409 | program. But after execution, the IPython interactive namespace gets |
|
1409 | (except for sharing global objects such as previously imported | |
|
1410 | modules). But after execution, the IPython interactive namespace gets | |||
1410 | updated with all variables defined in the program (except for __name__ |
|
1411 | updated with all variables defined in the program (except for __name__ | |
1411 | and sys.argv). This allows for very convenient loading of code for |
|
1412 | and sys.argv). This allows for very convenient loading of code for | |
1412 | interactive work, while giving each program a 'clean sheet' to run in. |
|
1413 | interactive work, while giving each program a 'clean sheet' to run in. |
@@ -5,7 +5,7 b' General purpose utilities.' | |||||
5 | This is a grab-bag of stuff I find useful in most programs I write. Some of |
|
5 | This is a grab-bag of stuff I find useful in most programs I write. Some of | |
6 | these things are also convenient when working at the command line. |
|
6 | these things are also convenient when working at the command line. | |
7 |
|
7 | |||
8 |
$Id: genutils.py 28 |
|
8 | $Id: genutils.py 2872 2007-11-25 17:58:05Z fperez $""" | |
9 |
|
9 | |||
10 | #***************************************************************************** |
|
10 | #***************************************************************************** | |
11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
@@ -620,9 +620,9 b" def process_cmdline(argv,names=[],defaults={},usage=''):" | |||||
620 |
|
620 | |||
621 | try: |
|
621 | try: | |
622 | getopt.processArguments(argv) |
|
622 | getopt.processArguments(argv) | |
623 | except: |
|
623 | except DPyGetOpt.ArgumentError, exc: | |
624 | print usage |
|
624 | print usage | |
625 |
warn( |
|
625 | warn('"%s"' % exc,level=4) | |
626 |
|
626 | |||
627 | defaults.update(getopt.optionValues) |
|
627 | defaults.update(getopt.optionValues) | |
628 | args = getopt.freeValues |
|
628 | args = getopt.freeValues |
@@ -6,7 +6,7 b' Requires Python 2.1 or better.' | |||||
6 |
|
6 | |||
7 | This file contains the main make_IPython() starter function. |
|
7 | This file contains the main make_IPython() starter function. | |
8 |
|
8 | |||
9 |
$Id: ipmaker.py 272 |
|
9 | $Id: ipmaker.py 2872 2007-11-25 17:58:05Z fperez $""" | |
10 |
|
10 | |||
11 | #***************************************************************************** |
|
11 | #***************************************************************************** | |
12 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
12 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
@@ -301,9 +301,9 b" object? -> Details about 'object'. ?object also works, ?? prints more." | |||||
301 |
|
301 | |||
302 | try: |
|
302 | try: | |
303 | getopt.processArguments(argv) |
|
303 | getopt.processArguments(argv) | |
304 | except: |
|
304 | except DPyGetOpt.ArgumentError, exc: | |
305 | print cmd_line_usage |
|
305 | print cmd_line_usage | |
306 |
warn('\nError in Arguments: ' |
|
306 | warn('\nError in Arguments: "%s"' % exc) | |
307 | sys.exit(1) |
|
307 | sys.exit(1) | |
308 |
|
308 | |||
309 | # convert the options dict to a struct for much lighter syntax later |
|
309 | # convert the options dict to a struct for much lighter syntax later |
@@ -1,3 +1,10 b'' | |||||
|
1 | 2007-11-24 Fernando Perez <Fernando.Perez@colorado.edu> | |||
|
2 | ||||
|
3 | * IPython/DPyGetOpt.py (ArgumentError): Apply patch by Paul Mueller | |||
|
4 | <gakusei-AT-dakotacom.net>, to fix deprecated string exceptions in | |||
|
5 | options handling. Unicode fix in %whos (committed a while ago) | |||
|
6 | was also contributed by Paul. | |||
|
7 | ||||
1 | 2007-11-23 Darren Dale <darren.dale@cornell.edu> |
|
8 | 2007-11-23 Darren Dale <darren.dale@cornell.edu> | |
2 | * ipy_traits_completer.py: let traits_completer respect the user's |
|
9 | * ipy_traits_completer.py: let traits_completer respect the user's | |
3 | readline_omit__names setting. |
|
10 | readline_omit__names setting. |
General Comments 0
You need to be logged in to leave comments.
Login now