##// END OF EJS Templates
Merge branch 'ready_unbundle' of https://github.com/tomspur/ipython into tomspur-ready_unbundle
Fernando Perez -
r3434:d935e092 merge
parent child Browse files
Show More
@@ -0,0 +1,4 b''
1 try:
2 from Itpl import *
3 except ImportError:
4 from _Itpl import *
@@ -0,0 +1,12 b''
1 try:
2 import argparse
3 # Workaround an argparse bug, FIXED in argparse 1.1.0
4 if 'RawTextHelpFormatterArgumentDefaultsHelpFormatter' in argparse.__all__:
5 import itertools
6 argparse.__all__ = list(itertools.chain( [i for i in argparse.__all__
7 if i != 'RawTextHelpFormatterArgumentDefaultsHelpFormatter'],
8 ['RawTextHelpFormatter', 'ArgumentDefaultsHelpFormatter']))
9 argparse.__all__.append('SUPPRESS')
10 from argparse import *
11 except ImportError:
12 from _argparse import *
@@ -0,0 +1,4 b''
1 try:
2 from configobj import *
3 except ImportError:
4 from _configobj import *
@@ -0,0 +1,8 b''
1 try:
2 from decorator import *
3 from decorator import getinfo, new_wrapper
4 # the following funcion is deprecated so using the python own one
5 from functools import update_wrapper
6 except ImportError:
7 from _decorator import *
8 from _decorator import getinfo, update_wrapper, new_wrapper
@@ -0,0 +1,4 b''
1 try:
2 from numpy.testing.decorators import *
3 except ImportError:
4 from _decorators import *
@@ -0,0 +1,4 b''
1 try:
2 from guid import *
3 except ImportError:
4 from _guid import *
@@ -0,0 +1,4 b''
1 try:
2 from mglob import *
3 except ImportError:
4 from _mglob import *
@@ -0,0 +1,4 b''
1 try:
2 from path import *
3 except ImportError:
4 from _path import *
@@ -0,0 +1,5 b''
1 try:
2 import pexpect
3 from pexpect import *
4 except ImportError:
5 from _pexpect import *
@@ -0,0 +1,4 b''
1 try:
2 from pyparsing import *
3 except ImportError:
4 from _pyparsing import *
@@ -0,0 +1,4 b''
1 try:
2 from simplegeneric import *
3 except ImportError:
4 from _simplegeneric import *
@@ -0,0 +1,8 b''
1 try:
2 import validate
3 if '__docformat__' in validate.__all__ and validate.__version__.split('.') >= ['1', '0', '1']:
4 # __docformat__ was removed in 1.0.1 but
5 validate.__all__ = [i for i in validate.__all__ if i != '__docformat__']
6 from validate import *
7 except ImportError:
8 from _validate import *
@@ -1,557 +1,557 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Display formatters.
3 3
4 4
5 5 Authors:
6 6
7 7 * Robert Kern
8 8 * Brian Granger
9 9 """
10 10 #-----------------------------------------------------------------------------
11 11 # Copyright (c) 2010, IPython Development Team.
12 12 #
13 13 # Distributed under the terms of the Modified BSD License.
14 14 #
15 15 # The full license is in the file COPYING.txt, distributed with this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 # Stdlib imports
23 23 import abc
24 24 import sys
25 25 # We must use StringIO, as cStringIO doesn't handle unicode properly.
26 26 from StringIO import StringIO
27 27
28 28 # Our own imports
29 29 from IPython.config.configurable import Configurable
30 from IPython.external import pretty
30 from IPython.lib import pretty
31 31 from IPython.utils.traitlets import Bool, Dict, Int, Str, CStr
32 32
33 33
34 34 #-----------------------------------------------------------------------------
35 35 # The main DisplayFormatter class
36 36 #-----------------------------------------------------------------------------
37 37
38 38
39 39 class DisplayFormatter(Configurable):
40 40
41 41 # When set to true only the default plain text formatter will be used.
42 42 plain_text_only = Bool(False, config=True)
43 43
44 44 # A dict of formatter whose keys are format types (MIME types) and whose
45 45 # values are subclasses of BaseFormatter.
46 46 formatters = Dict(config=True)
47 47 def _formatters_default(self):
48 48 """Activate the default formatters."""
49 49 formatter_classes = [
50 50 PlainTextFormatter,
51 51 HTMLFormatter,
52 52 SVGFormatter,
53 53 PNGFormatter,
54 54 LatexFormatter,
55 55 JSONFormatter
56 56 ]
57 57 d = {}
58 58 for cls in formatter_classes:
59 59 f = cls(config=self.config)
60 60 d[f.format_type] = f
61 61 return d
62 62
63 63 def format(self, obj, include=None, exclude=None):
64 64 """Return a format data dict for an object.
65 65
66 66 By default all format types will be computed.
67 67
68 68 The following MIME types are currently implemented:
69 69
70 70 * text/plain
71 71 * text/html
72 72 * text/latex
73 73 * application/json
74 74 * image/png
75 75 * immage/svg+xml
76 76
77 77 Parameters
78 78 ----------
79 79 obj : object
80 80 The Python object whose format data will be computed.
81 81 include : list or tuple, optional
82 82 A list of format type strings (MIME types) to include in the
83 83 format data dict. If this is set *only* the format types included
84 84 in this list will be computed.
85 85 exclude : list or tuple, optional
86 86 A list of format type string (MIME types) to exclue in the format
87 87 data dict. If this is set all format types will be computed,
88 88 except for those included in this argument.
89 89
90 90 Returns
91 91 -------
92 92 format_dict : dict
93 93 A dictionary of key/value pairs, one or each format that was
94 94 generated for the object. The keys are the format types, which
95 95 will usually be MIME type strings and the values and JSON'able
96 96 data structure containing the raw data for the representation in
97 97 that format.
98 98 """
99 99 format_dict = {}
100 100
101 101 # If plain text only is active
102 102 if self.plain_text_only:
103 103 formatter = self.formatters['text/plain']
104 104 try:
105 105 data = formatter(obj)
106 106 except:
107 107 # FIXME: log the exception
108 108 raise
109 109 if data is not None:
110 110 format_dict['text/plain'] = data
111 111 return format_dict
112 112
113 113 for format_type, formatter in self.formatters.items():
114 114 if include is not None:
115 115 if format_type not in include:
116 116 continue
117 117 if exclude is not None:
118 118 if format_type in exclude:
119 119 continue
120 120 try:
121 121 data = formatter(obj)
122 122 except:
123 123 # FIXME: log the exception
124 124 raise
125 125 if data is not None:
126 126 format_dict[format_type] = data
127 127 return format_dict
128 128
129 129 @property
130 130 def format_types(self):
131 131 """Return the format types (MIME types) of the active formatters."""
132 132 return self.formatters.keys()
133 133
134 134
135 135 #-----------------------------------------------------------------------------
136 136 # Formatters for specific format types (text, html, svg, etc.)
137 137 #-----------------------------------------------------------------------------
138 138
139 139
140 140 class FormatterABC(object):
141 141 """ Abstract base class for Formatters.
142 142
143 143 A formatter is a callable class that is responsible for computing the
144 144 raw format data for a particular format type (MIME type). For example,
145 145 an HTML formatter would have a format type of `text/html` and would return
146 146 the HTML representation of the object when called.
147 147 """
148 148 __metaclass__ = abc.ABCMeta
149 149
150 150 # The format type of the data returned, usually a MIME type.
151 151 format_type = 'text/plain'
152 152
153 153 # Is the formatter enabled...
154 154 enabled = True
155 155
156 156 @abc.abstractmethod
157 157 def __call__(self, obj):
158 158 """Return a JSON'able representation of the object.
159 159
160 160 If the object cannot be formatted by this formatter, then return None
161 161 """
162 162 try:
163 163 return repr(obj)
164 164 except TypeError:
165 165 return None
166 166
167 167
168 168 class BaseFormatter(Configurable):
169 169 """A base formatter class that is configurable.
170 170
171 171 This formatter should usually be used as the base class of all formatters.
172 172 It is a traited :class:`Configurable` class and includes an extensible
173 173 API for users to determine how their objects are formatted. The following
174 174 logic is used to find a function to format an given object.
175 175
176 176 1. The object is introspected to see if it has a method with the name
177 177 :attr:`print_method`. If is does, that object is passed to that method
178 178 for formatting.
179 179 2. If no print method is found, three internal dictionaries are consulted
180 180 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
181 181 and :attr:`deferred_printers`.
182 182
183 183 Users should use these dictionaries to register functions that will be
184 184 used to compute the format data for their objects (if those objects don't
185 185 have the special print methods). The easiest way of using these
186 186 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
187 187 methods.
188 188
189 189 If no function/callable is found to compute the format data, ``None`` is
190 190 returned and this format type is not used.
191 191 """
192 192
193 193 format_type = Str('text/plain')
194 194
195 195 enabled = Bool(True, config=True)
196 196
197 197 print_method = Str('__repr__')
198 198
199 199 # The singleton printers.
200 200 # Maps the IDs of the builtin singleton objects to the format functions.
201 201 singleton_printers = Dict(config=True)
202 202 def _singleton_printers_default(self):
203 203 return {}
204 204
205 205 # The type-specific printers.
206 206 # Map type objects to the format functions.
207 207 type_printers = Dict(config=True)
208 208 def _type_printers_default(self):
209 209 return {}
210 210
211 211 # The deferred-import type-specific printers.
212 212 # Map (modulename, classname) pairs to the format functions.
213 213 deferred_printers = Dict(config=True)
214 214 def _deferred_printers_default(self):
215 215 return {}
216 216
217 217 def __call__(self, obj):
218 218 """Compute the format for an object."""
219 219 if self.enabled:
220 220 obj_id = id(obj)
221 221 try:
222 222 obj_class = getattr(obj, '__class__', None) or type(obj)
223 223 if hasattr(obj_class, self.print_method):
224 224 printer = getattr(obj_class, self.print_method)
225 225 return printer(obj)
226 226 try:
227 227 printer = self.singleton_printers[obj_id]
228 228 except (TypeError, KeyError):
229 229 pass
230 230 else:
231 231 return printer(obj)
232 232 for cls in pretty._get_mro(obj_class):
233 233 if cls in self.type_printers:
234 234 return self.type_printers[cls](obj)
235 235 else:
236 236 printer = self._in_deferred_types(cls)
237 237 if printer is not None:
238 238 return printer(obj)
239 239 return None
240 240 except Exception:
241 241 pass
242 242 else:
243 243 return None
244 244
245 245 def for_type(self, typ, func):
246 246 """Add a format function for a given type.
247 247
248 248 Parameters
249 249 -----------
250 250 typ : class
251 251 The class of the object that will be formatted using `func`.
252 252 func : callable
253 253 The callable that will be called to compute the format data. The
254 254 call signature of this function is simple, it must take the
255 255 object to be formatted and return the raw data for the given
256 256 format. Subclasses may use a different call signature for the
257 257 `func` argument.
258 258 """
259 259 oldfunc = self.type_printers.get(typ, None)
260 260 if func is not None:
261 261 # To support easy restoration of old printers, we need to ignore
262 262 # Nones.
263 263 self.type_printers[typ] = func
264 264 return oldfunc
265 265
266 266 def for_type_by_name(self, type_module, type_name, func):
267 267 """Add a format function for a type specified by the full dotted
268 268 module and name of the type, rather than the type of the object.
269 269
270 270 Parameters
271 271 ----------
272 272 type_module : str
273 273 The full dotted name of the module the type is defined in, like
274 274 ``numpy``.
275 275 type_name : str
276 276 The name of the type (the class name), like ``dtype``
277 277 func : callable
278 278 The callable that will be called to compute the format data. The
279 279 call signature of this function is simple, it must take the
280 280 object to be formatted and return the raw data for the given
281 281 format. Subclasses may use a different call signature for the
282 282 `func` argument.
283 283 """
284 284 key = (type_module, type_name)
285 285 oldfunc = self.deferred_printers.get(key, None)
286 286 if func is not None:
287 287 # To support easy restoration of old printers, we need to ignore
288 288 # Nones.
289 289 self.deferred_printers[key] = func
290 290 return oldfunc
291 291
292 292 def _in_deferred_types(self, cls):
293 293 """
294 294 Check if the given class is specified in the deferred type registry.
295 295
296 296 Returns the printer from the registry if it exists, and None if the
297 297 class is not in the registry. Successful matches will be moved to the
298 298 regular type registry for future use.
299 299 """
300 300 mod = getattr(cls, '__module__', None)
301 301 name = getattr(cls, '__name__', None)
302 302 key = (mod, name)
303 303 printer = None
304 304 if key in self.deferred_printers:
305 305 # Move the printer over to the regular registry.
306 306 printer = self.deferred_printers.pop(key)
307 307 self.type_printers[cls] = printer
308 308 return printer
309 309
310 310
311 311 class PlainTextFormatter(BaseFormatter):
312 312 """The default pretty-printer.
313 313
314 314 This uses :mod:`IPython.external.pretty` to compute the format data of
315 315 the object. If the object cannot be pretty printed, :func:`repr` is used.
316 316 See the documentation of :mod:`IPython.external.pretty` for details on
317 317 how to write pretty printers. Here is a simple example::
318 318
319 319 def dtype_pprinter(obj, p, cycle):
320 320 if cycle:
321 321 return p.text('dtype(...)')
322 322 if hasattr(obj, 'fields'):
323 323 if obj.fields is None:
324 324 p.text(repr(obj))
325 325 else:
326 326 p.begin_group(7, 'dtype([')
327 327 for i, field in enumerate(obj.descr):
328 328 if i > 0:
329 329 p.text(',')
330 330 p.breakable()
331 331 p.pretty(field)
332 332 p.end_group(7, '])')
333 333 """
334 334
335 335 # The format type of data returned.
336 336 format_type = Str('text/plain')
337 337
338 338 # This subclass ignores this attribute as it always need to return
339 339 # something.
340 340 enabled = Bool(True, config=False)
341 341
342 342 # Look for a __pretty__ methods to use for pretty printing.
343 343 print_method = Str('__pretty__')
344 344
345 345 # Whether to pretty-print or not.
346 346 pprint = Bool(True, config=True)
347 347
348 348 # Whether to be verbose or not.
349 349 verbose = Bool(False, config=True)
350 350
351 351 # The maximum width.
352 352 max_width = Int(79, config=True)
353 353
354 354 # The newline character.
355 355 newline = Str('\n', config=True)
356 356
357 357 # format-string for pprinting floats
358 358 float_format = Str('%r')
359 359 # setter for float precision, either int or direct format-string
360 360 float_precision = CStr('', config=True)
361 361
362 362 def _float_precision_changed(self, name, old, new):
363 363 """float_precision changed, set float_format accordingly.
364 364
365 365 float_precision can be set by int or str.
366 366 This will set float_format, after interpreting input.
367 367 If numpy has been imported, numpy print precision will also be set.
368 368
369 369 integer `n` sets format to '%.nf', otherwise, format set directly.
370 370
371 371 An empty string returns to defaults (repr for float, 8 for numpy).
372 372
373 373 This parameter can be set via the '%precision' magic.
374 374 """
375 375
376 376 if '%' in new:
377 377 # got explicit format string
378 378 fmt = new
379 379 try:
380 380 fmt%3.14159
381 381 except Exception:
382 382 raise ValueError("Precision must be int or format string, not %r"%new)
383 383 elif new:
384 384 # otherwise, should be an int
385 385 try:
386 386 i = int(new)
387 387 assert i >= 0
388 388 except ValueError:
389 389 raise ValueError("Precision must be int or format string, not %r"%new)
390 390 except AssertionError:
391 391 raise ValueError("int precision must be non-negative, not %r"%i)
392 392
393 393 fmt = '%%.%if'%i
394 394 if 'numpy' in sys.modules:
395 395 # set numpy precision if it has been imported
396 396 import numpy
397 397 numpy.set_printoptions(precision=i)
398 398 else:
399 399 # default back to repr
400 400 fmt = '%r'
401 401 if 'numpy' in sys.modules:
402 402 import numpy
403 403 # numpy default is 8
404 404 numpy.set_printoptions(precision=8)
405 405 self.float_format = fmt
406 406
407 407 # Use the default pretty printers from IPython.external.pretty.
408 408 def _singleton_printers_default(self):
409 409 return pretty._singleton_pprinters.copy()
410 410
411 411 def _type_printers_default(self):
412 412 d = pretty._type_pprinters.copy()
413 413 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
414 414 return d
415 415
416 416 def _deferred_printers_default(self):
417 417 return pretty._deferred_type_pprinters.copy()
418 418
419 419 #### FormatterABC interface ####
420 420
421 421 def __call__(self, obj):
422 422 """Compute the pretty representation of the object."""
423 423 if not self.pprint:
424 424 try:
425 425 return repr(obj)
426 426 except TypeError:
427 427 return ''
428 428 else:
429 429 # This uses use StringIO, as cStringIO doesn't handle unicode.
430 430 stream = StringIO()
431 431 printer = pretty.RepresentationPrinter(stream, self.verbose,
432 432 self.max_width, self.newline,
433 433 singleton_pprinters=self.singleton_printers,
434 434 type_pprinters=self.type_printers,
435 435 deferred_pprinters=self.deferred_printers)
436 436 printer.pretty(obj)
437 437 printer.flush()
438 438 return stream.getvalue()
439 439
440 440
441 441 class HTMLFormatter(BaseFormatter):
442 442 """An HTML formatter.
443 443
444 444 To define the callables that compute the HTML representation of your
445 445 objects, define a :meth:`__html__` method or use the :meth:`for_type`
446 446 or :meth:`for_type_by_name` methods to register functions that handle
447 447 this.
448 448 """
449 449 format_type = Str('text/html')
450 450
451 451 print_method = Str('__html__')
452 452
453 453
454 454 class SVGFormatter(BaseFormatter):
455 455 """An SVG formatter.
456 456
457 457 To define the callables that compute the SVG representation of your
458 458 objects, define a :meth:`__svg__` method or use the :meth:`for_type`
459 459 or :meth:`for_type_by_name` methods to register functions that handle
460 460 this.
461 461 """
462 462 format_type = Str('image/svg+xml')
463 463
464 464 print_method = Str('__svg__')
465 465
466 466
467 467 class PNGFormatter(BaseFormatter):
468 468 """A PNG formatter.
469 469
470 470 To define the callables that compute the PNG representation of your
471 471 objects, define a :meth:`__png__` method or use the :meth:`for_type`
472 472 or :meth:`for_type_by_name` methods to register functions that handle
473 473 this. The raw data should be the base64 encoded raw png data.
474 474 """
475 475 format_type = Str('image/png')
476 476
477 477 print_method = Str('__png__')
478 478
479 479
480 480 class LatexFormatter(BaseFormatter):
481 481 """A LaTeX formatter.
482 482
483 483 To define the callables that compute the LaTeX representation of your
484 484 objects, define a :meth:`__latex__` method or use the :meth:`for_type`
485 485 or :meth:`for_type_by_name` methods to register functions that handle
486 486 this.
487 487 """
488 488 format_type = Str('text/latex')
489 489
490 490 print_method = Str('__latex__')
491 491
492 492
493 493 class JSONFormatter(BaseFormatter):
494 494 """A JSON string formatter.
495 495
496 496 To define the callables that compute the JSON string representation of
497 497 your objects, define a :meth:`__json__` method or use the :meth:`for_type`
498 498 or :meth:`for_type_by_name` methods to register functions that handle
499 499 this.
500 500 """
501 501 format_type = Str('application/json')
502 502
503 503 print_method = Str('__json__')
504 504
505 505
506 506 FormatterABC.register(BaseFormatter)
507 507 FormatterABC.register(PlainTextFormatter)
508 508 FormatterABC.register(HTMLFormatter)
509 509 FormatterABC.register(SVGFormatter)
510 510 FormatterABC.register(PNGFormatter)
511 511 FormatterABC.register(LatexFormatter)
512 512 FormatterABC.register(JSONFormatter)
513 513
514 514
515 515 def format_display_data(obj, include=None, exclude=None):
516 516 """Return a format data dict for an object.
517 517
518 518 By default all format types will be computed.
519 519
520 520 The following MIME types are currently implemented:
521 521
522 522 * text/plain
523 523 * text/html
524 524 * text/latex
525 525 * application/json
526 526 * image/png
527 527 * immage/svg+xml
528 528
529 529 Parameters
530 530 ----------
531 531 obj : object
532 532 The Python object whose format data will be computed.
533 533
534 534 Returns
535 535 -------
536 536 format_dict : dict
537 537 A dictionary of key/value pairs, one or each format that was
538 538 generated for the object. The keys are the format types, which
539 539 will usually be MIME type strings and the values and JSON'able
540 540 data structure containing the raw data for the representation in
541 541 that format.
542 542 include : list or tuple, optional
543 543 A list of format type strings (MIME types) to include in the
544 544 format data dict. If this is set *only* the format types included
545 545 in this list will be computed.
546 546 exclude : list or tuple, optional
547 547 A list of format type string (MIME types) to exclue in the format
548 548 data dict. If this is set all format types will be computed,
549 549 except for those included in this argument.
550 550 """
551 551 from IPython.core.interactiveshell import InteractiveShell
552 552
553 553 InteractiveShell.instance().display_formatter.format(
554 554 obj,
555 555 include,
556 556 exclude
557 557 )
1 NO CONTENT: file renamed from IPython/external/Itpl.py to IPython/external/Itpl/_Itpl.py
@@ -1,2329 +1,2311 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright © 2006-2009 Steven J. Bethard <steven.bethard@gmail.com>.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 # use this file except in compliance with the License. You may obtain a copy
7 # of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
1 # Author: Steven J. Bethard <steven.bethard@gmail.com>.
16 2
17 3 """Command-line parsing library
18 4
19 5 This module is an optparse-inspired command-line parsing library that:
20 6
21 7 - handles both optional and positional arguments
22 8 - produces highly informative usage messages
23 9 - supports parsers that dispatch to sub-parsers
24 10
25 11 The following is a simple usage example that sums integers from the
26 12 command-line and writes the result to a file::
27 13
28 14 parser = argparse.ArgumentParser(
29 15 description='sum the integers at the command line')
30 16 parser.add_argument(
31 17 'integers', metavar='int', nargs='+', type=int,
32 18 help='an integer to be summed')
33 19 parser.add_argument(
34 20 '--log', default=sys.stdout, type=argparse.FileType('w'),
35 21 help='the file where the sum should be written')
36 22 args = parser.parse_args()
37 23 args.log.write('%s' % sum(args.integers))
38 24 args.log.close()
39 25
40 26 The module contains the following public classes:
41 27
42 28 - ArgumentParser -- The main entry point for command-line parsing. As the
43 29 example above shows, the add_argument() method is used to populate
44 30 the parser with actions for optional and positional arguments. Then
45 31 the parse_args() method is invoked to convert the args at the
46 32 command-line into an object with attributes.
47 33
48 34 - ArgumentError -- The exception raised by ArgumentParser objects when
49 35 there are errors with the parser's actions. Errors raised while
50 36 parsing the command-line are caught by ArgumentParser and emitted
51 37 as command-line messages.
52 38
53 39 - FileType -- A factory for defining types of files to be created. As the
54 40 example above shows, instances of FileType are typically passed as
55 41 the type= argument of add_argument() calls.
56 42
57 43 - Action -- The base class for parser actions. Typically actions are
58 44 selected by passing strings like 'store_true' or 'append_const' to
59 45 the action= argument of add_argument(). However, for greater
60 46 customization of ArgumentParser actions, subclasses of Action may
61 47 be defined and passed as the action= argument.
62 48
63 49 - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
64 50 ArgumentDefaultsHelpFormatter -- Formatter classes which
65 51 may be passed as the formatter_class= argument to the
66 52 ArgumentParser constructor. HelpFormatter is the default,
67 53 RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser
68 54 not to change the formatting for help text, and
69 55 ArgumentDefaultsHelpFormatter adds information about argument defaults
70 56 to the help.
71 57
72 58 All other classes in this module are considered implementation details.
73 59 (Also note that HelpFormatter and RawDescriptionHelpFormatter are only
74 60 considered public as object names -- the API of the formatter objects is
75 61 still considered an implementation detail.)
76 62 """
77 63
78 __version__ = '1.1a1'
64 __version__ = '1.1'
79 65 __all__ = [
80 66 'ArgumentParser',
81 67 'ArgumentError',
82 68 'Namespace',
83 69 'Action',
84 70 'FileType',
85 71 'HelpFormatter',
86 72 'RawDescriptionHelpFormatter',
87 73 'RawTextHelpFormatter',
88 74 'ArgumentDefaultsHelpFormatter',
89 75 ]
90 76
91 77
92 78 import copy as _copy
93 79 import os as _os
94 80 import re as _re
95 81 import sys as _sys
96 82 import textwrap as _textwrap
97 83
98 84 from gettext import gettext as _
99 85
100 try:
101 _set = set
102 except NameError:
103 from sets import Set as _set
104
105 try:
106 _basestring = basestring
107 except NameError:
108 _basestring = str
109
110 try:
111 _sorted = sorted
112 except NameError:
113
114 def _sorted(iterable, reverse=False):
115 result = list(iterable)
116 result.sort()
117 if reverse:
118 result.reverse()
119 return result
120
121 86
122 87 def _callable(obj):
123 88 return hasattr(obj, '__call__') or hasattr(obj, '__bases__')
124 89
125 # silence Python 2.6 buggy warnings about Exception.message
126 if _sys.version_info[:2] == (2, 6):
127 import warnings
128 warnings.filterwarnings(
129 action='ignore',
130 message='BaseException.message has been deprecated as of Python 2.6',
131 category=DeprecationWarning,
132 module='argparse')
133
134 90
135 91 SUPPRESS = '==SUPPRESS=='
136 92
137 93 OPTIONAL = '?'
138 94 ZERO_OR_MORE = '*'
139 95 ONE_OR_MORE = '+'
140 96 PARSER = 'A...'
141 97 REMAINDER = '...'
142 98
143 99 # =============================
144 100 # Utility functions and classes
145 101 # =============================
146 102
147 103 class _AttributeHolder(object):
148 104 """Abstract base class that provides __repr__.
149 105
150 106 The __repr__ method returns a string in the format::
151 107 ClassName(attr=name, attr=name, ...)
152 108 The attributes are determined either by a class-level attribute,
153 109 '_kwarg_names', or by inspecting the instance __dict__.
154 110 """
155 111
156 112 def __repr__(self):
157 113 type_name = type(self).__name__
158 114 arg_strings = []
159 115 for arg in self._get_args():
160 116 arg_strings.append(repr(arg))
161 117 for name, value in self._get_kwargs():
162 118 arg_strings.append('%s=%r' % (name, value))
163 119 return '%s(%s)' % (type_name, ', '.join(arg_strings))
164 120
165 121 def _get_kwargs(self):
166 return _sorted(self.__dict__.items())
122 return sorted(self.__dict__.items())
167 123
168 124 def _get_args(self):
169 125 return []
170 126
171 127
172 128 def _ensure_value(namespace, name, value):
173 129 if getattr(namespace, name, None) is None:
174 130 setattr(namespace, name, value)
175 131 return getattr(namespace, name)
176 132
177 133
178 134 # ===============
179 135 # Formatting Help
180 136 # ===============
181 137
182 138 class HelpFormatter(object):
183 139 """Formatter for generating usage messages and argument help strings.
184 140
185 141 Only the name of this class is considered a public API. All the methods
186 142 provided by the class are considered an implementation detail.
187 143 """
188 144
189 145 def __init__(self,
190 146 prog,
191 147 indent_increment=2,
192 148 max_help_position=24,
193 149 width=None):
194 150
195 151 # default setting for width
196 152 if width is None:
197 153 try:
198 154 width = int(_os.environ['COLUMNS'])
199 155 except (KeyError, ValueError):
200 156 width = 80
201 157 width -= 2
202 158
203 159 self._prog = prog
204 160 self._indent_increment = indent_increment
205 161 self._max_help_position = max_help_position
206 162 self._width = width
207 163
208 164 self._current_indent = 0
209 165 self._level = 0
210 166 self._action_max_length = 0
211 167
212 168 self._root_section = self._Section(self, None)
213 169 self._current_section = self._root_section
214 170
215 171 self._whitespace_matcher = _re.compile(r'\s+')
216 172 self._long_break_matcher = _re.compile(r'\n\n\n+')
217 173
218 174 # ===============================
219 175 # Section and indentation methods
220 176 # ===============================
221 177 def _indent(self):
222 178 self._current_indent += self._indent_increment
223 179 self._level += 1
224 180
225 181 def _dedent(self):
226 182 self._current_indent -= self._indent_increment
227 183 assert self._current_indent >= 0, 'Indent decreased below 0.'
228 184 self._level -= 1
229 185
230 186 class _Section(object):
231 187
232 188 def __init__(self, formatter, parent, heading=None):
233 189 self.formatter = formatter
234 190 self.parent = parent
235 191 self.heading = heading
236 192 self.items = []
237 193
238 194 def format_help(self):
239 195 # format the indented section
240 196 if self.parent is not None:
241 197 self.formatter._indent()
242 198 join = self.formatter._join_parts
243 199 for func, args in self.items:
244 200 func(*args)
245 201 item_help = join([func(*args) for func, args in self.items])
246 202 if self.parent is not None:
247 203 self.formatter._dedent()
248 204
249 205 # return nothing if the section was empty
250 206 if not item_help:
251 207 return ''
252 208
253 209 # add the heading if the section was non-empty
254 210 if self.heading is not SUPPRESS and self.heading is not None:
255 211 current_indent = self.formatter._current_indent
256 212 heading = '%*s%s:\n' % (current_indent, '', self.heading)
257 213 else:
258 214 heading = ''
259 215
260 216 # join the section-initial newline, the heading and the help
261 217 return join(['\n', heading, item_help, '\n'])
262 218
263 219 def _add_item(self, func, args):
264 220 self._current_section.items.append((func, args))
265 221
266 222 # ========================
267 223 # Message building methods
268 224 # ========================
269 225 def start_section(self, heading):
270 226 self._indent()
271 227 section = self._Section(self, self._current_section, heading)
272 228 self._add_item(section.format_help, [])
273 229 self._current_section = section
274 230
275 231 def end_section(self):
276 232 self._current_section = self._current_section.parent
277 233 self._dedent()
278 234
279 235 def add_text(self, text):
280 236 if text is not SUPPRESS and text is not None:
281 237 self._add_item(self._format_text, [text])
282 238
283 239 def add_usage(self, usage, actions, groups, prefix=None):
284 240 if usage is not SUPPRESS:
285 241 args = usage, actions, groups, prefix
286 242 self._add_item(self._format_usage, args)
287 243
288 244 def add_argument(self, action):
289 245 if action.help is not SUPPRESS:
290 246
291 247 # find all invocations
292 248 get_invocation = self._format_action_invocation
293 249 invocations = [get_invocation(action)]
294 250 for subaction in self._iter_indented_subactions(action):
295 251 invocations.append(get_invocation(subaction))
296 252
297 253 # update the maximum item length
298 254 invocation_length = max([len(s) for s in invocations])
299 255 action_length = invocation_length + self._current_indent
300 256 self._action_max_length = max(self._action_max_length,
301 257 action_length)
302 258
303 259 # add the item to the list
304 260 self._add_item(self._format_action, [action])
305 261
306 262 def add_arguments(self, actions):
307 263 for action in actions:
308 264 self.add_argument(action)
309 265
310 266 # =======================
311 267 # Help-formatting methods
312 268 # =======================
313 269 def format_help(self):
314 270 help = self._root_section.format_help()
315 271 if help:
316 272 help = self._long_break_matcher.sub('\n\n', help)
317 273 help = help.strip('\n') + '\n'
318 274 return help
319 275
320 276 def _join_parts(self, part_strings):
321 277 return ''.join([part
322 278 for part in part_strings
323 279 if part and part is not SUPPRESS])
324 280
325 281 def _format_usage(self, usage, actions, groups, prefix):
326 282 if prefix is None:
327 283 prefix = _('usage: ')
328 284
329 285 # if usage is specified, use that
330 286 if usage is not None:
331 287 usage = usage % dict(prog=self._prog)
332 288
333 289 # if no optionals or positionals are available, usage is just prog
334 290 elif usage is None and not actions:
335 291 usage = '%(prog)s' % dict(prog=self._prog)
336 292
337 293 # if optionals and positionals are available, calculate usage
338 294 elif usage is None:
339 295 prog = '%(prog)s' % dict(prog=self._prog)
340 296
341 297 # split optionals from positionals
342 298 optionals = []
343 299 positionals = []
344 300 for action in actions:
345 301 if action.option_strings:
346 302 optionals.append(action)
347 303 else:
348 304 positionals.append(action)
349 305
350 306 # build full usage string
351 307 format = self._format_actions_usage
352 308 action_usage = format(optionals + positionals, groups)
353 309 usage = ' '.join([s for s in [prog, action_usage] if s])
354 310
355 311 # wrap the usage parts if it's too long
356 312 text_width = self._width - self._current_indent
357 313 if len(prefix) + len(usage) > text_width:
358 314
359 315 # break usage into wrappable parts
360 316 part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
361 317 opt_usage = format(optionals, groups)
362 318 pos_usage = format(positionals, groups)
363 319 opt_parts = _re.findall(part_regexp, opt_usage)
364 320 pos_parts = _re.findall(part_regexp, pos_usage)
365 321 assert ' '.join(opt_parts) == opt_usage
366 322 assert ' '.join(pos_parts) == pos_usage
367 323
368 324 # helper for wrapping lines
369 325 def get_lines(parts, indent, prefix=None):
370 326 lines = []
371 327 line = []
372 328 if prefix is not None:
373 329 line_len = len(prefix) - 1
374 330 else:
375 331 line_len = len(indent) - 1
376 332 for part in parts:
377 333 if line_len + 1 + len(part) > text_width:
378 334 lines.append(indent + ' '.join(line))
379 335 line = []
380 336 line_len = len(indent) - 1
381 337 line.append(part)
382 338 line_len += len(part) + 1
383 339 if line:
384 340 lines.append(indent + ' '.join(line))
385 341 if prefix is not None:
386 342 lines[0] = lines[0][len(indent):]
387 343 return lines
388 344
389 345 # if prog is short, follow it with optionals or positionals
390 346 if len(prefix) + len(prog) <= 0.75 * text_width:
391 347 indent = ' ' * (len(prefix) + len(prog) + 1)
392 348 if opt_parts:
393 349 lines = get_lines([prog] + opt_parts, indent, prefix)
394 350 lines.extend(get_lines(pos_parts, indent))
395 351 elif pos_parts:
396 352 lines = get_lines([prog] + pos_parts, indent, prefix)
397 353 else:
398 354 lines = [prog]
399 355
400 356 # if prog is long, put it on its own line
401 357 else:
402 358 indent = ' ' * len(prefix)
403 359 parts = opt_parts + pos_parts
404 360 lines = get_lines(parts, indent)
405 361 if len(lines) > 1:
406 362 lines = []
407 363 lines.extend(get_lines(opt_parts, indent))
408 364 lines.extend(get_lines(pos_parts, indent))
409 365 lines = [prog] + lines
410 366
411 367 # join lines into usage
412 368 usage = '\n'.join(lines)
413 369
414 370 # prefix with 'usage:'
415 371 return '%s%s\n\n' % (prefix, usage)
416 372
417 373 def _format_actions_usage(self, actions, groups):
418 374 # find group indices and identify actions in groups
419 group_actions = _set()
375 group_actions = set()
420 376 inserts = {}
421 377 for group in groups:
422 378 try:
423 379 start = actions.index(group._group_actions[0])
424 380 except ValueError:
425 381 continue
426 382 else:
427 383 end = start + len(group._group_actions)
428 384 if actions[start:end] == group._group_actions:
429 385 for action in group._group_actions:
430 386 group_actions.add(action)
431 387 if not group.required:
432 388 inserts[start] = '['
433 389 inserts[end] = ']'
434 390 else:
435 391 inserts[start] = '('
436 392 inserts[end] = ')'
437 393 for i in range(start + 1, end):
438 394 inserts[i] = '|'
439 395
440 396 # collect all actions format strings
441 397 parts = []
442 398 for i, action in enumerate(actions):
443 399
444 400 # suppressed arguments are marked with None
445 401 # remove | separators for suppressed arguments
446 402 if action.help is SUPPRESS:
447 403 parts.append(None)
448 404 if inserts.get(i) == '|':
449 405 inserts.pop(i)
450 406 elif inserts.get(i + 1) == '|':
451 407 inserts.pop(i + 1)
452 408
453 409 # produce all arg strings
454 410 elif not action.option_strings:
455 411 part = self._format_args(action, action.dest)
456 412
457 413 # if it's in a group, strip the outer []
458 414 if action in group_actions:
459 415 if part[0] == '[' and part[-1] == ']':
460 416 part = part[1:-1]
461 417
462 418 # add the action string to the list
463 419 parts.append(part)
464 420
465 421 # produce the first way to invoke the option in brackets
466 422 else:
467 423 option_string = action.option_strings[0]
468 424
469 425 # if the Optional doesn't take a value, format is:
470 426 # -s or --long
471 427 if action.nargs == 0:
472 428 part = '%s' % option_string
473 429
474 430 # if the Optional takes a value, format is:
475 431 # -s ARGS or --long ARGS
476 432 else:
477 433 default = action.dest.upper()
478 434 args_string = self._format_args(action, default)
479 435 part = '%s %s' % (option_string, args_string)
480 436
481 437 # make it look optional if it's not required or in a group
482 438 if not action.required and action not in group_actions:
483 439 part = '[%s]' % part
484 440
485 441 # add the action string to the list
486 442 parts.append(part)
487 443
488 444 # insert things at the necessary indices
489 for i in _sorted(inserts, reverse=True):
445 for i in sorted(inserts, reverse=True):
490 446 parts[i:i] = [inserts[i]]
491 447
492 448 # join all the action items with spaces
493 449 text = ' '.join([item for item in parts if item is not None])
494 450
495 451 # clean up separators for mutually exclusive groups
496 452 open = r'[\[(]'
497 453 close = r'[\])]'
498 454 text = _re.sub(r'(%s) ' % open, r'\1', text)
499 455 text = _re.sub(r' (%s)' % close, r'\1', text)
500 456 text = _re.sub(r'%s *%s' % (open, close), r'', text)
501 457 text = _re.sub(r'\(([^|]*)\)', r'\1', text)
502 458 text = text.strip()
503 459
504 460 # return the text
505 461 return text
506 462
507 463 def _format_text(self, text):
508 464 if '%(prog)' in text:
509 465 text = text % dict(prog=self._prog)
510 466 text_width = self._width - self._current_indent
511 467 indent = ' ' * self._current_indent
512 468 return self._fill_text(text, text_width, indent) + '\n\n'
513 469
514 470 def _format_action(self, action):
515 471 # determine the required width and the entry label
516 472 help_position = min(self._action_max_length + 2,
517 473 self._max_help_position)
518 474 help_width = self._width - help_position
519 475 action_width = help_position - self._current_indent - 2
520 476 action_header = self._format_action_invocation(action)
521 477
522 478 # ho nelp; start on same line and add a final newline
523 479 if not action.help:
524 480 tup = self._current_indent, '', action_header
525 481 action_header = '%*s%s\n' % tup
526 482
527 483 # short action name; start on the same line and pad two spaces
528 484 elif len(action_header) <= action_width:
529 485 tup = self._current_indent, '', action_width, action_header
530 486 action_header = '%*s%-*s ' % tup
531 487 indent_first = 0
532 488
533 489 # long action name; start on the next line
534 490 else:
535 491 tup = self._current_indent, '', action_header
536 492 action_header = '%*s%s\n' % tup
537 493 indent_first = help_position
538 494
539 495 # collect the pieces of the action help
540 496 parts = [action_header]
541 497
542 498 # if there was help for the action, add lines of help text
543 499 if action.help:
544 500 help_text = self._expand_help(action)
545 501 help_lines = self._split_lines(help_text, help_width)
546 502 parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
547 503 for line in help_lines[1:]:
548 504 parts.append('%*s%s\n' % (help_position, '', line))
549 505
550 506 # or add a newline if the description doesn't end with one
551 507 elif not action_header.endswith('\n'):
552 508 parts.append('\n')
553 509
554 510 # if there are any sub-actions, add their help as well
555 511 for subaction in self._iter_indented_subactions(action):
556 512 parts.append(self._format_action(subaction))
557 513
558 514 # return a single string
559 515 return self._join_parts(parts)
560 516
561 517 def _format_action_invocation(self, action):
562 518 if not action.option_strings:
563 519 metavar, = self._metavar_formatter(action, action.dest)(1)
564 520 return metavar
565 521
566 522 else:
567 523 parts = []
568 524
569 525 # if the Optional doesn't take a value, format is:
570 526 # -s, --long
571 527 if action.nargs == 0:
572 528 parts.extend(action.option_strings)
573 529
574 530 # if the Optional takes a value, format is:
575 531 # -s ARGS, --long ARGS
576 532 else:
577 533 default = action.dest.upper()
578 534 args_string = self._format_args(action, default)
579 535 for option_string in action.option_strings:
580 536 parts.append('%s %s' % (option_string, args_string))
581 537
582 538 return ', '.join(parts)
583 539
584 540 def _metavar_formatter(self, action, default_metavar):
585 541 if action.metavar is not None:
586 542 result = action.metavar
587 543 elif action.choices is not None:
588 544 choice_strs = [str(choice) for choice in action.choices]
589 545 result = '{%s}' % ','.join(choice_strs)
590 546 else:
591 547 result = default_metavar
592 548
593 549 def format(tuple_size):
594 550 if isinstance(result, tuple):
595 551 return result
596 552 else:
597 553 return (result, ) * tuple_size
598 554 return format
599 555
600 556 def _format_args(self, action, default_metavar):
601 557 get_metavar = self._metavar_formatter(action, default_metavar)
602 558 if action.nargs is None:
603 559 result = '%s' % get_metavar(1)
604 560 elif action.nargs == OPTIONAL:
605 561 result = '[%s]' % get_metavar(1)
606 562 elif action.nargs == ZERO_OR_MORE:
607 563 result = '[%s [%s ...]]' % get_metavar(2)
608 564 elif action.nargs == ONE_OR_MORE:
609 565 result = '%s [%s ...]' % get_metavar(2)
610 566 elif action.nargs == REMAINDER:
611 567 result = '...'
612 568 elif action.nargs == PARSER:
613 569 result = '%s ...' % get_metavar(1)
614 570 else:
615 571 formats = ['%s' for _ in range(action.nargs)]
616 572 result = ' '.join(formats) % get_metavar(action.nargs)
617 573 return result
618 574
619 575 def _expand_help(self, action):
620 576 params = dict(vars(action), prog=self._prog)
621 577 for name in list(params):
622 578 if params[name] is SUPPRESS:
623 579 del params[name]
580 for name in list(params):
581 if hasattr(params[name], '__name__'):
582 params[name] = params[name].__name__
624 583 if params.get('choices') is not None:
625 584 choices_str = ', '.join([str(c) for c in params['choices']])
626 585 params['choices'] = choices_str
627 586 return self._get_help_string(action) % params
628 587
629 588 def _iter_indented_subactions(self, action):
630 589 try:
631 590 get_subactions = action._get_subactions
632 591 except AttributeError:
633 592 pass
634 593 else:
635 594 self._indent()
636 595 for subaction in get_subactions():
637 596 yield subaction
638 597 self._dedent()
639 598
640 599 def _split_lines(self, text, width):
641 600 text = self._whitespace_matcher.sub(' ', text).strip()
642 601 return _textwrap.wrap(text, width)
643 602
644 603 def _fill_text(self, text, width, indent):
645 604 text = self._whitespace_matcher.sub(' ', text).strip()
646 605 return _textwrap.fill(text, width, initial_indent=indent,
647 606 subsequent_indent=indent)
648 607
649 608 def _get_help_string(self, action):
650 609 return action.help
651 610
652 611
653 612 class RawDescriptionHelpFormatter(HelpFormatter):
654 613 """Help message formatter which retains any formatting in descriptions.
655 614
656 615 Only the name of this class is considered a public API. All the methods
657 616 provided by the class are considered an implementation detail.
658 617 """
659 618
660 619 def _fill_text(self, text, width, indent):
661 620 return ''.join([indent + line for line in text.splitlines(True)])
662 621
663 622
664 623 class RawTextHelpFormatter(RawDescriptionHelpFormatter):
665 624 """Help message formatter which retains formatting of all help text.
666 625
667 626 Only the name of this class is considered a public API. All the methods
668 627 provided by the class are considered an implementation detail.
669 628 """
670 629
671 630 def _split_lines(self, text, width):
672 631 return text.splitlines()
673 632
674 633
675 634 class ArgumentDefaultsHelpFormatter(HelpFormatter):
676 635 """Help message formatter which adds default values to argument help.
677 636
678 637 Only the name of this class is considered a public API. All the methods
679 638 provided by the class are considered an implementation detail.
680 639 """
681 640
682 641 def _get_help_string(self, action):
683 642 help = action.help
684 643 if '%(default)' not in action.help:
685 644 if action.default is not SUPPRESS:
686 645 defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
687 646 if action.option_strings or action.nargs in defaulting_nargs:
688 647 help += ' (default: %(default)s)'
689 648 return help
690 649
691 650
692 651 # =====================
693 652 # Options and Arguments
694 653 # =====================
695 654
696 655 def _get_action_name(argument):
697 656 if argument is None:
698 657 return None
699 658 elif argument.option_strings:
700 659 return '/'.join(argument.option_strings)
701 660 elif argument.metavar not in (None, SUPPRESS):
702 661 return argument.metavar
703 662 elif argument.dest not in (None, SUPPRESS):
704 663 return argument.dest
705 664 else:
706 665 return None
707 666
708 667
709 668 class ArgumentError(Exception):
710 669 """An error from creating or using an argument (optional or positional).
711 670
712 671 The string value of this exception is the message, augmented with
713 672 information about the argument that caused it.
714 673 """
715 674
716 675 def __init__(self, argument, message):
717 676 self.argument_name = _get_action_name(argument)
718 677 self.message = message
719 678
720 679 def __str__(self):
721 680 if self.argument_name is None:
722 681 format = '%(message)s'
723 682 else:
724 683 format = 'argument %(argument_name)s: %(message)s'
725 684 return format % dict(message=self.message,
726 685 argument_name=self.argument_name)
727 686
728 687
729 688 class ArgumentTypeError(Exception):
730 689 """An error from trying to convert a command line string to a type."""
731 690 pass
732 691
733 692
734 693 # ==============
735 694 # Action classes
736 695 # ==============
737 696
738 697 class Action(_AttributeHolder):
739 698 """Information about how to convert command line strings to Python objects.
740 699
741 700 Action objects are used by an ArgumentParser to represent the information
742 701 needed to parse a single argument from one or more strings from the
743 702 command line. The keyword arguments to the Action constructor are also
744 703 all attributes of Action instances.
745 704
746 705 Keyword Arguments:
747 706
748 707 - option_strings -- A list of command-line option strings which
749 708 should be associated with this action.
750 709
751 710 - dest -- The name of the attribute to hold the created object(s)
752 711
753 712 - nargs -- The number of command-line arguments that should be
754 713 consumed. By default, one argument will be consumed and a single
755 714 value will be produced. Other values include:
756 715 - N (an integer) consumes N arguments (and produces a list)
757 716 - '?' consumes zero or one arguments
758 717 - '*' consumes zero or more arguments (and produces a list)
759 718 - '+' consumes one or more arguments (and produces a list)
760 719 Note that the difference between the default and nargs=1 is that
761 720 with the default, a single value will be produced, while with
762 721 nargs=1, a list containing a single value will be produced.
763 722
764 723 - const -- The value to be produced if the option is specified and the
765 724 option uses an action that takes no values.
766 725
767 726 - default -- The value to be produced if the option is not specified.
768 727
769 728 - type -- The type which the command-line arguments should be converted
770 729 to, should be one of 'string', 'int', 'float', 'complex' or a
771 730 callable object that accepts a single string argument. If None,
772 731 'string' is assumed.
773 732
774 733 - choices -- A container of values that should be allowed. If not None,
775 734 after a command-line argument has been converted to the appropriate
776 735 type, an exception will be raised if it is not a member of this
777 736 collection.
778 737
779 738 - required -- True if the action must always be specified at the
780 739 command line. This is only meaningful for optional command-line
781 740 arguments.
782 741
783 742 - help -- The help string describing the argument.
784 743
785 744 - metavar -- The name to be used for the option's argument with the
786 745 help string. If None, the 'dest' value will be used as the name.
787 746 """
788 747
789 748 def __init__(self,
790 749 option_strings,
791 750 dest,
792 751 nargs=None,
793 752 const=None,
794 753 default=None,
795 754 type=None,
796 755 choices=None,
797 756 required=False,
798 757 help=None,
799 758 metavar=None):
800 759 self.option_strings = option_strings
801 760 self.dest = dest
802 761 self.nargs = nargs
803 762 self.const = const
804 763 self.default = default
805 764 self.type = type
806 765 self.choices = choices
807 766 self.required = required
808 767 self.help = help
809 768 self.metavar = metavar
810 769
811 770 def _get_kwargs(self):
812 771 names = [
813 772 'option_strings',
814 773 'dest',
815 774 'nargs',
816 775 'const',
817 776 'default',
818 777 'type',
819 778 'choices',
820 779 'help',
821 780 'metavar',
822 781 ]
823 782 return [(name, getattr(self, name)) for name in names]
824 783
825 784 def __call__(self, parser, namespace, values, option_string=None):
826 785 raise NotImplementedError(_('.__call__() not defined'))
827 786
828 787
829 788 class _StoreAction(Action):
830 789
831 790 def __init__(self,
832 791 option_strings,
833 792 dest,
834 793 nargs=None,
835 794 const=None,
836 795 default=None,
837 796 type=None,
838 797 choices=None,
839 798 required=False,
840 799 help=None,
841 800 metavar=None):
842 801 if nargs == 0:
843 802 raise ValueError('nargs for store actions must be > 0; if you '
844 803 'have nothing to store, actions such as store '
845 804 'true or store const may be more appropriate')
846 805 if const is not None and nargs != OPTIONAL:
847 806 raise ValueError('nargs must be %r to supply const' % OPTIONAL)
848 807 super(_StoreAction, self).__init__(
849 808 option_strings=option_strings,
850 809 dest=dest,
851 810 nargs=nargs,
852 811 const=const,
853 812 default=default,
854 813 type=type,
855 814 choices=choices,
856 815 required=required,
857 816 help=help,
858 817 metavar=metavar)
859 818
860 819 def __call__(self, parser, namespace, values, option_string=None):
861 820 setattr(namespace, self.dest, values)
862 821
863 822
864 823 class _StoreConstAction(Action):
865 824
866 825 def __init__(self,
867 826 option_strings,
868 827 dest,
869 828 const,
870 829 default=None,
871 830 required=False,
872 831 help=None,
873 832 metavar=None):
874 833 super(_StoreConstAction, self).__init__(
875 834 option_strings=option_strings,
876 835 dest=dest,
877 836 nargs=0,
878 837 const=const,
879 838 default=default,
880 839 required=required,
881 840 help=help)
882 841
883 842 def __call__(self, parser, namespace, values, option_string=None):
884 843 setattr(namespace, self.dest, self.const)
885 844
886 845
887 846 class _StoreTrueAction(_StoreConstAction):
888 847
889 848 def __init__(self,
890 849 option_strings,
891 850 dest,
892 851 default=False,
893 852 required=False,
894 853 help=None):
895 854 super(_StoreTrueAction, self).__init__(
896 855 option_strings=option_strings,
897 856 dest=dest,
898 857 const=True,
899 858 default=default,
900 859 required=required,
901 860 help=help)
902 861
903 862
904 863 class _StoreFalseAction(_StoreConstAction):
905 864
906 865 def __init__(self,
907 866 option_strings,
908 867 dest,
909 868 default=True,
910 869 required=False,
911 870 help=None):
912 871 super(_StoreFalseAction, self).__init__(
913 872 option_strings=option_strings,
914 873 dest=dest,
915 874 const=False,
916 875 default=default,
917 876 required=required,
918 877 help=help)
919 878
920 879
921 880 class _AppendAction(Action):
922 881
923 882 def __init__(self,
924 883 option_strings,
925 884 dest,
926 885 nargs=None,
927 886 const=None,
928 887 default=None,
929 888 type=None,
930 889 choices=None,
931 890 required=False,
932 891 help=None,
933 892 metavar=None):
934 893 if nargs == 0:
935 894 raise ValueError('nargs for append actions must be > 0; if arg '
936 895 'strings are not supplying the value to append, '
937 896 'the append const action may be more appropriate')
938 897 if const is not None and nargs != OPTIONAL:
939 898 raise ValueError('nargs must be %r to supply const' % OPTIONAL)
940 899 super(_AppendAction, self).__init__(
941 900 option_strings=option_strings,
942 901 dest=dest,
943 902 nargs=nargs,
944 903 const=const,
945 904 default=default,
946 905 type=type,
947 906 choices=choices,
948 907 required=required,
949 908 help=help,
950 909 metavar=metavar)
951 910
952 911 def __call__(self, parser, namespace, values, option_string=None):
953 912 items = _copy.copy(_ensure_value(namespace, self.dest, []))
954 913 items.append(values)
955 914 setattr(namespace, self.dest, items)
956 915
957 916
958 917 class _AppendConstAction(Action):
959 918
960 919 def __init__(self,
961 920 option_strings,
962 921 dest,
963 922 const,
964 923 default=None,
965 924 required=False,
966 925 help=None,
967 926 metavar=None):
968 927 super(_AppendConstAction, self).__init__(
969 928 option_strings=option_strings,
970 929 dest=dest,
971 930 nargs=0,
972 931 const=const,
973 932 default=default,
974 933 required=required,
975 934 help=help,
976 935 metavar=metavar)
977 936
978 937 def __call__(self, parser, namespace, values, option_string=None):
979 938 items = _copy.copy(_ensure_value(namespace, self.dest, []))
980 939 items.append(self.const)
981 940 setattr(namespace, self.dest, items)
982 941
983 942
984 943 class _CountAction(Action):
985 944
986 945 def __init__(self,
987 946 option_strings,
988 947 dest,
989 948 default=None,
990 949 required=False,
991 950 help=None):
992 951 super(_CountAction, self).__init__(
993 952 option_strings=option_strings,
994 953 dest=dest,
995 954 nargs=0,
996 955 default=default,
997 956 required=required,
998 957 help=help)
999 958
1000 959 def __call__(self, parser, namespace, values, option_string=None):
1001 960 new_count = _ensure_value(namespace, self.dest, 0) + 1
1002 961 setattr(namespace, self.dest, new_count)
1003 962
1004 963
1005 964 class _HelpAction(Action):
1006 965
1007 966 def __init__(self,
1008 967 option_strings,
1009 968 dest=SUPPRESS,
1010 969 default=SUPPRESS,
1011 970 help=None):
1012 971 super(_HelpAction, self).__init__(
1013 972 option_strings=option_strings,
1014 973 dest=dest,
1015 974 default=default,
1016 975 nargs=0,
1017 976 help=help)
1018 977
1019 978 def __call__(self, parser, namespace, values, option_string=None):
1020 979 parser.print_help()
1021 980 parser.exit()
1022 981
1023 982
1024 983 class _VersionAction(Action):
1025 984
1026 985 def __init__(self,
1027 986 option_strings,
1028 987 version=None,
1029 988 dest=SUPPRESS,
1030 989 default=SUPPRESS,
1031 help=None):
990 help="show program's version number and exit"):
1032 991 super(_VersionAction, self).__init__(
1033 992 option_strings=option_strings,
1034 993 dest=dest,
1035 994 default=default,
1036 995 nargs=0,
1037 996 help=help)
1038 997 self.version = version
1039 998
1040 999 def __call__(self, parser, namespace, values, option_string=None):
1041 1000 version = self.version
1042 1001 if version is None:
1043 1002 version = parser.version
1044 1003 formatter = parser._get_formatter()
1045 1004 formatter.add_text(version)
1046 1005 parser.exit(message=formatter.format_help())
1047 1006
1048 1007
1049 1008 class _SubParsersAction(Action):
1050 1009
1051 1010 class _ChoicesPseudoAction(Action):
1052 1011
1053 1012 def __init__(self, name, help):
1054 1013 sup = super(_SubParsersAction._ChoicesPseudoAction, self)
1055 1014 sup.__init__(option_strings=[], dest=name, help=help)
1056 1015
1057 1016 def __init__(self,
1058 1017 option_strings,
1059 1018 prog,
1060 1019 parser_class,
1061 1020 dest=SUPPRESS,
1062 1021 help=None,
1063 1022 metavar=None):
1064 1023
1065 1024 self._prog_prefix = prog
1066 1025 self._parser_class = parser_class
1067 1026 self._name_parser_map = {}
1068 1027 self._choices_actions = []
1069 1028
1070 1029 super(_SubParsersAction, self).__init__(
1071 1030 option_strings=option_strings,
1072 1031 dest=dest,
1073 1032 nargs=PARSER,
1074 1033 choices=self._name_parser_map,
1075 1034 help=help,
1076 1035 metavar=metavar)
1077 1036
1078 1037 def add_parser(self, name, **kwargs):
1079 1038 # set prog from the existing prefix
1080 1039 if kwargs.get('prog') is None:
1081 1040 kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
1082 1041
1083 1042 # create a pseudo-action to hold the choice help
1084 1043 if 'help' in kwargs:
1085 1044 help = kwargs.pop('help')
1086 1045 choice_action = self._ChoicesPseudoAction(name, help)
1087 1046 self._choices_actions.append(choice_action)
1088 1047
1089 1048 # create the parser and add it to the map
1090 1049 parser = self._parser_class(**kwargs)
1091 1050 self._name_parser_map[name] = parser
1092 1051 return parser
1093 1052
1094 1053 def _get_subactions(self):
1095 1054 return self._choices_actions
1096 1055
1097 1056 def __call__(self, parser, namespace, values, option_string=None):
1098 1057 parser_name = values[0]
1099 1058 arg_strings = values[1:]
1100 1059
1101 1060 # set the parser name if requested
1102 1061 if self.dest is not SUPPRESS:
1103 1062 setattr(namespace, self.dest, parser_name)
1104 1063
1105 1064 # select the parser
1106 1065 try:
1107 1066 parser = self._name_parser_map[parser_name]
1108 1067 except KeyError:
1109 1068 tup = parser_name, ', '.join(self._name_parser_map)
1110 1069 msg = _('unknown parser %r (choices: %s)' % tup)
1111 1070 raise ArgumentError(self, msg)
1112 1071
1113 1072 # parse all the remaining options into the namespace
1114 1073 parser.parse_args(arg_strings, namespace)
1115 1074
1116 1075
1117 1076 # ==============
1118 1077 # Type classes
1119 1078 # ==============
1120 1079
1121 1080 class FileType(object):
1122 1081 """Factory for creating file object types
1123 1082
1124 1083 Instances of FileType are typically passed as type= arguments to the
1125 1084 ArgumentParser add_argument() method.
1126 1085
1127 1086 Keyword Arguments:
1128 1087 - mode -- A string indicating how the file is to be opened. Accepts the
1129 1088 same values as the builtin open() function.
1130 1089 - bufsize -- The file's desired buffer size. Accepts the same values as
1131 1090 the builtin open() function.
1132 1091 """
1133 1092
1134 1093 def __init__(self, mode='r', bufsize=None):
1135 1094 self._mode = mode
1136 1095 self._bufsize = bufsize
1137 1096
1138 1097 def __call__(self, string):
1139 1098 # the special argument "-" means sys.std{in,out}
1140 1099 if string == '-':
1141 1100 if 'r' in self._mode:
1142 1101 return _sys.stdin
1143 1102 elif 'w' in self._mode:
1144 1103 return _sys.stdout
1145 1104 else:
1146 1105 msg = _('argument "-" with mode %r' % self._mode)
1147 1106 raise ValueError(msg)
1148 1107
1149 1108 # all other arguments are used as file names
1150 1109 if self._bufsize:
1151 1110 return open(string, self._mode, self._bufsize)
1152 1111 else:
1153 1112 return open(string, self._mode)
1154 1113
1155 1114 def __repr__(self):
1156 1115 args = [self._mode, self._bufsize]
1157 1116 args_str = ', '.join([repr(arg) for arg in args if arg is not None])
1158 1117 return '%s(%s)' % (type(self).__name__, args_str)
1159 1118
1160 1119 # ===========================
1161 1120 # Optional and Positional Parsing
1162 1121 # ===========================
1163 1122
1164 1123 class Namespace(_AttributeHolder):
1165 1124 """Simple object for storing attributes.
1166 1125
1167 1126 Implements equality by attribute names and values, and provides a simple
1168 1127 string representation.
1169 1128 """
1170 1129
1171 1130 def __init__(self, **kwargs):
1172 self.__dict__.update(**kwargs)
1131 for name in kwargs:
1132 setattr(self, name, kwargs[name])
1133
1134 __hash__ = None
1173 1135
1174 1136 def __eq__(self, other):
1175 1137 return vars(self) == vars(other)
1176 1138
1177 1139 def __ne__(self, other):
1178 1140 return not (self == other)
1179 1141
1180 1142 def __contains__(self, key):
1181 1143 return key in self.__dict__
1182 1144
1183 1145
1184 1146 class _ActionsContainer(object):
1185 1147
1186 1148 def __init__(self,
1187 1149 description,
1188 1150 prefix_chars,
1189 1151 argument_default,
1190 1152 conflict_handler):
1191 1153 super(_ActionsContainer, self).__init__()
1192 1154
1193 1155 self.description = description
1194 1156 self.argument_default = argument_default
1195 1157 self.prefix_chars = prefix_chars
1196 1158 self.conflict_handler = conflict_handler
1197 1159
1198 1160 # set up registries
1199 1161 self._registries = {}
1200 1162
1201 1163 # register actions
1202 1164 self.register('action', None, _StoreAction)
1203 1165 self.register('action', 'store', _StoreAction)
1204 1166 self.register('action', 'store_const', _StoreConstAction)
1205 1167 self.register('action', 'store_true', _StoreTrueAction)
1206 1168 self.register('action', 'store_false', _StoreFalseAction)
1207 1169 self.register('action', 'append', _AppendAction)
1208 1170 self.register('action', 'append_const', _AppendConstAction)
1209 1171 self.register('action', 'count', _CountAction)
1210 1172 self.register('action', 'help', _HelpAction)
1211 1173 self.register('action', 'version', _VersionAction)
1212 1174 self.register('action', 'parsers', _SubParsersAction)
1213 1175
1214 1176 # raise an exception if the conflict handler is invalid
1215 1177 self._get_handler()
1216 1178
1217 1179 # action storage
1218 1180 self._actions = []
1219 1181 self._option_string_actions = {}
1220 1182
1221 1183 # groups
1222 1184 self._action_groups = []
1223 1185 self._mutually_exclusive_groups = []
1224 1186
1225 1187 # defaults storage
1226 1188 self._defaults = {}
1227 1189
1228 1190 # determines whether an "option" looks like a negative number
1229 1191 self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')
1230 1192
1231 1193 # whether or not there are any optionals that look like negative
1232 1194 # numbers -- uses a list so it can be shared and edited
1233 1195 self._has_negative_number_optionals = []
1234 1196
1235 1197 # ====================
1236 1198 # Registration methods
1237 1199 # ====================
1238 1200 def register(self, registry_name, value, object):
1239 1201 registry = self._registries.setdefault(registry_name, {})
1240 1202 registry[value] = object
1241 1203
1242 1204 def _registry_get(self, registry_name, value, default=None):
1243 1205 return self._registries[registry_name].get(value, default)
1244 1206
1245 1207 # ==================================
1246 1208 # Namespace default accessor methods
1247 1209 # ==================================
1248 1210 def set_defaults(self, **kwargs):
1249 1211 self._defaults.update(kwargs)
1250 1212
1251 1213 # if these defaults match any existing arguments, replace
1252 1214 # the previous default on the object with the new one
1253 1215 for action in self._actions:
1254 1216 if action.dest in kwargs:
1255 1217 action.default = kwargs[action.dest]
1256 1218
1257 1219 def get_default(self, dest):
1258 1220 for action in self._actions:
1259 1221 if action.dest == dest and action.default is not None:
1260 1222 return action.default
1261 1223 return self._defaults.get(dest, None)
1262 1224
1263 1225
1264 1226 # =======================
1265 1227 # Adding argument actions
1266 1228 # =======================
1267 1229 def add_argument(self, *args, **kwargs):
1268 1230 """
1269 1231 add_argument(dest, ..., name=value, ...)
1270 1232 add_argument(option_string, option_string, ..., name=value, ...)
1271 1233 """
1272 1234
1273 1235 # if no positional args are supplied or only one is supplied and
1274 1236 # it doesn't look like an option string, parse a positional
1275 1237 # argument
1276 1238 chars = self.prefix_chars
1277 1239 if not args or len(args) == 1 and args[0][0] not in chars:
1278 1240 if args and 'dest' in kwargs:
1279 1241 raise ValueError('dest supplied twice for positional argument')
1280 1242 kwargs = self._get_positional_kwargs(*args, **kwargs)
1281 1243
1282 1244 # otherwise, we're adding an optional argument
1283 1245 else:
1284 1246 kwargs = self._get_optional_kwargs(*args, **kwargs)
1285 1247
1286 1248 # if no default was supplied, use the parser-level default
1287 1249 if 'default' not in kwargs:
1288 1250 dest = kwargs['dest']
1289 1251 if dest in self._defaults:
1290 1252 kwargs['default'] = self._defaults[dest]
1291 1253 elif self.argument_default is not None:
1292 1254 kwargs['default'] = self.argument_default
1293 1255
1294 1256 # create the action object, and add it to the parser
1295 1257 action_class = self._pop_action_class(kwargs)
1296 1258 if not _callable(action_class):
1297 1259 raise ValueError('unknown action "%s"' % action_class)
1298 1260 action = action_class(**kwargs)
1261
1262 # raise an error if the action type is not callable
1263 type_func = self._registry_get('type', action.type, action.type)
1264 if not _callable(type_func):
1265 raise ValueError('%r is not callable' % type_func)
1266
1299 1267 return self._add_action(action)
1300 1268
1301 1269 def add_argument_group(self, *args, **kwargs):
1302 1270 group = _ArgumentGroup(self, *args, **kwargs)
1303 1271 self._action_groups.append(group)
1304 1272 return group
1305 1273
1306 1274 def add_mutually_exclusive_group(self, **kwargs):
1307 1275 group = _MutuallyExclusiveGroup(self, **kwargs)
1308 1276 self._mutually_exclusive_groups.append(group)
1309 1277 return group
1310 1278
1311 1279 def _add_action(self, action):
1312 1280 # resolve any conflicts
1313 1281 self._check_conflict(action)
1314 1282
1315 1283 # add to actions list
1316 1284 self._actions.append(action)
1317 1285 action.container = self
1318 1286
1319 1287 # index the action by any option strings it has
1320 1288 for option_string in action.option_strings:
1321 1289 self._option_string_actions[option_string] = action
1322 1290
1323 1291 # set the flag if any option strings look like negative numbers
1324 1292 for option_string in action.option_strings:
1325 1293 if self._negative_number_matcher.match(option_string):
1326 1294 if not self._has_negative_number_optionals:
1327 1295 self._has_negative_number_optionals.append(True)
1328 1296
1329 1297 # return the created action
1330 1298 return action
1331 1299
1332 1300 def _remove_action(self, action):
1333 1301 self._actions.remove(action)
1334 1302
1335 1303 def _add_container_actions(self, container):
1336 1304 # collect groups by titles
1337 1305 title_group_map = {}
1338 1306 for group in self._action_groups:
1339 1307 if group.title in title_group_map:
1340 1308 msg = _('cannot merge actions - two groups are named %r')
1341 1309 raise ValueError(msg % (group.title))
1342 1310 title_group_map[group.title] = group
1343 1311
1344 1312 # map each action to its group
1345 1313 group_map = {}
1346 1314 for group in container._action_groups:
1347 1315
1348 1316 # if a group with the title exists, use that, otherwise
1349 1317 # create a new group matching the container's group
1350 1318 if group.title not in title_group_map:
1351 1319 title_group_map[group.title] = self.add_argument_group(
1352 1320 title=group.title,
1353 1321 description=group.description,
1354 1322 conflict_handler=group.conflict_handler)
1355 1323
1356 1324 # map the actions to their new group
1357 1325 for action in group._group_actions:
1358 1326 group_map[action] = title_group_map[group.title]
1359 1327
1360 1328 # add container's mutually exclusive groups
1361 1329 # NOTE: if add_mutually_exclusive_group ever gains title= and
1362 1330 # description= then this code will need to be expanded as above
1363 1331 for group in container._mutually_exclusive_groups:
1364 1332 mutex_group = self.add_mutually_exclusive_group(
1365 1333 required=group.required)
1366 1334
1367 1335 # map the actions to their new mutex group
1368 1336 for action in group._group_actions:
1369 1337 group_map[action] = mutex_group
1370 1338
1371 1339 # add all actions to this container or their group
1372 1340 for action in container._actions:
1373 1341 group_map.get(action, self)._add_action(action)
1374 1342
1375 1343 def _get_positional_kwargs(self, dest, **kwargs):
1376 1344 # make sure required is not specified
1377 1345 if 'required' in kwargs:
1378 1346 msg = _("'required' is an invalid argument for positionals")
1379 1347 raise TypeError(msg)
1380 1348
1381 1349 # mark positional arguments as required if at least one is
1382 1350 # always required
1383 1351 if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
1384 1352 kwargs['required'] = True
1385 1353 if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
1386 1354 kwargs['required'] = True
1387 1355
1388 1356 # return the keyword arguments with no option strings
1389 1357 return dict(kwargs, dest=dest, option_strings=[])
1390 1358
1391 1359 def _get_optional_kwargs(self, *args, **kwargs):
1392 1360 # determine short and long option strings
1393 1361 option_strings = []
1394 1362 long_option_strings = []
1395 1363 for option_string in args:
1396 # error on one-or-fewer-character option strings
1397 if len(option_string) < 2:
1398 msg = _('invalid option string %r: '
1399 'must be at least two characters long')
1400 raise ValueError(msg % option_string)
1401
1402 1364 # error on strings that don't start with an appropriate prefix
1403 1365 if not option_string[0] in self.prefix_chars:
1404 1366 msg = _('invalid option string %r: '
1405 1367 'must start with a character %r')
1406 1368 tup = option_string, self.prefix_chars
1407 1369 raise ValueError(msg % tup)
1408 1370
1409 # error on strings that are all prefix characters
1410 if not (_set(option_string) - _set(self.prefix_chars)):
1411 msg = _('invalid option string %r: '
1412 'must contain characters other than %r')
1413 tup = option_string, self.prefix_chars
1414 raise ValueError(msg % tup)
1415
1416 1371 # strings starting with two prefix characters are long options
1417 1372 option_strings.append(option_string)
1418 1373 if option_string[0] in self.prefix_chars:
1419 if option_string[1] in self.prefix_chars:
1420 long_option_strings.append(option_string)
1374 if len(option_string) > 1:
1375 if option_string[1] in self.prefix_chars:
1376 long_option_strings.append(option_string)
1421 1377
1422 1378 # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
1423 1379 dest = kwargs.pop('dest', None)
1424 1380 if dest is None:
1425 1381 if long_option_strings:
1426 1382 dest_option_string = long_option_strings[0]
1427 1383 else:
1428 1384 dest_option_string = option_strings[0]
1429 1385 dest = dest_option_string.lstrip(self.prefix_chars)
1386 if not dest:
1387 msg = _('dest= is required for options like %r')
1388 raise ValueError(msg % option_string)
1430 1389 dest = dest.replace('-', '_')
1431 1390
1432 1391 # return the updated keyword arguments
1433 1392 return dict(kwargs, dest=dest, option_strings=option_strings)
1434 1393
1435 1394 def _pop_action_class(self, kwargs, default=None):
1436 1395 action = kwargs.pop('action', default)
1437 1396 return self._registry_get('action', action, action)
1438 1397
1439 1398 def _get_handler(self):
1440 1399 # determine function from conflict handler string
1441 1400 handler_func_name = '_handle_conflict_%s' % self.conflict_handler
1442 1401 try:
1443 1402 return getattr(self, handler_func_name)
1444 1403 except AttributeError:
1445 1404 msg = _('invalid conflict_resolution value: %r')
1446 1405 raise ValueError(msg % self.conflict_handler)
1447 1406
1448 1407 def _check_conflict(self, action):
1449 1408
1450 1409 # find all options that conflict with this option
1451 1410 confl_optionals = []
1452 1411 for option_string in action.option_strings:
1453 1412 if option_string in self._option_string_actions:
1454 1413 confl_optional = self._option_string_actions[option_string]
1455 1414 confl_optionals.append((option_string, confl_optional))
1456 1415
1457 1416 # resolve any conflicts
1458 1417 if confl_optionals:
1459 1418 conflict_handler = self._get_handler()
1460 1419 conflict_handler(action, confl_optionals)
1461 1420
1462 1421 def _handle_conflict_error(self, action, conflicting_actions):
1463 1422 message = _('conflicting option string(s): %s')
1464 1423 conflict_string = ', '.join([option_string
1465 1424 for option_string, action
1466 1425 in conflicting_actions])
1467 1426 raise ArgumentError(action, message % conflict_string)
1468 1427
1469 1428 def _handle_conflict_resolve(self, action, conflicting_actions):
1470 1429
1471 1430 # remove all conflicting options
1472 1431 for option_string, action in conflicting_actions:
1473 1432
1474 1433 # remove the conflicting option
1475 1434 action.option_strings.remove(option_string)
1476 1435 self._option_string_actions.pop(option_string, None)
1477 1436
1478 1437 # if the option now has no option string, remove it from the
1479 1438 # container holding it
1480 1439 if not action.option_strings:
1481 1440 action.container._remove_action(action)
1482 1441
1483 1442
1484 1443 class _ArgumentGroup(_ActionsContainer):
1485 1444
1486 1445 def __init__(self, container, title=None, description=None, **kwargs):
1487 1446 # add any missing keyword arguments by checking the container
1488 1447 update = kwargs.setdefault
1489 1448 update('conflict_handler', container.conflict_handler)
1490 1449 update('prefix_chars', container.prefix_chars)
1491 1450 update('argument_default', container.argument_default)
1492 1451 super_init = super(_ArgumentGroup, self).__init__
1493 1452 super_init(description=description, **kwargs)
1494 1453
1495 1454 # group attributes
1496 1455 self.title = title
1497 1456 self._group_actions = []
1498 1457
1499 1458 # share most attributes with the container
1500 1459 self._registries = container._registries
1501 1460 self._actions = container._actions
1502 1461 self._option_string_actions = container._option_string_actions
1503 1462 self._defaults = container._defaults
1504 1463 self._has_negative_number_optionals = \
1505 1464 container._has_negative_number_optionals
1506 1465
1507 1466 def _add_action(self, action):
1508 1467 action = super(_ArgumentGroup, self)._add_action(action)
1509 1468 self._group_actions.append(action)
1510 1469 return action
1511 1470
1512 1471 def _remove_action(self, action):
1513 1472 super(_ArgumentGroup, self)._remove_action(action)
1514 1473 self._group_actions.remove(action)
1515 1474
1516 1475
1517 1476 class _MutuallyExclusiveGroup(_ArgumentGroup):
1518 1477
1519 1478 def __init__(self, container, required=False):
1520 1479 super(_MutuallyExclusiveGroup, self).__init__(container)
1521 1480 self.required = required
1522 1481 self._container = container
1523 1482
1524 1483 def _add_action(self, action):
1525 1484 if action.required:
1526 1485 msg = _('mutually exclusive arguments must be optional')
1527 1486 raise ValueError(msg)
1528 1487 action = self._container._add_action(action)
1529 1488 self._group_actions.append(action)
1530 1489 return action
1531 1490
1532 1491 def _remove_action(self, action):
1533 1492 self._container._remove_action(action)
1534 1493 self._group_actions.remove(action)
1535 1494
1536 1495
1537 1496 class ArgumentParser(_AttributeHolder, _ActionsContainer):
1538 1497 """Object for parsing command line strings into Python objects.
1539 1498
1540 1499 Keyword Arguments:
1541 1500 - prog -- The name of the program (default: sys.argv[0])
1542 1501 - usage -- A usage message (default: auto-generated from arguments)
1543 1502 - description -- A description of what the program does
1544 1503 - epilog -- Text following the argument descriptions
1545 - version -- Add a -v/--version option with the given version string
1546 1504 - parents -- Parsers whose arguments should be copied into this one
1547 1505 - formatter_class -- HelpFormatter class for printing help messages
1548 1506 - prefix_chars -- Characters that prefix optional arguments
1549 1507 - fromfile_prefix_chars -- Characters that prefix files containing
1550 1508 additional arguments
1551 1509 - argument_default -- The default value for all arguments
1552 1510 - conflict_handler -- String indicating how to handle conflicts
1553 1511 - add_help -- Add a -h/-help option
1554 1512 """
1555 1513
1556 1514 def __init__(self,
1557 1515 prog=None,
1558 1516 usage=None,
1559 1517 description=None,
1560 1518 epilog=None,
1561 1519 version=None,
1562 1520 parents=[],
1563 1521 formatter_class=HelpFormatter,
1564 1522 prefix_chars='-',
1565 1523 fromfile_prefix_chars=None,
1566 1524 argument_default=None,
1567 1525 conflict_handler='error',
1568 1526 add_help=True):
1569 1527
1528 if version is not None:
1529 import warnings
1530 warnings.warn(
1531 """The "version" argument to ArgumentParser is deprecated. """
1532 """Please use """
1533 """"add_argument(..., action='version', version="N", ...)" """
1534 """instead""", DeprecationWarning)
1535
1570 1536 superinit = super(ArgumentParser, self).__init__
1571 1537 superinit(description=description,
1572 1538 prefix_chars=prefix_chars,
1573 1539 argument_default=argument_default,
1574 1540 conflict_handler=conflict_handler)
1575 1541
1576 1542 # default setting for prog
1577 1543 if prog is None:
1578 1544 prog = _os.path.basename(_sys.argv[0])
1579 1545
1580 1546 self.prog = prog
1581 1547 self.usage = usage
1582 1548 self.epilog = epilog
1583 1549 self.version = version
1584 1550 self.formatter_class = formatter_class
1585 1551 self.fromfile_prefix_chars = fromfile_prefix_chars
1586 1552 self.add_help = add_help
1587 1553
1588 1554 add_group = self.add_argument_group
1589 1555 self._positionals = add_group(_('positional arguments'))
1590 1556 self._optionals = add_group(_('optional arguments'))
1591 1557 self._subparsers = None
1592 1558
1593 1559 # register types
1594 1560 def identity(string):
1595 1561 return string
1596 1562 self.register('type', None, identity)
1597 1563
1598 1564 # add help and version arguments if necessary
1599 1565 # (using explicit default to override global argument_default)
1600 1566 if self.add_help:
1601 1567 self.add_argument(
1602 1568 '-h', '--help', action='help', default=SUPPRESS,
1603 1569 help=_('show this help message and exit'))
1604 1570 if self.version:
1605 1571 self.add_argument(
1606 1572 '-v', '--version', action='version', default=SUPPRESS,
1607 1573 version=self.version,
1608 1574 help=_("show program's version number and exit"))
1609 1575
1610 1576 # add parent arguments and defaults
1611 1577 for parent in parents:
1612 1578 self._add_container_actions(parent)
1613 1579 try:
1614 1580 defaults = parent._defaults
1615 1581 except AttributeError:
1616 1582 pass
1617 1583 else:
1618 1584 self._defaults.update(defaults)
1619 1585
1620 1586 # =======================
1621 1587 # Pretty __repr__ methods
1622 1588 # =======================
1623 1589 def _get_kwargs(self):
1624 1590 names = [
1625 1591 'prog',
1626 1592 'usage',
1627 1593 'description',
1628 1594 'version',
1629 1595 'formatter_class',
1630 1596 'conflict_handler',
1631 1597 'add_help',
1632 1598 ]
1633 1599 return [(name, getattr(self, name)) for name in names]
1634 1600
1635 1601 # ==================================
1636 1602 # Optional/Positional adding methods
1637 1603 # ==================================
1638 1604 def add_subparsers(self, **kwargs):
1639 1605 if self._subparsers is not None:
1640 1606 self.error(_('cannot have multiple subparser arguments'))
1641 1607
1642 1608 # add the parser class to the arguments if it's not present
1643 1609 kwargs.setdefault('parser_class', type(self))
1644 1610
1645 1611 if 'title' in kwargs or 'description' in kwargs:
1646 1612 title = _(kwargs.pop('title', 'subcommands'))
1647 1613 description = _(kwargs.pop('description', None))
1648 1614 self._subparsers = self.add_argument_group(title, description)
1649 1615 else:
1650 1616 self._subparsers = self._positionals
1651 1617
1652 1618 # prog defaults to the usage message of this parser, skipping
1653 1619 # optional arguments and with no "usage:" prefix
1654 1620 if kwargs.get('prog') is None:
1655 1621 formatter = self._get_formatter()
1656 1622 positionals = self._get_positional_actions()
1657 1623 groups = self._mutually_exclusive_groups
1658 1624 formatter.add_usage(self.usage, positionals, groups, '')
1659 1625 kwargs['prog'] = formatter.format_help().strip()
1660 1626
1661 1627 # create the parsers action and add it to the positionals list
1662 1628 parsers_class = self._pop_action_class(kwargs, 'parsers')
1663 1629 action = parsers_class(option_strings=[], **kwargs)
1664 1630 self._subparsers._add_action(action)
1665 1631
1666 1632 # return the created parsers action
1667 1633 return action
1668 1634
1669 1635 def _add_action(self, action):
1670 1636 if action.option_strings:
1671 1637 self._optionals._add_action(action)
1672 1638 else:
1673 1639 self._positionals._add_action(action)
1674 1640 return action
1675 1641
1676 1642 def _get_optional_actions(self):
1677 1643 return [action
1678 1644 for action in self._actions
1679 1645 if action.option_strings]
1680 1646
1681 1647 def _get_positional_actions(self):
1682 1648 return [action
1683 1649 for action in self._actions
1684 1650 if not action.option_strings]
1685 1651
1686 1652 # =====================================
1687 1653 # Command line argument parsing methods
1688 1654 # =====================================
1689 1655 def parse_args(self, args=None, namespace=None):
1690 1656 args, argv = self.parse_known_args(args, namespace)
1691 1657 if argv:
1692 1658 msg = _('unrecognized arguments: %s')
1693 1659 self.error(msg % ' '.join(argv))
1694 1660 return args
1695 1661
1696 1662 def parse_known_args(self, args=None, namespace=None):
1697 1663 # args default to the system args
1698 1664 if args is None:
1699 1665 args = _sys.argv[1:]
1700 1666
1701 1667 # default Namespace built from parser defaults
1702 1668 if namespace is None:
1703 1669 namespace = Namespace()
1704 1670
1705 1671 # add any action defaults that aren't present
1706 1672 for action in self._actions:
1707 1673 if action.dest is not SUPPRESS:
1708 1674 if not hasattr(namespace, action.dest):
1709 1675 if action.default is not SUPPRESS:
1710 1676 default = action.default
1711 if isinstance(action.default, _basestring):
1677 if isinstance(action.default, basestring):
1712 1678 default = self._get_value(action, default)
1713 1679 setattr(namespace, action.dest, default)
1714 1680
1715 1681 # add any parser defaults that aren't present
1716 1682 for dest in self._defaults:
1717 1683 if not hasattr(namespace, dest):
1718 1684 setattr(namespace, dest, self._defaults[dest])
1719 1685
1720 1686 # parse the arguments and exit if there are any errors
1721 1687 try:
1722 1688 return self._parse_known_args(args, namespace)
1723 1689 except ArgumentError:
1724 1690 err = _sys.exc_info()[1]
1725 1691 self.error(str(err))
1726 1692
1727 1693 def _parse_known_args(self, arg_strings, namespace):
1728 1694 # replace arg strings that are file references
1729 1695 if self.fromfile_prefix_chars is not None:
1730 1696 arg_strings = self._read_args_from_files(arg_strings)
1731 1697
1732 1698 # map all mutually exclusive arguments to the other arguments
1733 1699 # they can't occur with
1734 1700 action_conflicts = {}
1735 1701 for mutex_group in self._mutually_exclusive_groups:
1736 1702 group_actions = mutex_group._group_actions
1737 1703 for i, mutex_action in enumerate(mutex_group._group_actions):
1738 1704 conflicts = action_conflicts.setdefault(mutex_action, [])
1739 1705 conflicts.extend(group_actions[:i])
1740 1706 conflicts.extend(group_actions[i + 1:])
1741 1707
1742 1708 # find all option indices, and determine the arg_string_pattern
1743 1709 # which has an 'O' if there is an option at an index,
1744 1710 # an 'A' if there is an argument, or a '-' if there is a '--'
1745 1711 option_string_indices = {}
1746 1712 arg_string_pattern_parts = []
1747 1713 arg_strings_iter = iter(arg_strings)
1748 1714 for i, arg_string in enumerate(arg_strings_iter):
1749 1715
1750 1716 # all args after -- are non-options
1751 1717 if arg_string == '--':
1752 1718 arg_string_pattern_parts.append('-')
1753 1719 for arg_string in arg_strings_iter:
1754 1720 arg_string_pattern_parts.append('A')
1755 1721
1756 1722 # otherwise, add the arg to the arg strings
1757 1723 # and note the index if it was an option
1758 1724 else:
1759 1725 option_tuple = self._parse_optional(arg_string)
1760 1726 if option_tuple is None:
1761 1727 pattern = 'A'
1762 1728 else:
1763 1729 option_string_indices[i] = option_tuple
1764 1730 pattern = 'O'
1765 1731 arg_string_pattern_parts.append(pattern)
1766 1732
1767 1733 # join the pieces together to form the pattern
1768 1734 arg_strings_pattern = ''.join(arg_string_pattern_parts)
1769 1735
1770 1736 # converts arg strings to the appropriate and then takes the action
1771 seen_actions = _set()
1772 seen_non_default_actions = _set()
1737 seen_actions = set()
1738 seen_non_default_actions = set()
1773 1739
1774 1740 def take_action(action, argument_strings, option_string=None):
1775 1741 seen_actions.add(action)
1776 1742 argument_values = self._get_values(action, argument_strings)
1777 1743
1778 1744 # error if this argument is not allowed with other previously
1779 1745 # seen arguments, assuming that actions that use the default
1780 1746 # value don't really count as "present"
1781 1747 if argument_values is not action.default:
1782 1748 seen_non_default_actions.add(action)
1783 1749 for conflict_action in action_conflicts.get(action, []):
1784 1750 if conflict_action in seen_non_default_actions:
1785 1751 msg = _('not allowed with argument %s')
1786 1752 action_name = _get_action_name(conflict_action)
1787 1753 raise ArgumentError(action, msg % action_name)
1788 1754
1789 1755 # take the action if we didn't receive a SUPPRESS value
1790 1756 # (e.g. from a default)
1791 1757 if argument_values is not SUPPRESS:
1792 1758 action(self, namespace, argument_values, option_string)
1793 1759
1794 1760 # function to convert arg_strings into an optional action
1795 1761 def consume_optional(start_index):
1796 1762
1797 1763 # get the optional identified at this index
1798 1764 option_tuple = option_string_indices[start_index]
1799 1765 action, option_string, explicit_arg = option_tuple
1800 1766
1801 1767 # identify additional optionals in the same arg string
1802 1768 # (e.g. -xyz is the same as -x -y -z if no args are required)
1803 1769 match_argument = self._match_argument
1804 1770 action_tuples = []
1805 1771 while True:
1806 1772
1807 1773 # if we found no optional action, skip it
1808 1774 if action is None:
1809 1775 extras.append(arg_strings[start_index])
1810 1776 return start_index + 1
1811 1777
1812 1778 # if there is an explicit argument, try to match the
1813 1779 # optional's string arguments to only this
1814 1780 if explicit_arg is not None:
1815 1781 arg_count = match_argument(action, 'A')
1816 1782
1817 1783 # if the action is a single-dash option and takes no
1818 1784 # arguments, try to parse more single-dash options out
1819 1785 # of the tail of the option string
1820 1786 chars = self.prefix_chars
1821 1787 if arg_count == 0 and option_string[1] not in chars:
1822 1788 action_tuples.append((action, [], option_string))
1823 1789 for char in self.prefix_chars:
1824 1790 option_string = char + explicit_arg[0]
1825 1791 explicit_arg = explicit_arg[1:] or None
1826 1792 optionals_map = self._option_string_actions
1827 1793 if option_string in optionals_map:
1828 1794 action = optionals_map[option_string]
1829 1795 break
1830 1796 else:
1831 1797 msg = _('ignored explicit argument %r')
1832 1798 raise ArgumentError(action, msg % explicit_arg)
1833 1799
1834 1800 # if the action expect exactly one argument, we've
1835 1801 # successfully matched the option; exit the loop
1836 1802 elif arg_count == 1:
1837 1803 stop = start_index + 1
1838 1804 args = [explicit_arg]
1839 1805 action_tuples.append((action, args, option_string))
1840 1806 break
1841 1807
1842 1808 # error if a double-dash option did not use the
1843 1809 # explicit argument
1844 1810 else:
1845 1811 msg = _('ignored explicit argument %r')
1846 1812 raise ArgumentError(action, msg % explicit_arg)
1847 1813
1848 1814 # if there is no explicit argument, try to match the
1849 1815 # optional's string arguments with the following strings
1850 1816 # if successful, exit the loop
1851 1817 else:
1852 1818 start = start_index + 1
1853 1819 selected_patterns = arg_strings_pattern[start:]
1854 1820 arg_count = match_argument(action, selected_patterns)
1855 1821 stop = start + arg_count
1856 1822 args = arg_strings[start:stop]
1857 1823 action_tuples.append((action, args, option_string))
1858 1824 break
1859 1825
1860 1826 # add the Optional to the list and return the index at which
1861 1827 # the Optional's string args stopped
1862 1828 assert action_tuples
1863 1829 for action, args, option_string in action_tuples:
1864 1830 take_action(action, args, option_string)
1865 1831 return stop
1866 1832
1867 1833 # the list of Positionals left to be parsed; this is modified
1868 1834 # by consume_positionals()
1869 1835 positionals = self._get_positional_actions()
1870 1836
1871 1837 # function to convert arg_strings into positional actions
1872 1838 def consume_positionals(start_index):
1873 1839 # match as many Positionals as possible
1874 1840 match_partial = self._match_arguments_partial
1875 1841 selected_pattern = arg_strings_pattern[start_index:]
1876 1842 arg_counts = match_partial(positionals, selected_pattern)
1877 1843
1878 1844 # slice off the appropriate arg strings for each Positional
1879 1845 # and add the Positional and its args to the list
1880 1846 for action, arg_count in zip(positionals, arg_counts):
1881 1847 args = arg_strings[start_index: start_index + arg_count]
1882 1848 start_index += arg_count
1883 1849 take_action(action, args)
1884 1850
1885 1851 # slice off the Positionals that we just parsed and return the
1886 1852 # index at which the Positionals' string args stopped
1887 1853 positionals[:] = positionals[len(arg_counts):]
1888 1854 return start_index
1889 1855
1890 1856 # consume Positionals and Optionals alternately, until we have
1891 1857 # passed the last option string
1892 1858 extras = []
1893 1859 start_index = 0
1894 1860 if option_string_indices:
1895 1861 max_option_string_index = max(option_string_indices)
1896 1862 else:
1897 1863 max_option_string_index = -1
1898 1864 while start_index <= max_option_string_index:
1899 1865
1900 1866 # consume any Positionals preceding the next option
1901 1867 next_option_string_index = min([
1902 1868 index
1903 1869 for index in option_string_indices
1904 1870 if index >= start_index])
1905 1871 if start_index != next_option_string_index:
1906 1872 positionals_end_index = consume_positionals(start_index)
1907 1873
1908 1874 # only try to parse the next optional if we didn't consume
1909 1875 # the option string during the positionals parsing
1910 1876 if positionals_end_index > start_index:
1911 1877 start_index = positionals_end_index
1912 1878 continue
1913 1879 else:
1914 1880 start_index = positionals_end_index
1915 1881
1916 1882 # if we consumed all the positionals we could and we're not
1917 1883 # at the index of an option string, there were extra arguments
1918 1884 if start_index not in option_string_indices:
1919 1885 strings = arg_strings[start_index:next_option_string_index]
1920 1886 extras.extend(strings)
1921 1887 start_index = next_option_string_index
1922 1888
1923 1889 # consume the next optional and any arguments for it
1924 1890 start_index = consume_optional(start_index)
1925 1891
1926 1892 # consume any positionals following the last Optional
1927 1893 stop_index = consume_positionals(start_index)
1928 1894
1929 1895 # if we didn't consume all the argument strings, there were extras
1930 1896 extras.extend(arg_strings[stop_index:])
1931 1897
1932 1898 # if we didn't use all the Positional objects, there were too few
1933 1899 # arg strings supplied.
1934 1900 if positionals:
1935 1901 self.error(_('too few arguments'))
1936 1902
1937 1903 # make sure all required actions were present
1938 1904 for action in self._actions:
1939 1905 if action.required:
1940 1906 if action not in seen_actions:
1941 1907 name = _get_action_name(action)
1942 1908 self.error(_('argument %s is required') % name)
1943 1909
1944 1910 # make sure all required groups had one option present
1945 1911 for group in self._mutually_exclusive_groups:
1946 1912 if group.required:
1947 1913 for action in group._group_actions:
1948 1914 if action in seen_non_default_actions:
1949 1915 break
1950 1916
1951 1917 # if no actions were used, report the error
1952 1918 else:
1953 1919 names = [_get_action_name(action)
1954 1920 for action in group._group_actions
1955 1921 if action.help is not SUPPRESS]
1956 1922 msg = _('one of the arguments %s is required')
1957 1923 self.error(msg % ' '.join(names))
1958 1924
1959 1925 # return the updated namespace and the extra arguments
1960 1926 return namespace, extras
1961 1927
1962 1928 def _read_args_from_files(self, arg_strings):
1963 1929 # expand arguments referencing files
1964 1930 new_arg_strings = []
1965 1931 for arg_string in arg_strings:
1966 1932
1967 1933 # for regular arguments, just add them back into the list
1968 1934 if arg_string[0] not in self.fromfile_prefix_chars:
1969 1935 new_arg_strings.append(arg_string)
1970 1936
1971 1937 # replace arguments referencing files with the file content
1972 1938 else:
1973 1939 try:
1974 1940 args_file = open(arg_string[1:])
1975 1941 try:
1976 arg_strings = args_file.read().splitlines()
1942 arg_strings = []
1943 for arg_line in args_file.read().splitlines():
1944 for arg in self.convert_arg_line_to_args(arg_line):
1945 arg_strings.append(arg)
1977 1946 arg_strings = self._read_args_from_files(arg_strings)
1978 1947 new_arg_strings.extend(arg_strings)
1979 1948 finally:
1980 1949 args_file.close()
1981 1950 except IOError:
1982 1951 err = _sys.exc_info()[1]
1983 1952 self.error(str(err))
1984 1953
1985 1954 # return the modified argument list
1986 1955 return new_arg_strings
1987 1956
1957 def convert_arg_line_to_args(self, arg_line):
1958 return [arg_line]
1959
1988 1960 def _match_argument(self, action, arg_strings_pattern):
1989 1961 # match the pattern for this action to the arg strings
1990 1962 nargs_pattern = self._get_nargs_pattern(action)
1991 1963 match = _re.match(nargs_pattern, arg_strings_pattern)
1992 1964
1993 1965 # raise an exception if we weren't able to find a match
1994 1966 if match is None:
1995 1967 nargs_errors = {
1996 1968 None: _('expected one argument'),
1997 1969 OPTIONAL: _('expected at most one argument'),
1998 1970 ONE_OR_MORE: _('expected at least one argument'),
1999 1971 }
2000 1972 default = _('expected %s argument(s)') % action.nargs
2001 1973 msg = nargs_errors.get(action.nargs, default)
2002 1974 raise ArgumentError(action, msg)
2003 1975
2004 1976 # return the number of arguments matched
2005 1977 return len(match.group(1))
2006 1978
2007 1979 def _match_arguments_partial(self, actions, arg_strings_pattern):
2008 1980 # progressively shorten the actions list by slicing off the
2009 1981 # final actions until we find a match
2010 1982 result = []
2011 1983 for i in range(len(actions), 0, -1):
2012 1984 actions_slice = actions[:i]
2013 1985 pattern = ''.join([self._get_nargs_pattern(action)
2014 1986 for action in actions_slice])
2015 1987 match = _re.match(pattern, arg_strings_pattern)
2016 1988 if match is not None:
2017 1989 result.extend([len(string) for string in match.groups()])
2018 1990 break
2019 1991
2020 1992 # return the list of arg string counts
2021 1993 return result
2022 1994
2023 1995 def _parse_optional(self, arg_string):
2024 1996 # if it's an empty string, it was meant to be a positional
2025 1997 if not arg_string:
2026 1998 return None
2027 1999
2028 2000 # if it doesn't start with a prefix, it was meant to be positional
2029 2001 if not arg_string[0] in self.prefix_chars:
2030 2002 return None
2031 2003
2032 # if it's just dashes, it was meant to be positional
2033 if not arg_string.strip('-'):
2034 return None
2035
2036 2004 # if the option string is present in the parser, return the action
2037 2005 if arg_string in self._option_string_actions:
2038 2006 action = self._option_string_actions[arg_string]
2039 2007 return action, arg_string, None
2040 2008
2009 # if it's just a single character, it was meant to be positional
2010 if len(arg_string) == 1:
2011 return None
2012
2041 2013 # if the option string before the "=" is present, return the action
2042 2014 if '=' in arg_string:
2043 2015 option_string, explicit_arg = arg_string.split('=', 1)
2044 2016 if option_string in self._option_string_actions:
2045 2017 action = self._option_string_actions[option_string]
2046 2018 return action, option_string, explicit_arg
2047 2019
2048 2020 # search through all possible prefixes of the option string
2049 2021 # and all actions in the parser for possible interpretations
2050 2022 option_tuples = self._get_option_tuples(arg_string)
2051 2023
2052 2024 # if multiple actions match, the option string was ambiguous
2053 2025 if len(option_tuples) > 1:
2054 2026 options = ', '.join([option_string
2055 2027 for action, option_string, explicit_arg in option_tuples])
2056 2028 tup = arg_string, options
2057 2029 self.error(_('ambiguous option: %s could match %s') % tup)
2058 2030
2059 2031 # if exactly one action matched, this segmentation is good,
2060 2032 # so return the parsed action
2061 2033 elif len(option_tuples) == 1:
2062 2034 option_tuple, = option_tuples
2063 2035 return option_tuple
2064 2036
2065 2037 # if it was not found as an option, but it looks like a negative
2066 2038 # number, it was meant to be positional
2067 2039 # unless there are negative-number-like options
2068 2040 if self._negative_number_matcher.match(arg_string):
2069 2041 if not self._has_negative_number_optionals:
2070 2042 return None
2071 2043
2072 2044 # if it contains a space, it was meant to be a positional
2073 2045 if ' ' in arg_string:
2074 2046 return None
2075 2047
2076 2048 # it was meant to be an optional but there is no such option
2077 2049 # in this parser (though it might be a valid option in a subparser)
2078 2050 return None, arg_string, None
2079 2051
2080 2052 def _get_option_tuples(self, option_string):
2081 2053 result = []
2082 2054
2083 2055 # option strings starting with two prefix characters are only
2084 2056 # split at the '='
2085 2057 chars = self.prefix_chars
2086 2058 if option_string[0] in chars and option_string[1] in chars:
2087 2059 if '=' in option_string:
2088 2060 option_prefix, explicit_arg = option_string.split('=', 1)
2089 2061 else:
2090 2062 option_prefix = option_string
2091 2063 explicit_arg = None
2092 2064 for option_string in self._option_string_actions:
2093 2065 if option_string.startswith(option_prefix):
2094 2066 action = self._option_string_actions[option_string]
2095 2067 tup = action, option_string, explicit_arg
2096 2068 result.append(tup)
2097 2069
2098 2070 # single character options can be concatenated with their arguments
2099 2071 # but multiple character options always have to have their argument
2100 2072 # separate
2101 2073 elif option_string[0] in chars and option_string[1] not in chars:
2102 2074 option_prefix = option_string
2103 2075 explicit_arg = None
2104 2076 short_option_prefix = option_string[:2]
2105 2077 short_explicit_arg = option_string[2:]
2106 2078
2107 2079 for option_string in self._option_string_actions:
2108 2080 if option_string == short_option_prefix:
2109 2081 action = self._option_string_actions[option_string]
2110 2082 tup = action, option_string, short_explicit_arg
2111 2083 result.append(tup)
2112 2084 elif option_string.startswith(option_prefix):
2113 2085 action = self._option_string_actions[option_string]
2114 2086 tup = action, option_string, explicit_arg
2115 2087 result.append(tup)
2116 2088
2117 2089 # shouldn't ever get here
2118 2090 else:
2119 2091 self.error(_('unexpected option string: %s') % option_string)
2120 2092
2121 2093 # return the collected option tuples
2122 2094 return result
2123 2095
2124 2096 def _get_nargs_pattern(self, action):
2125 2097 # in all examples below, we have to allow for '--' args
2126 2098 # which are represented as '-' in the pattern
2127 2099 nargs = action.nargs
2128 2100
2129 2101 # the default (None) is assumed to be a single argument
2130 2102 if nargs is None:
2131 2103 nargs_pattern = '(-*A-*)'
2132 2104
2133 2105 # allow zero or one arguments
2134 2106 elif nargs == OPTIONAL:
2135 2107 nargs_pattern = '(-*A?-*)'
2136 2108
2137 2109 # allow zero or more arguments
2138 2110 elif nargs == ZERO_OR_MORE:
2139 2111 nargs_pattern = '(-*[A-]*)'
2140 2112
2141 2113 # allow one or more arguments
2142 2114 elif nargs == ONE_OR_MORE:
2143 2115 nargs_pattern = '(-*A[A-]*)'
2144 2116
2145 2117 # allow any number of options or arguments
2146 2118 elif nargs == REMAINDER:
2147 2119 nargs_pattern = '([-AO]*)'
2148 2120
2149 2121 # allow one argument followed by any number of options or arguments
2150 2122 elif nargs == PARSER:
2151 2123 nargs_pattern = '(-*A[-AO]*)'
2152 2124
2153 2125 # all others should be integers
2154 2126 else:
2155 2127 nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs)
2156 2128
2157 2129 # if this is an optional action, -- is not allowed
2158 2130 if action.option_strings:
2159 2131 nargs_pattern = nargs_pattern.replace('-*', '')
2160 2132 nargs_pattern = nargs_pattern.replace('-', '')
2161 2133
2162 2134 # return the pattern
2163 2135 return nargs_pattern
2164 2136
2165 2137 # ========================
2166 2138 # Value conversion methods
2167 2139 # ========================
2168 2140 def _get_values(self, action, arg_strings):
2169 2141 # for everything but PARSER args, strip out '--'
2170 2142 if action.nargs not in [PARSER, REMAINDER]:
2171 2143 arg_strings = [s for s in arg_strings if s != '--']
2172 2144
2173 2145 # optional argument produces a default when not present
2174 2146 if not arg_strings and action.nargs == OPTIONAL:
2175 2147 if action.option_strings:
2176 2148 value = action.const
2177 2149 else:
2178 2150 value = action.default
2179 if isinstance(value, _basestring):
2151 if isinstance(value, basestring):
2180 2152 value = self._get_value(action, value)
2181 2153 self._check_value(action, value)
2182 2154
2183 2155 # when nargs='*' on a positional, if there were no command-line
2184 2156 # args, use the default if it is anything other than None
2185 2157 elif (not arg_strings and action.nargs == ZERO_OR_MORE and
2186 2158 not action.option_strings):
2187 2159 if action.default is not None:
2188 2160 value = action.default
2189 2161 else:
2190 2162 value = arg_strings
2191 2163 self._check_value(action, value)
2192 2164
2193 2165 # single argument or optional argument produces a single value
2194 2166 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2195 2167 arg_string, = arg_strings
2196 2168 value = self._get_value(action, arg_string)
2197 2169 self._check_value(action, value)
2198 2170
2199 2171 # REMAINDER arguments convert all values, checking none
2200 2172 elif action.nargs == REMAINDER:
2201 2173 value = [self._get_value(action, v) for v in arg_strings]
2202 2174
2203 2175 # PARSER arguments convert all values, but check only the first
2204 2176 elif action.nargs == PARSER:
2205 2177 value = [self._get_value(action, v) for v in arg_strings]
2206 2178 self._check_value(action, value[0])
2207 2179
2208 2180 # all other types of nargs produce a list
2209 2181 else:
2210 2182 value = [self._get_value(action, v) for v in arg_strings]
2211 2183 for v in value:
2212 2184 self._check_value(action, v)
2213 2185
2214 2186 # return the converted value
2215 2187 return value
2216 2188
2217 2189 def _get_value(self, action, arg_string):
2218 2190 type_func = self._registry_get('type', action.type, action.type)
2219 2191 if not _callable(type_func):
2220 2192 msg = _('%r is not callable')
2221 2193 raise ArgumentError(action, msg % type_func)
2222 2194
2223 2195 # convert the value to the appropriate type
2224 2196 try:
2225 2197 result = type_func(arg_string)
2226 2198
2227 2199 # ArgumentTypeErrors indicate errors
2228 2200 except ArgumentTypeError:
2229 2201 name = getattr(action.type, '__name__', repr(action.type))
2230 2202 msg = str(_sys.exc_info()[1])
2231 2203 raise ArgumentError(action, msg)
2232 2204
2233 2205 # TypeErrors or ValueErrors also indicate errors
2234 2206 except (TypeError, ValueError):
2235 2207 name = getattr(action.type, '__name__', repr(action.type))
2236 2208 msg = _('invalid %s value: %r')
2237 2209 raise ArgumentError(action, msg % (name, arg_string))
2238 2210
2239 2211 # return the converted value
2240 2212 return result
2241 2213
2242 2214 def _check_value(self, action, value):
2243 2215 # converted value must be one of the choices (if specified)
2244 2216 if action.choices is not None and value not in action.choices:
2245 2217 tup = value, ', '.join(map(repr, action.choices))
2246 2218 msg = _('invalid choice: %r (choose from %s)') % tup
2247 2219 raise ArgumentError(action, msg)
2248 2220
2249 2221 # =======================
2250 2222 # Help-formatting methods
2251 2223 # =======================
2252 2224 def format_usage(self):
2253 2225 formatter = self._get_formatter()
2254 2226 formatter.add_usage(self.usage, self._actions,
2255 2227 self._mutually_exclusive_groups)
2256 2228 return formatter.format_help()
2257 2229
2258 2230 def format_help(self):
2259 2231 formatter = self._get_formatter()
2260 2232
2261 2233 # usage
2262 2234 formatter.add_usage(self.usage, self._actions,
2263 2235 self._mutually_exclusive_groups)
2264 2236
2265 2237 # description
2266 2238 formatter.add_text(self.description)
2267 2239
2268 2240 # positionals, optionals and user-defined groups
2269 2241 for action_group in self._action_groups:
2270 2242 formatter.start_section(action_group.title)
2271 2243 formatter.add_text(action_group.description)
2272 2244 formatter.add_arguments(action_group._group_actions)
2273 2245 formatter.end_section()
2274 2246
2275 2247 # epilog
2276 2248 formatter.add_text(self.epilog)
2277 2249
2278 2250 # determine help from format above
2279 2251 return formatter.format_help()
2280 2252
2281 2253 def format_version(self):
2254 import warnings
2255 warnings.warn(
2256 'The format_version method is deprecated -- the "version" '
2257 'argument to ArgumentParser is no longer supported.',
2258 DeprecationWarning)
2282 2259 formatter = self._get_formatter()
2283 2260 formatter.add_text(self.version)
2284 2261 return formatter.format_help()
2285 2262
2286 2263 def _get_formatter(self):
2287 2264 return self.formatter_class(prog=self.prog)
2288 2265
2289 2266 # =====================
2290 2267 # Help-printing methods
2291 2268 # =====================
2292 2269 def print_usage(self, file=None):
2293 2270 if file is None:
2294 2271 file = _sys.stdout
2295 2272 self._print_message(self.format_usage(), file)
2296 2273
2297 2274 def print_help(self, file=None):
2298 2275 if file is None:
2299 2276 file = _sys.stdout
2300 2277 self._print_message(self.format_help(), file)
2301 2278
2302 2279 def print_version(self, file=None):
2280 import warnings
2281 warnings.warn(
2282 'The print_version method is deprecated -- the "version" '
2283 'argument to ArgumentParser is no longer supported.',
2284 DeprecationWarning)
2303 2285 self._print_message(self.format_version(), file)
2304 2286
2305 2287 def _print_message(self, message, file=None):
2306 2288 if message:
2307 2289 if file is None:
2308 2290 file = _sys.stderr
2309 2291 file.write(message)
2310 2292
2311 2293 # ===============
2312 2294 # Exiting methods
2313 2295 # ===============
2314 2296 def exit(self, status=0, message=None):
2315 2297 if message:
2316 2298 self._print_message(message, _sys.stderr)
2317 2299 _sys.exit(status)
2318 2300
2319 2301 def error(self, message):
2320 2302 """error(message: string)
2321 2303
2322 2304 Prints a usage message incorporating the message to stderr and
2323 2305 exits.
2324 2306
2325 2307 If you override this in a subclass, it should not return -- it
2326 2308 should either exit or raise an exception.
2327 2309 """
2328 2310 self.print_usage(_sys.stderr)
2329 2311 self.exit(2, _('%s: error: %s\n') % (self.prog, message))
1 NO CONTENT: file renamed from IPython/external/configobj.py to IPython/external/configobj/_configobj.py
1 NO CONTENT: file renamed from IPython/external/decorator.py to IPython/external/decorator/_decorator.py
1 NO CONTENT: file renamed from IPython/external/decorators.py to IPython/external/decorators/_decorators.py
1 NO CONTENT: file renamed from IPython/external/_numpy_testing_utils.py to IPython/external/decorators/_numpy_testing_utils.py
1 NO CONTENT: file renamed from IPython/external/guid.py to IPython/external/guid/_guid.py
1 NO CONTENT: file renamed from IPython/external/mglob.py to IPython/external/mglob/_mglob.py
1 NO CONTENT: file renamed from IPython/external/path.py to IPython/external/path/_path.py
1 NO CONTENT: file renamed from IPython/external/pexpect.py to IPython/external/pexpect/_pexpect.py
1 NO CONTENT: file renamed from IPython/external/pyparsing.py to IPython/external/pyparsing/_pyparsing.py
1 NO CONTENT: file renamed from IPython/external/simplegeneric.py to IPython/external/simplegeneric/_simplegeneric.py
1 NO CONTENT: file renamed from IPython/external/validate.py to IPython/external/validate/_validate.py
1 NO CONTENT: file renamed from IPython/external/pretty.py to IPython/lib/pretty.py
@@ -1,369 +1,380 b''
1 1 # encoding: utf-8
2 2 """
3 3 This module defines the things that are used in setup.py for building IPython
4 4
5 5 This includes:
6 6
7 7 * The basic arguments to setup
8 8 * Functions for finding things like packages, package data, etc.
9 9 * A function for checking dependencies.
10 10 """
11 11 from __future__ import print_function
12 12
13 13 #-------------------------------------------------------------------------------
14 14 # Copyright (C) 2008 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-------------------------------------------------------------------------------
19 19
20 20 #-------------------------------------------------------------------------------
21 21 # Imports
22 22 #-------------------------------------------------------------------------------
23 23 import os
24 24 import sys
25 25
26 26 from ConfigParser import ConfigParser
27 27 from distutils.command.build_py import build_py
28 28 from glob import glob
29 29
30 30 from setupext import install_data_ext
31 31
32 32 #-------------------------------------------------------------------------------
33 33 # Useful globals and utility functions
34 34 #-------------------------------------------------------------------------------
35 35
36 36 # A few handy globals
37 37 isfile = os.path.isfile
38 38 pjoin = os.path.join
39 39
40 40 def oscmd(s):
41 41 print(">", s)
42 42 os.system(s)
43 43
44 44 # A little utility we'll need below, since glob() does NOT allow you to do
45 45 # exclusion on multiple endings!
46 46 def file_doesnt_endwith(test,endings):
47 47 """Return true if test is a file and its name does NOT end with any
48 48 of the strings listed in endings."""
49 49 if not isfile(test):
50 50 return False
51 51 for e in endings:
52 52 if test.endswith(e):
53 53 return False
54 54 return True
55 55
56 56 #---------------------------------------------------------------------------
57 57 # Basic project information
58 58 #---------------------------------------------------------------------------
59 59
60 60 # release.py contains version, authors, license, url, keywords, etc.
61 61 execfile(pjoin('IPython','core','release.py'))
62 62
63 63 # Create a dict with the basic information
64 64 # This dict is eventually passed to setup after additional keys are added.
65 65 setup_args = dict(
66 66 name = name,
67 67 version = version,
68 68 description = description,
69 69 long_description = long_description,
70 70 author = author,
71 71 author_email = author_email,
72 72 url = url,
73 73 download_url = download_url,
74 74 license = license,
75 75 platforms = platforms,
76 76 keywords = keywords,
77 77 cmdclass = {'install_data': install_data_ext},
78 78 )
79 79
80 80
81 81 #---------------------------------------------------------------------------
82 82 # Find packages
83 83 #---------------------------------------------------------------------------
84 84
85 85 def add_package(packages,pname,config=False,tests=False,scripts=False,
86 86 others=None):
87 87 """
88 88 Add a package to the list of packages, including certain subpackages.
89 89 """
90 90 packages.append('.'.join(['IPython',pname]))
91 91 if config:
92 92 packages.append('.'.join(['IPython',pname,'config']))
93 93 if tests:
94 94 packages.append('.'.join(['IPython',pname,'tests']))
95 95 if scripts:
96 96 packages.append('.'.join(['IPython',pname,'scripts']))
97 97 if others is not None:
98 98 for o in others:
99 99 packages.append('.'.join(['IPython',pname,o]))
100 100
101 101 def find_packages():
102 102 """
103 103 Find all of IPython's packages.
104 104 """
105 105 packages = ['IPython']
106 106 add_package(packages, 'config', tests=True, others=['default','profile'])
107 107 add_package(packages, 'core', tests=True)
108 108 add_package(packages, 'deathrow', tests=True)
109 109 add_package(packages, 'extensions')
110 110 add_package(packages, 'external')
111 add_package(packages, 'external.argparse')
112 add_package(packages, 'external.configobj')
113 add_package(packages, 'external.decorator')
114 add_package(packages, 'external.decorators')
115 add_package(packages, 'external.guid')
116 add_package(packages, 'external.Itpl')
117 add_package(packages, 'external.mglob')
118 add_package(packages, 'external.path')
119 add_package(packages, 'external.pyparsing')
120 add_package(packages, 'external.simplegeneric')
121 add_package(packages, 'external.validate')
111 122 add_package(packages, 'frontend')
112 123 add_package(packages, 'frontend.qt')
113 124 add_package(packages, 'frontend.qt.console', tests=True)
114 125 add_package(packages, 'frontend.terminal', tests=True)
115 126 add_package(packages, 'kernel', config=False, tests=True, scripts=True)
116 127 add_package(packages, 'kernel.core', config=False, tests=True)
117 128 add_package(packages, 'lib', tests=True)
118 129 add_package(packages, 'quarantine', tests=True)
119 130 add_package(packages, 'scripts')
120 131 add_package(packages, 'testing', tests=True)
121 132 add_package(packages, 'testing.plugin', tests=False)
122 133 add_package(packages, 'utils', tests=True)
123 134 add_package(packages, 'zmq')
124 135 add_package(packages, 'zmq.pylab')
125 136 return packages
126 137
127 138 #---------------------------------------------------------------------------
128 139 # Find package data
129 140 #---------------------------------------------------------------------------
130 141
131 142 def find_package_data():
132 143 """
133 144 Find IPython's package_data.
134 145 """
135 146 # This is not enough for these things to appear in an sdist.
136 147 # We need to muck with the MANIFEST to get this to work
137 148 package_data = {
138 149 'IPython.config.userconfig' : ['*'],
139 150 'IPython.testing' : ['*.txt']
140 151 }
141 152 return package_data
142 153
143 154
144 155 #---------------------------------------------------------------------------
145 156 # Find data files
146 157 #---------------------------------------------------------------------------
147 158
148 159 def make_dir_struct(tag,base,out_base):
149 160 """Make the directory structure of all files below a starting dir.
150 161
151 162 This is just a convenience routine to help build a nested directory
152 163 hierarchy because distutils is too stupid to do this by itself.
153 164
154 165 XXX - this needs a proper docstring!
155 166 """
156 167
157 168 # we'll use these a lot below
158 169 lbase = len(base)
159 170 pathsep = os.path.sep
160 171 lpathsep = len(pathsep)
161 172
162 173 out = []
163 174 for (dirpath,dirnames,filenames) in os.walk(base):
164 175 # we need to strip out the dirpath from the base to map it to the
165 176 # output (installation) path. This requires possibly stripping the
166 177 # path separator, because otherwise pjoin will not work correctly
167 178 # (pjoin('foo/','/bar') returns '/bar').
168 179
169 180 dp_eff = dirpath[lbase:]
170 181 if dp_eff.startswith(pathsep):
171 182 dp_eff = dp_eff[lpathsep:]
172 183 # The output path must be anchored at the out_base marker
173 184 out_path = pjoin(out_base,dp_eff)
174 185 # Now we can generate the final filenames. Since os.walk only produces
175 186 # filenames, we must join back with the dirpath to get full valid file
176 187 # paths:
177 188 pfiles = [pjoin(dirpath,f) for f in filenames]
178 189 # Finally, generate the entry we need, which is a pari of (output
179 190 # path, files) for use as a data_files parameter in install_data.
180 191 out.append((out_path, pfiles))
181 192
182 193 return out
183 194
184 195
185 196 def find_data_files():
186 197 """
187 198 Find IPython's data_files.
188 199
189 200 Most of these are docs.
190 201 """
191 202
192 203 docdirbase = pjoin('share', 'doc', 'ipython')
193 204 manpagebase = pjoin('share', 'man', 'man1')
194 205
195 206 # Simple file lists can be made by hand
196 207 manpages = filter(isfile, glob(pjoin('docs','man','*.1.gz')))
197 208 igridhelpfiles = filter(isfile,
198 209 glob(pjoin('IPython','extensions','igrid_help.*')))
199 210
200 211 # For nested structures, use the utility above
201 212 example_files = make_dir_struct(
202 213 'data',
203 214 pjoin('docs','examples'),
204 215 pjoin(docdirbase,'examples')
205 216 )
206 217 manual_files = make_dir_struct(
207 218 'data',
208 219 pjoin('docs','dist'),
209 220 pjoin(docdirbase,'manual')
210 221 )
211 222
212 223 # And assemble the entire output list
213 224 data_files = [ (manpagebase, manpages),
214 225 (pjoin(docdirbase, 'extensions'), igridhelpfiles),
215 226 ] + manual_files + example_files
216 227
217 228 return data_files
218 229
219 230
220 231 def make_man_update_target(manpage):
221 232 """Return a target_update-compliant tuple for the given manpage.
222 233
223 234 Parameters
224 235 ----------
225 236 manpage : string
226 237 Name of the manpage, must include the section number (trailing number).
227 238
228 239 Example
229 240 -------
230 241
231 242 >>> make_man_update_target('ipython.1') #doctest: +NORMALIZE_WHITESPACE
232 243 ('docs/man/ipython.1.gz',
233 244 ['docs/man/ipython.1'],
234 245 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz')
235 246 """
236 247 man_dir = pjoin('docs', 'man')
237 248 manpage_gz = manpage + '.gz'
238 249 manpath = pjoin(man_dir, manpage)
239 250 manpath_gz = pjoin(man_dir, manpage_gz)
240 251 gz_cmd = ( "cd %(man_dir)s && gzip -9c %(manpage)s > %(manpage_gz)s" %
241 252 locals() )
242 253 return (manpath_gz, [manpath], gz_cmd)
243 254
244 255 #---------------------------------------------------------------------------
245 256 # Find scripts
246 257 #---------------------------------------------------------------------------
247 258
248 259 def find_scripts():
249 260 """
250 261 Find IPython's scripts.
251 262 """
252 263 kernel_scripts = pjoin('IPython','kernel','scripts')
253 264 main_scripts = pjoin('IPython','scripts')
254 265 scripts = [pjoin(kernel_scripts, 'ipengine'),
255 266 pjoin(kernel_scripts, 'ipcontroller'),
256 267 pjoin(kernel_scripts, 'ipcluster'),
257 268 pjoin(main_scripts, 'ipython'),
258 269 pjoin(main_scripts, 'ipython-qtconsole'),
259 270 pjoin(main_scripts, 'pycolor'),
260 271 pjoin(main_scripts, 'irunner'),
261 272 pjoin(main_scripts, 'iptest')
262 273 ]
263 274
264 275 # Script to be run by the windows binary installer after the default setup
265 276 # routine, to add shortcuts and similar windows-only things. Windows
266 277 # post-install scripts MUST reside in the scripts/ dir, otherwise distutils
267 278 # doesn't find them.
268 279 if 'bdist_wininst' in sys.argv:
269 280 if len(sys.argv) > 2 and \
270 281 ('sdist' in sys.argv or 'bdist_rpm' in sys.argv):
271 282 print("ERROR: bdist_wininst must be run alone. Exiting.",
272 283 file=sys.stderr)
273 284 sys.exit(1)
274 285 scripts.append(pjoin('scripts','ipython_win_post_install.py'))
275 286
276 287 return scripts
277 288
278 289 #---------------------------------------------------------------------------
279 290 # Verify all dependencies
280 291 #---------------------------------------------------------------------------
281 292
282 293 def check_for_dependencies():
283 294 """Check for IPython's dependencies.
284 295
285 296 This function should NOT be called if running under setuptools!
286 297 """
287 298 from setupext.setupext import (
288 299 print_line, print_raw, print_status,
289 300 check_for_zopeinterface, check_for_twisted,
290 301 check_for_foolscap, check_for_pyopenssl,
291 302 check_for_sphinx, check_for_pygments,
292 303 check_for_nose, check_for_pexpect
293 304 )
294 305 print_line()
295 306 print_raw("BUILDING IPYTHON")
296 307 print_status('python', sys.version)
297 308 print_status('platform', sys.platform)
298 309 if sys.platform == 'win32':
299 310 print_status('Windows version', sys.getwindowsversion())
300 311
301 312 print_raw("")
302 313 print_raw("OPTIONAL DEPENDENCIES")
303 314
304 315 check_for_zopeinterface()
305 316 check_for_twisted()
306 317 check_for_foolscap()
307 318 check_for_pyopenssl()
308 319 check_for_sphinx()
309 320 check_for_pygments()
310 321 check_for_nose()
311 322 check_for_pexpect()
312 323
313 324
314 325 def record_commit_info(pkg_dir, build_cmd=build_py):
315 326 """ Return extended build command class for recording commit
316 327
317 328 The extended command tries to run git to find the current commit, getting
318 329 the empty string if it fails. It then writes the commit hash into a file
319 330 in the `pkg_dir` path, named ``.git_commit_info.ini``.
320 331
321 332 In due course this information can be used by the package after it is
322 333 installed, to tell you what commit it was installed from if known.
323 334
324 335 To make use of this system, you need a package with a .git_commit_info.ini
325 336 file - e.g. ``myproject/.git_commit_info.ini`` - that might well look like
326 337 this::
327 338
328 339 # This is an ini file that may contain information about the code state
329 340 [commit hash]
330 341 # The line below may contain a valid hash if it has been substituted
331 342 # during 'git archive'
332 343 archive_subst_hash=$Format:%h$
333 344 # This line may be modified by the install process
334 345 install_hash=
335 346
336 347 The .git_commit_info file above is also designed to be used with git
337 348 substitution - so you probably also want a ``.gitattributes`` file in the
338 349 root directory of your working tree that contains something like this::
339 350
340 351 myproject/.git_commit_info.ini export-subst
341 352
342 353 That will cause the ``.git_commit_info.ini`` file to get filled in by ``git
343 354 archive`` - useful in case someone makes such an archive - for example with
344 355 via the github 'download source' button.
345 356
346 357 Although all the above will work as is, you might consider having something
347 358 like a ``get_info()`` function in your package to display the commit
348 359 information at the terminal. See the ``pkg_info.py`` module in the nipy
349 360 package for an example.
350 361 """
351 362 class MyBuildPy(build_cmd):
352 363 ''' Subclass to write commit data into installation tree '''
353 364 def run(self):
354 365 build_py.run(self)
355 366 import subprocess
356 367 proc = subprocess.Popen('git rev-parse --short HEAD',
357 368 stdout=subprocess.PIPE,
358 369 stderr=subprocess.PIPE,
359 370 shell=True)
360 371 repo_commit, _ = proc.communicate()
361 372 # We write the installation commit even if it's empty
362 373 cfg_parser = ConfigParser()
363 374 cfg_parser.read(pjoin(pkg_dir, '.git_commit_info.ini'))
364 375 cfg_parser.set('commit hash', 'install_hash', repo_commit)
365 376 out_pth = pjoin(self.build_lib, pkg_dir, '.git_commit_info.ini')
366 377 out_file = open(out_pth, 'wt')
367 378 cfg_parser.write(out_file)
368 379 out_file.close()
369 380 return MyBuildPy
General Comments 0
You need to be logged in to leave comments. Login now