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