##// END OF EJS Templates
Make an error message a bit easier to understand.
Fernando Perez -
Show More
@@ -1,1892 +1,1892 b''
1 1 # -*- coding: utf-8 -*-
2 2 """General purpose utilities.
3 3
4 4 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 5 these things are also convenient when working at the command line.
6 6 """
7 7
8 8 #*****************************************************************************
9 9 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #*****************************************************************************
14 14
15 15 #****************************************************************************
16 16 # required modules from the Python standard library
17 17 import __main__
18 18
19 19 import os
20 20 import platform
21 21 import re
22 22 import shlex
23 23 import shutil
24 24 import subprocess
25 25 import sys
26 26 import time
27 27 import types
28 28 import warnings
29 29
30 30 # Curses and termios are Unix-only modules
31 31 try:
32 32 import curses
33 33 # We need termios as well, so if its import happens to raise, we bail on
34 34 # using curses altogether.
35 35 import termios
36 36 except ImportError:
37 37 USE_CURSES = False
38 38 else:
39 39 # Curses on Solaris may not be complete, so we can't use it there
40 40 USE_CURSES = hasattr(curses,'initscr')
41 41
42 42 # Other IPython utilities
43 43 import IPython
44 44 from IPython.external.Itpl import itpl,printpl
45 45 from IPython.utils import platutils
46 46 from IPython.utils.generics import result_display
47 47 from IPython.external.path import path
48 48
49 49
50 50 #****************************************************************************
51 51 # Exceptions
52 52 class Error(Exception):
53 53 """Base class for exceptions in this module."""
54 54 pass
55 55
56 56 #----------------------------------------------------------------------------
57 57 class IOStream:
58 58 def __init__(self,stream,fallback):
59 59 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
60 60 stream = fallback
61 61 self.stream = stream
62 62 self._swrite = stream.write
63 63 self.flush = stream.flush
64 64
65 65 def write(self,data):
66 66 try:
67 67 self._swrite(data)
68 68 except:
69 69 try:
70 70 # print handles some unicode issues which may trip a plain
71 71 # write() call. Attempt to emulate write() by using a
72 72 # trailing comma
73 73 print >> self.stream, data,
74 74 except:
75 75 # if we get here, something is seriously broken.
76 76 print >> sys.stderr, \
77 77 'ERROR - failed to write data to stream:', self.stream
78 78
79 79 def writeln(self, data):
80 80 self.write(data)
81 81 self.write('\n')
82 82
83 83 def close(self):
84 84 pass
85 85
86 86
87 87 class IOTerm:
88 88 """ Term holds the file or file-like objects for handling I/O operations.
89 89
90 90 These are normally just sys.stdin, sys.stdout and sys.stderr but for
91 91 Windows they can can replaced to allow editing the strings before they are
92 92 displayed."""
93 93
94 94 # In the future, having IPython channel all its I/O operations through
95 95 # this class will make it easier to embed it into other environments which
96 96 # are not a normal terminal (such as a GUI-based shell)
97 97 def __init__(self,cin=None,cout=None,cerr=None):
98 98 self.cin = IOStream(cin,sys.stdin)
99 99 self.cout = IOStream(cout,sys.stdout)
100 100 self.cerr = IOStream(cerr,sys.stderr)
101 101
102 102 # Global variable to be used for all I/O
103 103 Term = IOTerm()
104 104
105 105 import IPython.utils.rlineimpl as readline
106 106 # Remake Term to use the readline i/o facilities
107 107 if sys.platform == 'win32' and readline.have_readline:
108 108
109 109 Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile)
110 110
111 111
112 112 class Tee(object):
113 113 """A class to duplicate an output stream to stdout/err.
114 114
115 115 This works in a manner very similar to the Unix 'tee' command.
116 116
117 117 When the object is closed or deleted, it closes the original file given to
118 118 it for duplication.
119 119 """
120 120 # Inspired by:
121 121 # http://mail.python.org/pipermail/python-list/2007-May/442737.html
122 122
123 123 def __init__(self, file, mode=None, channel='stdout'):
124 124 """Construct a new Tee object.
125 125
126 126 Parameters
127 127 ----------
128 128 file : filename or open filehandle (writable)
129 129 File that will be duplicated
130 130
131 131 mode : optional, valid mode for open().
132 132 If a filename was give, open with this mode.
133 133
134 134 channel : str, one of ['stdout', 'stderr']
135 135 """
136 136 if channel not in ['stdout', 'stderr']:
137 137 raise ValueError('Invalid channel spec %s' % channel)
138 138
139 139 if hasattr(file, 'write') and hasattr(file, 'seek'):
140 140 self.file = file
141 141 else:
142 142 self.file = open(name, mode)
143 143 self.channel = channel
144 144 self.ostream = getattr(sys, channel)
145 145 setattr(sys, channel, self)
146 146 self._closed = False
147 147
148 148 def close(self):
149 149 """Close the file and restore the channel."""
150 150 self.flush()
151 151 setattr(sys, self.channel, self.ostream)
152 152 self.file.close()
153 153 self._closed = True
154 154
155 155 def write(self, data):
156 156 """Write data to both channels."""
157 157 self.file.write(data)
158 158 self.ostream.write(data)
159 159 self.ostream.flush()
160 160
161 161 def flush(self):
162 162 """Flush both channels."""
163 163 self.file.flush()
164 164 self.ostream.flush()
165 165
166 166 def __del__(self):
167 167 if not self._closed:
168 168 self.close()
169 169
170 170
171 171 #****************************************************************************
172 172 # Generic warning/error printer, used by everything else
173 173 def warn(msg,level=2,exit_val=1):
174 174 """Standard warning printer. Gives formatting consistency.
175 175
176 176 Output is sent to Term.cerr (sys.stderr by default).
177 177
178 178 Options:
179 179
180 180 -level(2): allows finer control:
181 181 0 -> Do nothing, dummy function.
182 182 1 -> Print message.
183 183 2 -> Print 'WARNING:' + message. (Default level).
184 184 3 -> Print 'ERROR:' + message.
185 185 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
186 186
187 187 -exit_val (1): exit value returned by sys.exit() for a level 4
188 188 warning. Ignored for all other levels."""
189 189
190 190 if level>0:
191 191 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
192 192 print >> Term.cerr, '%s%s' % (header[level],msg)
193 193 if level == 4:
194 194 print >> Term.cerr,'Exiting.\n'
195 195 sys.exit(exit_val)
196 196
197 197 def info(msg):
198 198 """Equivalent to warn(msg,level=1)."""
199 199
200 200 warn(msg,level=1)
201 201
202 202 def error(msg):
203 203 """Equivalent to warn(msg,level=3)."""
204 204
205 205 warn(msg,level=3)
206 206
207 207 def fatal(msg,exit_val=1):
208 208 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
209 209
210 210 warn(msg,exit_val=exit_val,level=4)
211 211
212 212 #---------------------------------------------------------------------------
213 213 # Debugging routines
214 214 #
215 215 def debugx(expr,pre_msg=''):
216 216 """Print the value of an expression from the caller's frame.
217 217
218 218 Takes an expression, evaluates it in the caller's frame and prints both
219 219 the given expression and the resulting value (as well as a debug mark
220 220 indicating the name of the calling function. The input must be of a form
221 221 suitable for eval().
222 222
223 223 An optional message can be passed, which will be prepended to the printed
224 224 expr->value pair."""
225 225
226 226 cf = sys._getframe(1)
227 227 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
228 228 eval(expr,cf.f_globals,cf.f_locals))
229 229
230 230 # deactivate it by uncommenting the following line, which makes it a no-op
231 231 #def debugx(expr,pre_msg=''): pass
232 232
233 233 #----------------------------------------------------------------------------
234 234 StringTypes = types.StringTypes
235 235
236 236 # Basic timing functionality
237 237
238 238 # If possible (Unix), use the resource module instead of time.clock()
239 239 try:
240 240 import resource
241 241 def clocku():
242 242 """clocku() -> floating point number
243 243
244 244 Return the *USER* CPU time in seconds since the start of the process.
245 245 This is done via a call to resource.getrusage, so it avoids the
246 246 wraparound problems in time.clock()."""
247 247
248 248 return resource.getrusage(resource.RUSAGE_SELF)[0]
249 249
250 250 def clocks():
251 251 """clocks() -> floating point number
252 252
253 253 Return the *SYSTEM* CPU time in seconds since the start of the process.
254 254 This is done via a call to resource.getrusage, so it avoids the
255 255 wraparound problems in time.clock()."""
256 256
257 257 return resource.getrusage(resource.RUSAGE_SELF)[1]
258 258
259 259 def clock():
260 260 """clock() -> floating point number
261 261
262 262 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
263 263 the process. This is done via a call to resource.getrusage, so it
264 264 avoids the wraparound problems in time.clock()."""
265 265
266 266 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
267 267 return u+s
268 268
269 269 def clock2():
270 270 """clock2() -> (t_user,t_system)
271 271
272 272 Similar to clock(), but return a tuple of user/system times."""
273 273 return resource.getrusage(resource.RUSAGE_SELF)[:2]
274 274
275 275 except ImportError:
276 276 # There is no distinction of user/system time under windows, so we just use
277 277 # time.clock() for everything...
278 278 clocku = clocks = clock = time.clock
279 279 def clock2():
280 280 """Under windows, system CPU time can't be measured.
281 281
282 282 This just returns clock() and zero."""
283 283 return time.clock(),0.0
284 284
285 285 def timings_out(reps,func,*args,**kw):
286 286 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
287 287
288 288 Execute a function reps times, return a tuple with the elapsed total
289 289 CPU time in seconds, the time per call and the function's output.
290 290
291 291 Under Unix, the return value is the sum of user+system time consumed by
292 292 the process, computed via the resource module. This prevents problems
293 293 related to the wraparound effect which the time.clock() function has.
294 294
295 295 Under Windows the return value is in wall clock seconds. See the
296 296 documentation for the time module for more details."""
297 297
298 298 reps = int(reps)
299 299 assert reps >=1, 'reps must be >= 1'
300 300 if reps==1:
301 301 start = clock()
302 302 out = func(*args,**kw)
303 303 tot_time = clock()-start
304 304 else:
305 305 rng = xrange(reps-1) # the last time is executed separately to store output
306 306 start = clock()
307 307 for dummy in rng: func(*args,**kw)
308 308 out = func(*args,**kw) # one last time
309 309 tot_time = clock()-start
310 310 av_time = tot_time / reps
311 311 return tot_time,av_time,out
312 312
313 313 def timings(reps,func,*args,**kw):
314 314 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
315 315
316 316 Execute a function reps times, return a tuple with the elapsed total CPU
317 317 time in seconds and the time per call. These are just the first two values
318 318 in timings_out()."""
319 319
320 320 return timings_out(reps,func,*args,**kw)[0:2]
321 321
322 322 def timing(func,*args,**kw):
323 323 """timing(func,*args,**kw) -> t_total
324 324
325 325 Execute a function once, return the elapsed total CPU time in
326 326 seconds. This is just the first value in timings_out()."""
327 327
328 328 return timings_out(1,func,*args,**kw)[0]
329 329
330 330 #****************************************************************************
331 331 # file and system
332 332
333 333 def arg_split(s,posix=False):
334 334 """Split a command line's arguments in a shell-like manner.
335 335
336 336 This is a modified version of the standard library's shlex.split()
337 337 function, but with a default of posix=False for splitting, so that quotes
338 338 in inputs are respected."""
339 339
340 340 # XXX - there may be unicode-related problems here!!! I'm not sure that
341 341 # shlex is truly unicode-safe, so it might be necessary to do
342 342 #
343 343 # s = s.encode(sys.stdin.encoding)
344 344 #
345 345 # first, to ensure that shlex gets a normal string. Input from anyone who
346 346 # knows more about unicode and shlex than I would be good to have here...
347 347 lex = shlex.shlex(s, posix=posix)
348 348 lex.whitespace_split = True
349 349 return list(lex)
350 350
351 351 def system(cmd,verbose=0,debug=0,header=''):
352 352 """Execute a system command, return its exit status.
353 353
354 354 Options:
355 355
356 356 - verbose (0): print the command to be executed.
357 357
358 358 - debug (0): only print, do not actually execute.
359 359
360 360 - header (''): Header to print on screen prior to the executed command (it
361 361 is only prepended to the command, no newlines are added).
362 362
363 363 Note: a stateful version of this function is available through the
364 364 SystemExec class."""
365 365
366 366 stat = 0
367 367 if verbose or debug: print header+cmd
368 368 sys.stdout.flush()
369 369 if not debug: stat = os.system(cmd)
370 370 return stat
371 371
372 372 def abbrev_cwd():
373 373 """ Return abbreviated version of cwd, e.g. d:mydir """
374 374 cwd = os.getcwd().replace('\\','/')
375 375 drivepart = ''
376 376 tail = cwd
377 377 if sys.platform == 'win32':
378 378 if len(cwd) < 4:
379 379 return cwd
380 380 drivepart,tail = os.path.splitdrive(cwd)
381 381
382 382
383 383 parts = tail.split('/')
384 384 if len(parts) > 2:
385 385 tail = '/'.join(parts[-2:])
386 386
387 387 return (drivepart + (
388 388 cwd == '/' and '/' or tail))
389 389
390 390
391 391 # This function is used by ipython in a lot of places to make system calls.
392 392 # We need it to be slightly different under win32, due to the vagaries of
393 393 # 'network shares'. A win32 override is below.
394 394
395 395 def shell(cmd,verbose=0,debug=0,header=''):
396 396 """Execute a command in the system shell, always return None.
397 397
398 398 Options:
399 399
400 400 - verbose (0): print the command to be executed.
401 401
402 402 - debug (0): only print, do not actually execute.
403 403
404 404 - header (''): Header to print on screen prior to the executed command (it
405 405 is only prepended to the command, no newlines are added).
406 406
407 407 Note: this is similar to genutils.system(), but it returns None so it can
408 408 be conveniently used in interactive loops without getting the return value
409 409 (typically 0) printed many times."""
410 410
411 411 stat = 0
412 412 if verbose or debug: print header+cmd
413 413 # flush stdout so we don't mangle python's buffering
414 414 sys.stdout.flush()
415 415
416 416 if not debug:
417 417 platutils.set_term_title("IPy " + cmd)
418 418 os.system(cmd)
419 419 platutils.set_term_title("IPy " + abbrev_cwd())
420 420
421 421 # override shell() for win32 to deal with network shares
422 422 if os.name in ('nt','dos'):
423 423
424 424 shell_ori = shell
425 425
426 426 def shell(cmd,verbose=0,debug=0,header=''):
427 427 if os.getcwd().startswith(r"\\"):
428 428 path = os.getcwd()
429 429 # change to c drive (cannot be on UNC-share when issuing os.system,
430 430 # as cmd.exe cannot handle UNC addresses)
431 431 os.chdir("c:")
432 432 # issue pushd to the UNC-share and then run the command
433 433 try:
434 434 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
435 435 finally:
436 436 os.chdir(path)
437 437 else:
438 438 shell_ori(cmd,verbose,debug,header)
439 439
440 440 shell.__doc__ = shell_ori.__doc__
441 441
442 442 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
443 443 """Dummy substitute for perl's backquotes.
444 444
445 445 Executes a command and returns the output.
446 446
447 447 Accepts the same arguments as system(), plus:
448 448
449 449 - split(0): if true, the output is returned as a list split on newlines.
450 450
451 451 Note: a stateful version of this function is available through the
452 452 SystemExec class.
453 453
454 454 This is pretty much deprecated and rarely used,
455 455 genutils.getoutputerror may be what you need.
456 456
457 457 """
458 458
459 459 if verbose or debug: print header+cmd
460 460 if not debug:
461 461 pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
462 462 output = pipe.read()
463 463 # stipping last \n is here for backwards compat.
464 464 if output.endswith('\n'):
465 465 output = output[:-1]
466 466 if split:
467 467 return output.split('\n')
468 468 else:
469 469 return output
470 470
471 471 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
472 472 """Return (standard output,standard error) of executing cmd in a shell.
473 473
474 474 Accepts the same arguments as system(), plus:
475 475
476 476 - split(0): if true, each of stdout/err is returned as a list split on
477 477 newlines.
478 478
479 479 Note: a stateful version of this function is available through the
480 480 SystemExec class."""
481 481
482 482 if verbose or debug: print header+cmd
483 483 if not cmd:
484 484 if split:
485 485 return [],[]
486 486 else:
487 487 return '',''
488 488 if not debug:
489 489 p = subprocess.Popen(cmd, shell=True,
490 490 stdin=subprocess.PIPE,
491 491 stdout=subprocess.PIPE,
492 492 stderr=subprocess.PIPE,
493 493 close_fds=True)
494 494 pin, pout, perr = (p.stdin, p.stdout, p.stderr)
495 495
496 496 tout = pout.read().rstrip()
497 497 terr = perr.read().rstrip()
498 498 pin.close()
499 499 pout.close()
500 500 perr.close()
501 501 if split:
502 502 return tout.split('\n'),terr.split('\n')
503 503 else:
504 504 return tout,terr
505 505
506 506 # for compatibility with older naming conventions
507 507 xsys = system
508 508 bq = getoutput
509 509
510 510 class SystemExec:
511 511 """Access the system and getoutput functions through a stateful interface.
512 512
513 513 Note: here we refer to the system and getoutput functions from this
514 514 library, not the ones from the standard python library.
515 515
516 516 This class offers the system and getoutput functions as methods, but the
517 517 verbose, debug and header parameters can be set for the instance (at
518 518 creation time or later) so that they don't need to be specified on each
519 519 call.
520 520
521 521 For efficiency reasons, there's no way to override the parameters on a
522 522 per-call basis other than by setting instance attributes. If you need
523 523 local overrides, it's best to directly call system() or getoutput().
524 524
525 525 The following names are provided as alternate options:
526 526 - xsys: alias to system
527 527 - bq: alias to getoutput
528 528
529 529 An instance can then be created as:
530 530 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
531 531 """
532 532
533 533 def __init__(self,verbose=0,debug=0,header='',split=0):
534 534 """Specify the instance's values for verbose, debug and header."""
535 535 setattr_list(self,'verbose debug header split')
536 536
537 537 def system(self,cmd):
538 538 """Stateful interface to system(), with the same keyword parameters."""
539 539
540 540 system(cmd,self.verbose,self.debug,self.header)
541 541
542 542 def shell(self,cmd):
543 543 """Stateful interface to shell(), with the same keyword parameters."""
544 544
545 545 shell(cmd,self.verbose,self.debug,self.header)
546 546
547 547 xsys = system # alias
548 548
549 549 def getoutput(self,cmd):
550 550 """Stateful interface to getoutput()."""
551 551
552 552 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
553 553
554 554 def getoutputerror(self,cmd):
555 555 """Stateful interface to getoutputerror()."""
556 556
557 557 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
558 558
559 559 bq = getoutput # alias
560 560
561 561 #-----------------------------------------------------------------------------
562 562 def mutex_opts(dict,ex_op):
563 563 """Check for presence of mutually exclusive keys in a dict.
564 564
565 565 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
566 566 for op1,op2 in ex_op:
567 567 if op1 in dict and op2 in dict:
568 568 raise ValueError,'\n*** ERROR in Arguments *** '\
569 569 'Options '+op1+' and '+op2+' are mutually exclusive.'
570 570
571 571 #-----------------------------------------------------------------------------
572 572 def get_py_filename(name):
573 573 """Return a valid python filename in the current directory.
574 574
575 575 If the given name is not a file, it adds '.py' and searches again.
576 576 Raises IOError with an informative message if the file isn't found."""
577 577
578 578 name = os.path.expanduser(name)
579 579 if not os.path.isfile(name) and not name.endswith('.py'):
580 580 name += '.py'
581 581 if os.path.isfile(name):
582 582 return name
583 583 else:
584 584 raise IOError,'File `%s` not found.' % name
585 585
586 586 #-----------------------------------------------------------------------------
587 587
588 588
589 589 def filefind(filename, path_dirs=None):
590 590 """Find a file by looking through a sequence of paths.
591 591
592 592 This iterates through a sequence of paths looking for a file and returns
593 593 the full, absolute path of the first occurence of the file. If no set of
594 594 path dirs is given, the filename is tested as is, after running through
595 595 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
596 596
597 597 filefind('myfile.txt')
598 598
599 599 will find the file in the current working dir, but::
600 600
601 601 filefind('~/myfile.txt')
602 602
603 603 Will find the file in the users home directory. This function does not
604 604 automatically try any paths, such as the cwd or the user's home directory.
605 605
606 606 Parameters
607 607 ----------
608 608 filename : str
609 609 The filename to look for.
610 610 path_dirs : str, None or sequence of str
611 611 The sequence of paths to look for the file in. If None, the filename
612 612 need to be absolute or be in the cwd. If a string, the string is
613 613 put into a sequence and the searched. If a sequence, walk through
614 614 each element and join with ``filename``, calling :func:`expandvars`
615 615 and :func:`expanduser` before testing for existence.
616 616
617 617 Returns
618 618 -------
619 619 Raises :exc:`IOError` or returns absolute path to file.
620 620 """
621 621
622 622 # If paths are quoted, abspath gets confused, strip them...
623 623 filename = filename.strip('"').strip("'")
624 624 # If the input is an absolute path, just check it exists
625 625 if os.path.isabs(filename) and os.path.isfile(filename):
626 626 return filename
627 627
628 628 if path_dirs is None:
629 629 path_dirs = ("",)
630 630 elif isinstance(path_dirs, basestring):
631 631 path_dirs = (path_dirs,)
632 632
633 633 for path in path_dirs:
634 634 if path == '.': path = os.getcwd()
635 635 testname = expand_path(os.path.join(path, filename))
636 636 if os.path.isfile(testname):
637 637 return os.path.abspath(testname)
638 raise IOError("File does not exist in any "
639 "of the search paths: %r, %r" % \
638
639 raise IOError("File %r does not exist in any of the search paths: %r" %
640 640 (filename, path_dirs))
641 641
642 642
643 643 #----------------------------------------------------------------------------
644 644 def file_read(filename):
645 645 """Read a file and close it. Returns the file source."""
646 646 fobj = open(filename,'r');
647 647 source = fobj.read();
648 648 fobj.close()
649 649 return source
650 650
651 651 def file_readlines(filename):
652 652 """Read a file and close it. Returns the file source using readlines()."""
653 653 fobj = open(filename,'r');
654 654 lines = fobj.readlines();
655 655 fobj.close()
656 656 return lines
657 657
658 658 #----------------------------------------------------------------------------
659 659 def target_outdated(target,deps):
660 660 """Determine whether a target is out of date.
661 661
662 662 target_outdated(target,deps) -> 1/0
663 663
664 664 deps: list of filenames which MUST exist.
665 665 target: single filename which may or may not exist.
666 666
667 667 If target doesn't exist or is older than any file listed in deps, return
668 668 true, otherwise return false.
669 669 """
670 670 try:
671 671 target_time = os.path.getmtime(target)
672 672 except os.error:
673 673 return 1
674 674 for dep in deps:
675 675 dep_time = os.path.getmtime(dep)
676 676 if dep_time > target_time:
677 677 #print "For target",target,"Dep failed:",dep # dbg
678 678 #print "times (dep,tar):",dep_time,target_time # dbg
679 679 return 1
680 680 return 0
681 681
682 682 #-----------------------------------------------------------------------------
683 683 def target_update(target,deps,cmd):
684 684 """Update a target with a given command given a list of dependencies.
685 685
686 686 target_update(target,deps,cmd) -> runs cmd if target is outdated.
687 687
688 688 This is just a wrapper around target_outdated() which calls the given
689 689 command if target is outdated."""
690 690
691 691 if target_outdated(target,deps):
692 692 xsys(cmd)
693 693
694 694 #----------------------------------------------------------------------------
695 695 def unquote_ends(istr):
696 696 """Remove a single pair of quotes from the endpoints of a string."""
697 697
698 698 if not istr:
699 699 return istr
700 700 if (istr[0]=="'" and istr[-1]=="'") or \
701 701 (istr[0]=='"' and istr[-1]=='"'):
702 702 return istr[1:-1]
703 703 else:
704 704 return istr
705 705
706 706 #----------------------------------------------------------------------------
707 707 def flag_calls(func):
708 708 """Wrap a function to detect and flag when it gets called.
709 709
710 710 This is a decorator which takes a function and wraps it in a function with
711 711 a 'called' attribute. wrapper.called is initialized to False.
712 712
713 713 The wrapper.called attribute is set to False right before each call to the
714 714 wrapped function, so if the call fails it remains False. After the call
715 715 completes, wrapper.called is set to True and the output is returned.
716 716
717 717 Testing for truth in wrapper.called allows you to determine if a call to
718 718 func() was attempted and succeeded."""
719 719
720 720 def wrapper(*args,**kw):
721 721 wrapper.called = False
722 722 out = func(*args,**kw)
723 723 wrapper.called = True
724 724 return out
725 725
726 726 wrapper.called = False
727 727 wrapper.__doc__ = func.__doc__
728 728 return wrapper
729 729
730 730 #----------------------------------------------------------------------------
731 731 def dhook_wrap(func,*a,**k):
732 732 """Wrap a function call in a sys.displayhook controller.
733 733
734 734 Returns a wrapper around func which calls func, with all its arguments and
735 735 keywords unmodified, using the default sys.displayhook. Since IPython
736 736 modifies sys.displayhook, it breaks the behavior of certain systems that
737 737 rely on the default behavior, notably doctest.
738 738 """
739 739
740 740 def f(*a,**k):
741 741
742 742 dhook_s = sys.displayhook
743 743 sys.displayhook = sys.__displayhook__
744 744 try:
745 745 out = func(*a,**k)
746 746 finally:
747 747 sys.displayhook = dhook_s
748 748
749 749 return out
750 750
751 751 f.__doc__ = func.__doc__
752 752 return f
753 753
754 754 #----------------------------------------------------------------------------
755 755 def doctest_reload():
756 756 """Properly reload doctest to reuse it interactively.
757 757
758 758 This routine:
759 759
760 760 - imports doctest but does NOT reload it (see below).
761 761
762 762 - resets its global 'master' attribute to None, so that multiple uses of
763 763 the module interactively don't produce cumulative reports.
764 764
765 765 - Monkeypatches its core test runner method to protect it from IPython's
766 766 modified displayhook. Doctest expects the default displayhook behavior
767 767 deep down, so our modification breaks it completely. For this reason, a
768 768 hard monkeypatch seems like a reasonable solution rather than asking
769 769 users to manually use a different doctest runner when under IPython.
770 770
771 771 Notes
772 772 -----
773 773
774 774 This function *used to* reload doctest, but this has been disabled because
775 775 reloading doctest unconditionally can cause massive breakage of other
776 776 doctest-dependent modules already in memory, such as those for IPython's
777 777 own testing system. The name wasn't changed to avoid breaking people's
778 778 code, but the reload call isn't actually made anymore."""
779 779
780 780 import doctest
781 781 doctest.master = None
782 782 doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run)
783 783
784 784 #----------------------------------------------------------------------------
785 785 class HomeDirError(Error):
786 786 pass
787 787
788 788 def get_home_dir():
789 789 """Return the closest possible equivalent to a 'home' directory.
790 790
791 791 * On POSIX, we try $HOME.
792 792 * On Windows we try:
793 793 - %HOME%: rare, but some people with unix-like setups may have defined it
794 794 - %HOMESHARE%
795 795 - %HOMEDRIVE\%HOMEPATH%
796 796 - %USERPROFILE%
797 797 - Registry hack
798 798 * On Dos C:\
799 799
800 800 Currently only Posix and NT are implemented, a HomeDirError exception is
801 801 raised for all other OSes.
802 802 """
803 803
804 804 isdir = os.path.isdir
805 805 env = os.environ
806 806
807 807 # first, check py2exe distribution root directory for _ipython.
808 808 # This overrides all. Normally does not exist.
809 809
810 810 if hasattr(sys, "frozen"): #Is frozen by py2exe
811 811 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
812 812 root, rest = IPython.__file__.lower().split('library.zip')
813 813 else:
814 814 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
815 815 root=os.path.abspath(root).rstrip('\\')
816 816 if isdir(os.path.join(root, '_ipython')):
817 817 os.environ["IPYKITROOT"] = root
818 818 return root.decode(sys.getfilesystemencoding())
819 819
820 820 if os.name == 'posix':
821 821 # Linux, Unix, AIX, OS X
822 822 try:
823 823 homedir = env['HOME']
824 824 except KeyError:
825 825 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
826 826 else:
827 827 return homedir.decode(sys.getfilesystemencoding())
828 828 elif os.name == 'nt':
829 829 # Now for win9x, XP, Vista, 7?
830 830 # For some strange reason all of these return 'nt' for os.name.
831 831 # First look for a network home directory. This will return the UNC
832 832 # path (\\server\\Users\%username%) not the mapped path (Z:\). This
833 833 # is needed when running IPython on cluster where all paths have to
834 834 # be UNC.
835 835 try:
836 836 # A user with a lot of unix tools in win32 may have defined $HOME,
837 837 # honor it if it exists, but otherwise let the more typical
838 838 # %HOMESHARE% variable be used.
839 839 homedir = env.get('HOME')
840 840 if homedir is None:
841 841 homedir = env['HOMESHARE']
842 842 except KeyError:
843 843 pass
844 844 else:
845 845 if isdir(homedir):
846 846 return homedir.decode(sys.getfilesystemencoding())
847 847
848 848 # Now look for a local home directory
849 849 try:
850 850 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
851 851 except KeyError:
852 852 pass
853 853 else:
854 854 if isdir(homedir):
855 855 return homedir.decode(sys.getfilesystemencoding())
856 856
857 857 # Now the users profile directory
858 858 try:
859 859 homedir = os.path.join(env['USERPROFILE'])
860 860 except KeyError:
861 861 pass
862 862 else:
863 863 if isdir(homedir):
864 864 return homedir.decode(sys.getfilesystemencoding())
865 865
866 866 # Use the registry to get the 'My Documents' folder.
867 867 try:
868 868 import _winreg as wreg
869 869 key = wreg.OpenKey(
870 870 wreg.HKEY_CURRENT_USER,
871 871 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
872 872 )
873 873 homedir = wreg.QueryValueEx(key,'Personal')[0]
874 874 key.Close()
875 875 except:
876 876 pass
877 877 else:
878 878 if isdir(homedir):
879 879 return homedir.decode(sys.getfilesystemencoding())
880 880
881 881 # If all else fails, raise HomeDirError
882 882 raise HomeDirError('No valid home directory could be found')
883 883 elif os.name == 'dos':
884 884 # Desperate, may do absurd things in classic MacOS. May work under DOS.
885 885 return 'C:\\'.decode(sys.getfilesystemencoding())
886 886 else:
887 887 raise HomeDirError('No valid home directory could be found for your OS')
888 888
889 889
890 890 def get_ipython_dir():
891 891 """Get the IPython directory for this platform and user.
892 892
893 893 This uses the logic in `get_home_dir` to find the home directory
894 894 and the adds .ipython to the end of the path.
895 895 """
896 896 ipdir_def = '.ipython'
897 897 home_dir = get_home_dir()
898 898 #import pdb; pdb.set_trace() # dbg
899 899 ipdir = os.environ.get(
900 900 'IPYTHON_DIR', os.environ.get(
901 901 'IPYTHONDIR', os.path.join(home_dir, ipdir_def)
902 902 )
903 903 )
904 904 return ipdir.decode(sys.getfilesystemencoding())
905 905
906 906
907 907 def get_ipython_package_dir():
908 908 """Get the base directory where IPython itself is installed."""
909 909 ipdir = os.path.dirname(IPython.__file__)
910 910 return ipdir.decode(sys.getfilesystemencoding())
911 911
912 912
913 913 #****************************************************************************
914 914 # strings and text
915 915
916 916 class LSString(str):
917 917 """String derivative with a special access attributes.
918 918
919 919 These are normal strings, but with the special attributes:
920 920
921 921 .l (or .list) : value as list (split on newlines).
922 922 .n (or .nlstr): original value (the string itself).
923 923 .s (or .spstr): value as whitespace-separated string.
924 924 .p (or .paths): list of path objects
925 925
926 926 Any values which require transformations are computed only once and
927 927 cached.
928 928
929 929 Such strings are very useful to efficiently interact with the shell, which
930 930 typically only understands whitespace-separated options for commands."""
931 931
932 932 def get_list(self):
933 933 try:
934 934 return self.__list
935 935 except AttributeError:
936 936 self.__list = self.split('\n')
937 937 return self.__list
938 938
939 939 l = list = property(get_list)
940 940
941 941 def get_spstr(self):
942 942 try:
943 943 return self.__spstr
944 944 except AttributeError:
945 945 self.__spstr = self.replace('\n',' ')
946 946 return self.__spstr
947 947
948 948 s = spstr = property(get_spstr)
949 949
950 950 def get_nlstr(self):
951 951 return self
952 952
953 953 n = nlstr = property(get_nlstr)
954 954
955 955 def get_paths(self):
956 956 try:
957 957 return self.__paths
958 958 except AttributeError:
959 959 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
960 960 return self.__paths
961 961
962 962 p = paths = property(get_paths)
963 963
964 964 def print_lsstring(arg):
965 965 """ Prettier (non-repr-like) and more informative printer for LSString """
966 966 print "LSString (.p, .n, .l, .s available). Value:"
967 967 print arg
968 968
969 969 print_lsstring = result_display.when_type(LSString)(print_lsstring)
970 970
971 971 #----------------------------------------------------------------------------
972 972 class SList(list):
973 973 """List derivative with a special access attributes.
974 974
975 975 These are normal lists, but with the special attributes:
976 976
977 977 .l (or .list) : value as list (the list itself).
978 978 .n (or .nlstr): value as a string, joined on newlines.
979 979 .s (or .spstr): value as a string, joined on spaces.
980 980 .p (or .paths): list of path objects
981 981
982 982 Any values which require transformations are computed only once and
983 983 cached."""
984 984
985 985 def get_list(self):
986 986 return self
987 987
988 988 l = list = property(get_list)
989 989
990 990 def get_spstr(self):
991 991 try:
992 992 return self.__spstr
993 993 except AttributeError:
994 994 self.__spstr = ' '.join(self)
995 995 return self.__spstr
996 996
997 997 s = spstr = property(get_spstr)
998 998
999 999 def get_nlstr(self):
1000 1000 try:
1001 1001 return self.__nlstr
1002 1002 except AttributeError:
1003 1003 self.__nlstr = '\n'.join(self)
1004 1004 return self.__nlstr
1005 1005
1006 1006 n = nlstr = property(get_nlstr)
1007 1007
1008 1008 def get_paths(self):
1009 1009 try:
1010 1010 return self.__paths
1011 1011 except AttributeError:
1012 1012 self.__paths = [path(p) for p in self if os.path.exists(p)]
1013 1013 return self.__paths
1014 1014
1015 1015 p = paths = property(get_paths)
1016 1016
1017 1017 def grep(self, pattern, prune = False, field = None):
1018 1018 """ Return all strings matching 'pattern' (a regex or callable)
1019 1019
1020 1020 This is case-insensitive. If prune is true, return all items
1021 1021 NOT matching the pattern.
1022 1022
1023 1023 If field is specified, the match must occur in the specified
1024 1024 whitespace-separated field.
1025 1025
1026 1026 Examples::
1027 1027
1028 1028 a.grep( lambda x: x.startswith('C') )
1029 1029 a.grep('Cha.*log', prune=1)
1030 1030 a.grep('chm', field=-1)
1031 1031 """
1032 1032
1033 1033 def match_target(s):
1034 1034 if field is None:
1035 1035 return s
1036 1036 parts = s.split()
1037 1037 try:
1038 1038 tgt = parts[field]
1039 1039 return tgt
1040 1040 except IndexError:
1041 1041 return ""
1042 1042
1043 1043 if isinstance(pattern, basestring):
1044 1044 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
1045 1045 else:
1046 1046 pred = pattern
1047 1047 if not prune:
1048 1048 return SList([el for el in self if pred(match_target(el))])
1049 1049 else:
1050 1050 return SList([el for el in self if not pred(match_target(el))])
1051 1051 def fields(self, *fields):
1052 1052 """ Collect whitespace-separated fields from string list
1053 1053
1054 1054 Allows quick awk-like usage of string lists.
1055 1055
1056 1056 Example data (in var a, created by 'a = !ls -l')::
1057 1057 -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
1058 1058 drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
1059 1059
1060 1060 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
1061 1061 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
1062 1062 (note the joining by space).
1063 1063 a.fields(-1) is ['ChangeLog', 'IPython']
1064 1064
1065 1065 IndexErrors are ignored.
1066 1066
1067 1067 Without args, fields() just split()'s the strings.
1068 1068 """
1069 1069 if len(fields) == 0:
1070 1070 return [el.split() for el in self]
1071 1071
1072 1072 res = SList()
1073 1073 for el in [f.split() for f in self]:
1074 1074 lineparts = []
1075 1075
1076 1076 for fd in fields:
1077 1077 try:
1078 1078 lineparts.append(el[fd])
1079 1079 except IndexError:
1080 1080 pass
1081 1081 if lineparts:
1082 1082 res.append(" ".join(lineparts))
1083 1083
1084 1084 return res
1085 1085 def sort(self,field= None, nums = False):
1086 1086 """ sort by specified fields (see fields())
1087 1087
1088 1088 Example::
1089 1089 a.sort(1, nums = True)
1090 1090
1091 1091 Sorts a by second field, in numerical order (so that 21 > 3)
1092 1092
1093 1093 """
1094 1094
1095 1095 #decorate, sort, undecorate
1096 1096 if field is not None:
1097 1097 dsu = [[SList([line]).fields(field), line] for line in self]
1098 1098 else:
1099 1099 dsu = [[line, line] for line in self]
1100 1100 if nums:
1101 1101 for i in range(len(dsu)):
1102 1102 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
1103 1103 try:
1104 1104 n = int(numstr)
1105 1105 except ValueError:
1106 1106 n = 0;
1107 1107 dsu[i][0] = n
1108 1108
1109 1109
1110 1110 dsu.sort()
1111 1111 return SList([t[1] for t in dsu])
1112 1112
1113 1113 def print_slist(arg):
1114 1114 """ Prettier (non-repr-like) and more informative printer for SList """
1115 1115 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
1116 1116 if hasattr(arg, 'hideonce') and arg.hideonce:
1117 1117 arg.hideonce = False
1118 1118 return
1119 1119
1120 1120 nlprint(arg)
1121 1121
1122 1122 print_slist = result_display.when_type(SList)(print_slist)
1123 1123
1124 1124
1125 1125
1126 1126 #----------------------------------------------------------------------------
1127 1127 def esc_quotes(strng):
1128 1128 """Return the input string with single and double quotes escaped out"""
1129 1129
1130 1130 return strng.replace('"','\\"').replace("'","\\'")
1131 1131
1132 1132 #----------------------------------------------------------------------------
1133 1133 def make_quoted_expr(s):
1134 1134 """Return string s in appropriate quotes, using raw string if possible.
1135 1135
1136 1136 XXX - example removed because it caused encoding errors in documentation
1137 1137 generation. We need a new example that doesn't contain invalid chars.
1138 1138
1139 1139 Note the use of raw string and padding at the end to allow trailing
1140 1140 backslash.
1141 1141 """
1142 1142
1143 1143 tail = ''
1144 1144 tailpadding = ''
1145 1145 raw = ''
1146 1146 if "\\" in s:
1147 1147 raw = 'r'
1148 1148 if s.endswith('\\'):
1149 1149 tail = '[:-1]'
1150 1150 tailpadding = '_'
1151 1151 if '"' not in s:
1152 1152 quote = '"'
1153 1153 elif "'" not in s:
1154 1154 quote = "'"
1155 1155 elif '"""' not in s and not s.endswith('"'):
1156 1156 quote = '"""'
1157 1157 elif "'''" not in s and not s.endswith("'"):
1158 1158 quote = "'''"
1159 1159 else:
1160 1160 # give up, backslash-escaped string will do
1161 1161 return '"%s"' % esc_quotes(s)
1162 1162 res = raw + quote + s + tailpadding + quote + tail
1163 1163 return res
1164 1164
1165 1165
1166 1166 #----------------------------------------------------------------------------
1167 1167 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
1168 1168 """Take multiple lines of input.
1169 1169
1170 1170 A list with each line of input as a separate element is returned when a
1171 1171 termination string is entered (defaults to a single '.'). Input can also
1172 1172 terminate via EOF (^D in Unix, ^Z-RET in Windows).
1173 1173
1174 1174 Lines of input which end in \\ are joined into single entries (and a
1175 1175 secondary continuation prompt is issued as long as the user terminates
1176 1176 lines with \\). This allows entering very long strings which are still
1177 1177 meant to be treated as single entities.
1178 1178 """
1179 1179
1180 1180 try:
1181 1181 if header:
1182 1182 header += '\n'
1183 1183 lines = [raw_input(header + ps1)]
1184 1184 except EOFError:
1185 1185 return []
1186 1186 terminate = [terminate_str]
1187 1187 try:
1188 1188 while lines[-1:] != terminate:
1189 1189 new_line = raw_input(ps1)
1190 1190 while new_line.endswith('\\'):
1191 1191 new_line = new_line[:-1] + raw_input(ps2)
1192 1192 lines.append(new_line)
1193 1193
1194 1194 return lines[:-1] # don't return the termination command
1195 1195 except EOFError:
1196 1196 print
1197 1197 return lines
1198 1198
1199 1199 #----------------------------------------------------------------------------
1200 1200 def raw_input_ext(prompt='', ps2='... '):
1201 1201 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
1202 1202
1203 1203 line = raw_input(prompt)
1204 1204 while line.endswith('\\'):
1205 1205 line = line[:-1] + raw_input(ps2)
1206 1206 return line
1207 1207
1208 1208 #----------------------------------------------------------------------------
1209 1209 def ask_yes_no(prompt,default=None):
1210 1210 """Asks a question and returns a boolean (y/n) answer.
1211 1211
1212 1212 If default is given (one of 'y','n'), it is used if the user input is
1213 1213 empty. Otherwise the question is repeated until an answer is given.
1214 1214
1215 1215 An EOF is treated as the default answer. If there is no default, an
1216 1216 exception is raised to prevent infinite loops.
1217 1217
1218 1218 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1219 1219
1220 1220 answers = {'y':True,'n':False,'yes':True,'no':False}
1221 1221 ans = None
1222 1222 while ans not in answers.keys():
1223 1223 try:
1224 1224 ans = raw_input(prompt+' ').lower()
1225 1225 if not ans: # response was an empty string
1226 1226 ans = default
1227 1227 except KeyboardInterrupt:
1228 1228 pass
1229 1229 except EOFError:
1230 1230 if default in answers.keys():
1231 1231 ans = default
1232 1232 print
1233 1233 else:
1234 1234 raise
1235 1235
1236 1236 return answers[ans]
1237 1237
1238 1238 #----------------------------------------------------------------------------
1239 1239 class EvalDict:
1240 1240 """
1241 1241 Emulate a dict which evaluates its contents in the caller's frame.
1242 1242
1243 1243 Usage:
1244 1244 >>> number = 19
1245 1245
1246 1246 >>> text = "python"
1247 1247
1248 1248 >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1249 1249 Python 2.1 rules!
1250 1250 """
1251 1251
1252 1252 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1253 1253 # modified (shorter) version of:
1254 1254 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1255 1255 # Skip Montanaro (skip@pobox.com).
1256 1256
1257 1257 def __getitem__(self, name):
1258 1258 frame = sys._getframe(1)
1259 1259 return eval(name, frame.f_globals, frame.f_locals)
1260 1260
1261 1261 EvalString = EvalDict # for backwards compatibility
1262 1262 #----------------------------------------------------------------------------
1263 1263 def qw(words,flat=0,sep=None,maxsplit=-1):
1264 1264 """Similar to Perl's qw() operator, but with some more options.
1265 1265
1266 1266 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1267 1267
1268 1268 words can also be a list itself, and with flat=1, the output will be
1269 1269 recursively flattened.
1270 1270
1271 1271 Examples:
1272 1272
1273 1273 >>> qw('1 2')
1274 1274 ['1', '2']
1275 1275
1276 1276 >>> qw(['a b','1 2',['m n','p q']])
1277 1277 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1278 1278
1279 1279 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1280 1280 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
1281 1281 """
1282 1282
1283 1283 if type(words) in StringTypes:
1284 1284 return [word.strip() for word in words.split(sep,maxsplit)
1285 1285 if word and not word.isspace() ]
1286 1286 if flat:
1287 1287 return flatten(map(qw,words,[1]*len(words)))
1288 1288 return map(qw,words)
1289 1289
1290 1290 #----------------------------------------------------------------------------
1291 1291 def qwflat(words,sep=None,maxsplit=-1):
1292 1292 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1293 1293 return qw(words,1,sep,maxsplit)
1294 1294
1295 1295 #----------------------------------------------------------------------------
1296 1296 def qw_lol(indata):
1297 1297 """qw_lol('a b') -> [['a','b']],
1298 1298 otherwise it's just a call to qw().
1299 1299
1300 1300 We need this to make sure the modules_some keys *always* end up as a
1301 1301 list of lists."""
1302 1302
1303 1303 if type(indata) in StringTypes:
1304 1304 return [qw(indata)]
1305 1305 else:
1306 1306 return qw(indata)
1307 1307
1308 1308 #----------------------------------------------------------------------------
1309 1309 def grep(pat,list,case=1):
1310 1310 """Simple minded grep-like function.
1311 1311 grep(pat,list) returns occurrences of pat in list, None on failure.
1312 1312
1313 1313 It only does simple string matching, with no support for regexps. Use the
1314 1314 option case=0 for case-insensitive matching."""
1315 1315
1316 1316 # This is pretty crude. At least it should implement copying only references
1317 1317 # to the original data in case it's big. Now it copies the data for output.
1318 1318 out=[]
1319 1319 if case:
1320 1320 for term in list:
1321 1321 if term.find(pat)>-1: out.append(term)
1322 1322 else:
1323 1323 lpat=pat.lower()
1324 1324 for term in list:
1325 1325 if term.lower().find(lpat)>-1: out.append(term)
1326 1326
1327 1327 if len(out): return out
1328 1328 else: return None
1329 1329
1330 1330 #----------------------------------------------------------------------------
1331 1331 def dgrep(pat,*opts):
1332 1332 """Return grep() on dir()+dir(__builtins__).
1333 1333
1334 1334 A very common use of grep() when working interactively."""
1335 1335
1336 1336 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1337 1337
1338 1338 #----------------------------------------------------------------------------
1339 1339 def idgrep(pat):
1340 1340 """Case-insensitive dgrep()"""
1341 1341
1342 1342 return dgrep(pat,0)
1343 1343
1344 1344 #----------------------------------------------------------------------------
1345 1345 def igrep(pat,list):
1346 1346 """Synonym for case-insensitive grep."""
1347 1347
1348 1348 return grep(pat,list,case=0)
1349 1349
1350 1350 #----------------------------------------------------------------------------
1351 1351 def indent(str,nspaces=4,ntabs=0):
1352 1352 """Indent a string a given number of spaces or tabstops.
1353 1353
1354 1354 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1355 1355 """
1356 1356 if str is None:
1357 1357 return
1358 1358 ind = '\t'*ntabs+' '*nspaces
1359 1359 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1360 1360 if outstr.endswith(os.linesep+ind):
1361 1361 return outstr[:-len(ind)]
1362 1362 else:
1363 1363 return outstr
1364 1364
1365 1365 #-----------------------------------------------------------------------------
1366 1366 def native_line_ends(filename,backup=1):
1367 1367 """Convert (in-place) a file to line-ends native to the current OS.
1368 1368
1369 1369 If the optional backup argument is given as false, no backup of the
1370 1370 original file is left. """
1371 1371
1372 1372 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1373 1373
1374 1374 bak_filename = filename + backup_suffixes[os.name]
1375 1375
1376 1376 original = open(filename).read()
1377 1377 shutil.copy2(filename,bak_filename)
1378 1378 try:
1379 1379 new = open(filename,'wb')
1380 1380 new.write(os.linesep.join(original.splitlines()))
1381 1381 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1382 1382 new.close()
1383 1383 except:
1384 1384 os.rename(bak_filename,filename)
1385 1385 if not backup:
1386 1386 try:
1387 1387 os.remove(bak_filename)
1388 1388 except:
1389 1389 pass
1390 1390
1391 1391 #****************************************************************************
1392 1392 # lists, dicts and structures
1393 1393
1394 1394 def belong(candidates,checklist):
1395 1395 """Check whether a list of items appear in a given list of options.
1396 1396
1397 1397 Returns a list of 1 and 0, one for each candidate given."""
1398 1398
1399 1399 return [x in checklist for x in candidates]
1400 1400
1401 1401 #----------------------------------------------------------------------------
1402 1402 def uniq_stable(elems):
1403 1403 """uniq_stable(elems) -> list
1404 1404
1405 1405 Return from an iterable, a list of all the unique elements in the input,
1406 1406 but maintaining the order in which they first appear.
1407 1407
1408 1408 A naive solution to this problem which just makes a dictionary with the
1409 1409 elements as keys fails to respect the stability condition, since
1410 1410 dictionaries are unsorted by nature.
1411 1411
1412 1412 Note: All elements in the input must be valid dictionary keys for this
1413 1413 routine to work, as it internally uses a dictionary for efficiency
1414 1414 reasons."""
1415 1415
1416 1416 unique = []
1417 1417 unique_dict = {}
1418 1418 for nn in elems:
1419 1419 if nn not in unique_dict:
1420 1420 unique.append(nn)
1421 1421 unique_dict[nn] = None
1422 1422 return unique
1423 1423
1424 1424 #----------------------------------------------------------------------------
1425 1425 class NLprinter:
1426 1426 """Print an arbitrarily nested list, indicating index numbers.
1427 1427
1428 1428 An instance of this class called nlprint is available and callable as a
1429 1429 function.
1430 1430
1431 1431 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1432 1432 and using 'sep' to separate the index from the value. """
1433 1433
1434 1434 def __init__(self):
1435 1435 self.depth = 0
1436 1436
1437 1437 def __call__(self,lst,pos='',**kw):
1438 1438 """Prints the nested list numbering levels."""
1439 1439 kw.setdefault('indent',' ')
1440 1440 kw.setdefault('sep',': ')
1441 1441 kw.setdefault('start',0)
1442 1442 kw.setdefault('stop',len(lst))
1443 1443 # we need to remove start and stop from kw so they don't propagate
1444 1444 # into a recursive call for a nested list.
1445 1445 start = kw['start']; del kw['start']
1446 1446 stop = kw['stop']; del kw['stop']
1447 1447 if self.depth == 0 and 'header' in kw.keys():
1448 1448 print kw['header']
1449 1449
1450 1450 for idx in range(start,stop):
1451 1451 elem = lst[idx]
1452 1452 if type(elem)==type([]):
1453 1453 self.depth += 1
1454 1454 self.__call__(elem,itpl('$pos$idx,'),**kw)
1455 1455 self.depth -= 1
1456 1456 else:
1457 1457 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1458 1458
1459 1459 nlprint = NLprinter()
1460 1460 #----------------------------------------------------------------------------
1461 1461 def all_belong(candidates,checklist):
1462 1462 """Check whether a list of items ALL appear in a given list of options.
1463 1463
1464 1464 Returns a single 1 or 0 value."""
1465 1465
1466 1466 return 1-(0 in [x in checklist for x in candidates])
1467 1467
1468 1468 #----------------------------------------------------------------------------
1469 1469 def sort_compare(lst1,lst2,inplace = 1):
1470 1470 """Sort and compare two lists.
1471 1471
1472 1472 By default it does it in place, thus modifying the lists. Use inplace = 0
1473 1473 to avoid that (at the cost of temporary copy creation)."""
1474 1474 if not inplace:
1475 1475 lst1 = lst1[:]
1476 1476 lst2 = lst2[:]
1477 1477 lst1.sort(); lst2.sort()
1478 1478 return lst1 == lst2
1479 1479
1480 1480 #----------------------------------------------------------------------------
1481 1481 def list2dict(lst):
1482 1482 """Takes a list of (key,value) pairs and turns it into a dict."""
1483 1483
1484 1484 dic = {}
1485 1485 for k,v in lst: dic[k] = v
1486 1486 return dic
1487 1487
1488 1488 #----------------------------------------------------------------------------
1489 1489 def list2dict2(lst,default=''):
1490 1490 """Takes a list and turns it into a dict.
1491 1491 Much slower than list2dict, but more versatile. This version can take
1492 1492 lists with sublists of arbitrary length (including sclars)."""
1493 1493
1494 1494 dic = {}
1495 1495 for elem in lst:
1496 1496 if type(elem) in (types.ListType,types.TupleType):
1497 1497 size = len(elem)
1498 1498 if size == 0:
1499 1499 pass
1500 1500 elif size == 1:
1501 1501 dic[elem] = default
1502 1502 else:
1503 1503 k,v = elem[0], elem[1:]
1504 1504 if len(v) == 1: v = v[0]
1505 1505 dic[k] = v
1506 1506 else:
1507 1507 dic[elem] = default
1508 1508 return dic
1509 1509
1510 1510 #----------------------------------------------------------------------------
1511 1511 def flatten(seq):
1512 1512 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1513 1513
1514 1514 return [x for subseq in seq for x in subseq]
1515 1515
1516 1516 #----------------------------------------------------------------------------
1517 1517 def get_slice(seq,start=0,stop=None,step=1):
1518 1518 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1519 1519 if stop == None:
1520 1520 stop = len(seq)
1521 1521 item = lambda i: seq[i]
1522 1522 return map(item,xrange(start,stop,step))
1523 1523
1524 1524 #----------------------------------------------------------------------------
1525 1525 def chop(seq,size):
1526 1526 """Chop a sequence into chunks of the given size."""
1527 1527 chunk = lambda i: seq[i:i+size]
1528 1528 return map(chunk,xrange(0,len(seq),size))
1529 1529
1530 1530 #----------------------------------------------------------------------------
1531 1531 # with is a keyword as of python 2.5, so this function is renamed to withobj
1532 1532 # from its old 'with' name.
1533 1533 def with_obj(object, **args):
1534 1534 """Set multiple attributes for an object, similar to Pascal's with.
1535 1535
1536 1536 Example:
1537 1537 with_obj(jim,
1538 1538 born = 1960,
1539 1539 haircolour = 'Brown',
1540 1540 eyecolour = 'Green')
1541 1541
1542 1542 Credit: Greg Ewing, in
1543 1543 http://mail.python.org/pipermail/python-list/2001-May/040703.html.
1544 1544
1545 1545 NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with'
1546 1546 has become a keyword for Python 2.5, so we had to rename it."""
1547 1547
1548 1548 object.__dict__.update(args)
1549 1549
1550 1550 #----------------------------------------------------------------------------
1551 1551 def setattr_list(obj,alist,nspace = None):
1552 1552 """Set a list of attributes for an object taken from a namespace.
1553 1553
1554 1554 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1555 1555 alist with their values taken from nspace, which must be a dict (something
1556 1556 like locals() will often do) If nspace isn't given, locals() of the
1557 1557 *caller* is used, so in most cases you can omit it.
1558 1558
1559 1559 Note that alist can be given as a string, which will be automatically
1560 1560 split into a list on whitespace. If given as a list, it must be a list of
1561 1561 *strings* (the variable names themselves), not of variables."""
1562 1562
1563 1563 # this grabs the local variables from the *previous* call frame -- that is
1564 1564 # the locals from the function that called setattr_list().
1565 1565 # - snipped from weave.inline()
1566 1566 if nspace is None:
1567 1567 call_frame = sys._getframe().f_back
1568 1568 nspace = call_frame.f_locals
1569 1569
1570 1570 if type(alist) in StringTypes:
1571 1571 alist = alist.split()
1572 1572 for attr in alist:
1573 1573 val = eval(attr,nspace)
1574 1574 setattr(obj,attr,val)
1575 1575
1576 1576 #----------------------------------------------------------------------------
1577 1577 def getattr_list(obj,alist,*args):
1578 1578 """getattr_list(obj,alist[, default]) -> attribute list.
1579 1579
1580 1580 Get a list of named attributes for an object. When a default argument is
1581 1581 given, it is returned when the attribute doesn't exist; without it, an
1582 1582 exception is raised in that case.
1583 1583
1584 1584 Note that alist can be given as a string, which will be automatically
1585 1585 split into a list on whitespace. If given as a list, it must be a list of
1586 1586 *strings* (the variable names themselves), not of variables."""
1587 1587
1588 1588 if type(alist) in StringTypes:
1589 1589 alist = alist.split()
1590 1590 if args:
1591 1591 if len(args)==1:
1592 1592 default = args[0]
1593 1593 return map(lambda attr: getattr(obj,attr,default),alist)
1594 1594 else:
1595 1595 raise ValueError,'getattr_list() takes only one optional argument'
1596 1596 else:
1597 1597 return map(lambda attr: getattr(obj,attr),alist)
1598 1598
1599 1599 #----------------------------------------------------------------------------
1600 1600 def map_method(method,object_list,*argseq,**kw):
1601 1601 """map_method(method,object_list,*args,**kw) -> list
1602 1602
1603 1603 Return a list of the results of applying the methods to the items of the
1604 1604 argument sequence(s). If more than one sequence is given, the method is
1605 1605 called with an argument list consisting of the corresponding item of each
1606 1606 sequence. All sequences must be of the same length.
1607 1607
1608 1608 Keyword arguments are passed verbatim to all objects called.
1609 1609
1610 1610 This is Python code, so it's not nearly as fast as the builtin map()."""
1611 1611
1612 1612 out_list = []
1613 1613 idx = 0
1614 1614 for object in object_list:
1615 1615 try:
1616 1616 handler = getattr(object, method)
1617 1617 except AttributeError:
1618 1618 out_list.append(None)
1619 1619 else:
1620 1620 if argseq:
1621 1621 args = map(lambda lst:lst[idx],argseq)
1622 1622 #print 'ob',object,'hand',handler,'ar',args # dbg
1623 1623 out_list.append(handler(args,**kw))
1624 1624 else:
1625 1625 out_list.append(handler(**kw))
1626 1626 idx += 1
1627 1627 return out_list
1628 1628
1629 1629 #----------------------------------------------------------------------------
1630 1630 def get_class_members(cls):
1631 1631 ret = dir(cls)
1632 1632 if hasattr(cls,'__bases__'):
1633 1633 for base in cls.__bases__:
1634 1634 ret.extend(get_class_members(base))
1635 1635 return ret
1636 1636
1637 1637 #----------------------------------------------------------------------------
1638 1638 def dir2(obj):
1639 1639 """dir2(obj) -> list of strings
1640 1640
1641 1641 Extended version of the Python builtin dir(), which does a few extra
1642 1642 checks, and supports common objects with unusual internals that confuse
1643 1643 dir(), such as Traits and PyCrust.
1644 1644
1645 1645 This version is guaranteed to return only a list of true strings, whereas
1646 1646 dir() returns anything that objects inject into themselves, even if they
1647 1647 are later not really valid for attribute access (many extension libraries
1648 1648 have such bugs).
1649 1649 """
1650 1650
1651 1651 # Start building the attribute list via dir(), and then complete it
1652 1652 # with a few extra special-purpose calls.
1653 1653 words = dir(obj)
1654 1654
1655 1655 if hasattr(obj,'__class__'):
1656 1656 words.append('__class__')
1657 1657 words.extend(get_class_members(obj.__class__))
1658 1658 #if '__base__' in words: 1/0
1659 1659
1660 1660 # Some libraries (such as traits) may introduce duplicates, we want to
1661 1661 # track and clean this up if it happens
1662 1662 may_have_dupes = False
1663 1663
1664 1664 # this is the 'dir' function for objects with Enthought's traits
1665 1665 if hasattr(obj, 'trait_names'):
1666 1666 try:
1667 1667 words.extend(obj.trait_names())
1668 1668 may_have_dupes = True
1669 1669 except TypeError:
1670 1670 # This will happen if `obj` is a class and not an instance.
1671 1671 pass
1672 1672
1673 1673 # Support for PyCrust-style _getAttributeNames magic method.
1674 1674 if hasattr(obj, '_getAttributeNames'):
1675 1675 try:
1676 1676 words.extend(obj._getAttributeNames())
1677 1677 may_have_dupes = True
1678 1678 except TypeError:
1679 1679 # `obj` is a class and not an instance. Ignore
1680 1680 # this error.
1681 1681 pass
1682 1682
1683 1683 if may_have_dupes:
1684 1684 # eliminate possible duplicates, as some traits may also
1685 1685 # appear as normal attributes in the dir() call.
1686 1686 words = list(set(words))
1687 1687 words.sort()
1688 1688
1689 1689 # filter out non-string attributes which may be stuffed by dir() calls
1690 1690 # and poor coding in third-party modules
1691 1691 return [w for w in words if isinstance(w, basestring)]
1692 1692
1693 1693 #----------------------------------------------------------------------------
1694 1694 def import_fail_info(mod_name,fns=None):
1695 1695 """Inform load failure for a module."""
1696 1696
1697 1697 if fns == None:
1698 1698 warn("Loading of %s failed.\n" % (mod_name,))
1699 1699 else:
1700 1700 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1701 1701
1702 1702 #----------------------------------------------------------------------------
1703 1703 # Proposed popitem() extension, written as a method
1704 1704
1705 1705
1706 1706 class NotGiven: pass
1707 1707
1708 1708 def popkey(dct,key,default=NotGiven):
1709 1709 """Return dct[key] and delete dct[key].
1710 1710
1711 1711 If default is given, return it if dct[key] doesn't exist, otherwise raise
1712 1712 KeyError. """
1713 1713
1714 1714 try:
1715 1715 val = dct[key]
1716 1716 except KeyError:
1717 1717 if default is NotGiven:
1718 1718 raise
1719 1719 else:
1720 1720 return default
1721 1721 else:
1722 1722 del dct[key]
1723 1723 return val
1724 1724
1725 1725 def wrap_deprecated(func, suggest = '<nothing>'):
1726 1726 def newFunc(*args, **kwargs):
1727 1727 warnings.warn("Call to deprecated function %s, use %s instead" %
1728 1728 ( func.__name__, suggest),
1729 1729 category=DeprecationWarning,
1730 1730 stacklevel = 2)
1731 1731 return func(*args, **kwargs)
1732 1732 return newFunc
1733 1733
1734 1734
1735 1735 def _num_cpus_unix():
1736 1736 """Return the number of active CPUs on a Unix system."""
1737 1737 return os.sysconf("SC_NPROCESSORS_ONLN")
1738 1738
1739 1739
1740 1740 def _num_cpus_darwin():
1741 1741 """Return the number of active CPUs on a Darwin system."""
1742 1742 p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
1743 1743 return p.stdout.read()
1744 1744
1745 1745
1746 1746 def _num_cpus_windows():
1747 1747 """Return the number of active CPUs on a Windows system."""
1748 1748 return os.environ.get("NUMBER_OF_PROCESSORS")
1749 1749
1750 1750
1751 1751 def num_cpus():
1752 1752 """Return the effective number of CPUs in the system as an integer.
1753 1753
1754 1754 This cross-platform function makes an attempt at finding the total number of
1755 1755 available CPUs in the system, as returned by various underlying system and
1756 1756 python calls.
1757 1757
1758 1758 If it can't find a sensible answer, it returns 1 (though an error *may* make
1759 1759 it return a large positive number that's actually incorrect).
1760 1760 """
1761 1761
1762 1762 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
1763 1763 # for the names of the keys we needed to look up for this function. This
1764 1764 # code was inspired by their equivalent function.
1765 1765
1766 1766 ncpufuncs = {'Linux':_num_cpus_unix,
1767 1767 'Darwin':_num_cpus_darwin,
1768 1768 'Windows':_num_cpus_windows,
1769 1769 # On Vista, python < 2.5.2 has a bug and returns 'Microsoft'
1770 1770 # See http://bugs.python.org/issue1082 for details.
1771 1771 'Microsoft':_num_cpus_windows,
1772 1772 }
1773 1773
1774 1774 ncpufunc = ncpufuncs.get(platform.system(),
1775 1775 # default to unix version (Solaris, AIX, etc)
1776 1776 _num_cpus_unix)
1777 1777
1778 1778 try:
1779 1779 ncpus = max(1,int(ncpufunc()))
1780 1780 except:
1781 1781 ncpus = 1
1782 1782 return ncpus
1783 1783
1784 1784 def extract_vars(*names,**kw):
1785 1785 """Extract a set of variables by name from another frame.
1786 1786
1787 1787 :Parameters:
1788 1788 - `*names`: strings
1789 1789 One or more variable names which will be extracted from the caller's
1790 1790 frame.
1791 1791
1792 1792 :Keywords:
1793 1793 - `depth`: integer (0)
1794 1794 How many frames in the stack to walk when looking for your variables.
1795 1795
1796 1796
1797 1797 Examples:
1798 1798
1799 1799 In [2]: def func(x):
1800 1800 ...: y = 1
1801 1801 ...: print extract_vars('x','y')
1802 1802 ...:
1803 1803
1804 1804 In [3]: func('hello')
1805 1805 {'y': 1, 'x': 'hello'}
1806 1806 """
1807 1807
1808 1808 depth = kw.get('depth',0)
1809 1809
1810 1810 callerNS = sys._getframe(depth+1).f_locals
1811 1811 return dict((k,callerNS[k]) for k in names)
1812 1812
1813 1813
1814 1814 def extract_vars_above(*names):
1815 1815 """Extract a set of variables by name from another frame.
1816 1816
1817 1817 Similar to extractVars(), but with a specified depth of 1, so that names
1818 1818 are exctracted exactly from above the caller.
1819 1819
1820 1820 This is simply a convenience function so that the very common case (for us)
1821 1821 of skipping exactly 1 frame doesn't have to construct a special dict for
1822 1822 keyword passing."""
1823 1823
1824 1824 callerNS = sys._getframe(2).f_locals
1825 1825 return dict((k,callerNS[k]) for k in names)
1826 1826
1827 1827 def expand_path(s):
1828 1828 """Expand $VARS and ~names in a string, like a shell
1829 1829
1830 1830 :Examples:
1831 1831
1832 1832 In [2]: os.environ['FOO']='test'
1833 1833
1834 1834 In [3]: expand_path('variable FOO is $FOO')
1835 1835 Out[3]: 'variable FOO is test'
1836 1836 """
1837 1837 # This is a pretty subtle hack. When expand user is given a UNC path
1838 1838 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
1839 1839 # the $ to get (\\server\share\%username%). I think it considered $
1840 1840 # alone an empty var. But, we need the $ to remains there (it indicates
1841 1841 # a hidden share).
1842 1842 if os.name=='nt':
1843 1843 s = s.replace('$\\', 'IPYTHON_TEMP')
1844 1844 s = os.path.expandvars(os.path.expanduser(s))
1845 1845 if os.name=='nt':
1846 1846 s = s.replace('IPYTHON_TEMP', '$\\')
1847 1847 return s
1848 1848
1849 1849 def list_strings(arg):
1850 1850 """Always return a list of strings, given a string or list of strings
1851 1851 as input.
1852 1852
1853 1853 :Examples:
1854 1854
1855 1855 In [7]: list_strings('A single string')
1856 1856 Out[7]: ['A single string']
1857 1857
1858 1858 In [8]: list_strings(['A single string in a list'])
1859 1859 Out[8]: ['A single string in a list']
1860 1860
1861 1861 In [9]: list_strings(['A','list','of','strings'])
1862 1862 Out[9]: ['A', 'list', 'of', 'strings']
1863 1863 """
1864 1864
1865 1865 if isinstance(arg,basestring): return [arg]
1866 1866 else: return arg
1867 1867
1868 1868
1869 1869 #----------------------------------------------------------------------------
1870 1870 def marquee(txt='',width=78,mark='*'):
1871 1871 """Return the input string centered in a 'marquee'.
1872 1872
1873 1873 :Examples:
1874 1874
1875 1875 In [16]: marquee('A test',40)
1876 1876 Out[16]: '**************** A test ****************'
1877 1877
1878 1878 In [17]: marquee('A test',40,'-')
1879 1879 Out[17]: '---------------- A test ----------------'
1880 1880
1881 1881 In [18]: marquee('A test',40,' ')
1882 1882 Out[18]: ' A test '
1883 1883
1884 1884 """
1885 1885 if not txt:
1886 1886 return (mark*width)[:width]
1887 1887 nmark = (width-len(txt)-2)/len(mark)/2
1888 1888 if nmark < 0: nmark =0
1889 1889 marks = mark*nmark
1890 1890 return '%s %s %s' % (marks,txt,marks)
1891 1891
1892 1892 #*************************** end of file <genutils.py> **********************
General Comments 0
You need to be logged in to leave comments. Login now