##// END OF EJS Templates
Work to address the review comments on Fernando's branch....
Brian Granger -
Show More
@@ -0,0 +1,167 b''
1 # encoding: utf-8
2 """
3 Older utilities that are not being used.
4
5 WARNING: IF YOU NEED TO USE ONE OF THESE FUNCTIONS, PLEASE FIRST MOVE IT
6 TO ANOTHER APPROPRIATE MODULE IN IPython.utils.
7 """
8
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2009 The IPython Development Team
11 #
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
20 import sys
21 import warnings
22
23 from IPython.utils.warn import warn
24
25 #-----------------------------------------------------------------------------
26 # Code
27 #-----------------------------------------------------------------------------
28
29
30 def mutex_opts(dict,ex_op):
31 """Check for presence of mutually exclusive keys in a dict.
32
33 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
34 for op1,op2 in ex_op:
35 if op1 in dict and op2 in dict:
36 raise ValueError,'\n*** ERROR in Arguments *** '\
37 'Options '+op1+' and '+op2+' are mutually exclusive.'
38
39
40 class EvalDict:
41 """
42 Emulate a dict which evaluates its contents in the caller's frame.
43
44 Usage:
45 >>> number = 19
46
47 >>> text = "python"
48
49 >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
50 Python 2.1 rules!
51 """
52
53 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
54 # modified (shorter) version of:
55 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
56 # Skip Montanaro (skip@pobox.com).
57
58 def __getitem__(self, name):
59 frame = sys._getframe(1)
60 return eval(name, frame.f_globals, frame.f_locals)
61
62 EvalString = EvalDict # for backwards compatibility
63
64
65 def all_belong(candidates,checklist):
66 """Check whether a list of items ALL appear in a given list of options.
67
68 Returns a single 1 or 0 value."""
69
70 return 1-(0 in [x in checklist for x in candidates])
71
72
73 def belong(candidates,checklist):
74 """Check whether a list of items appear in a given list of options.
75
76 Returns a list of 1 and 0, one for each candidate given."""
77
78 return [x in checklist for x in candidates]
79
80
81 def with_obj(object, **args):
82 """Set multiple attributes for an object, similar to Pascal's with.
83
84 Example:
85 with_obj(jim,
86 born = 1960,
87 haircolour = 'Brown',
88 eyecolour = 'Green')
89
90 Credit: Greg Ewing, in
91 http://mail.python.org/pipermail/python-list/2001-May/040703.html.
92
93 NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with'
94 has become a keyword for Python 2.5, so we had to rename it."""
95
96 object.__dict__.update(args)
97
98
99 def map_method(method,object_list,*argseq,**kw):
100 """map_method(method,object_list,*args,**kw) -> list
101
102 Return a list of the results of applying the methods to the items of the
103 argument sequence(s). If more than one sequence is given, the method is
104 called with an argument list consisting of the corresponding item of each
105 sequence. All sequences must be of the same length.
106
107 Keyword arguments are passed verbatim to all objects called.
108
109 This is Python code, so it's not nearly as fast as the builtin map()."""
110
111 out_list = []
112 idx = 0
113 for object in object_list:
114 try:
115 handler = getattr(object, method)
116 except AttributeError:
117 out_list.append(None)
118 else:
119 if argseq:
120 args = map(lambda lst:lst[idx],argseq)
121 #print 'ob',object,'hand',handler,'ar',args # dbg
122 out_list.append(handler(args,**kw))
123 else:
124 out_list.append(handler(**kw))
125 idx += 1
126 return out_list
127
128
129 def import_fail_info(mod_name,fns=None):
130 """Inform load failure for a module."""
131
132 if fns == None:
133 warn("Loading of %s failed.\n" % (mod_name,))
134 else:
135 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
136
137
138 class NotGiven: pass
139
140 def popkey(dct,key,default=NotGiven):
141 """Return dct[key] and delete dct[key].
142
143 If default is given, return it if dct[key] doesn't exist, otherwise raise
144 KeyError. """
145
146 try:
147 val = dct[key]
148 except KeyError:
149 if default is NotGiven:
150 raise
151 else:
152 return default
153 else:
154 del dct[key]
155 return val
156
157
158 def wrap_deprecated(func, suggest = '<nothing>'):
159 def newFunc(*args, **kwargs):
160 warnings.warn("Call to deprecated function %s, use %s instead" %
161 ( func.__name__, suggest),
162 category=DeprecationWarning,
163 stacklevel = 2)
164 return func(*args, **kwargs)
165 return newFunc
166
167
@@ -0,0 +1,31 b''
1 # encoding: utf-8
2 """
3 See if we have curses.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 #-----------------------------------------------------------------------------
18 # Code
19 #-----------------------------------------------------------------------------
20
21 # Curses and termios are Unix-only modules
22 try:
23 import curses
24 # We need termios as well, so if its import happens to raise, we bail on
25 # using curses altogether.
26 import termios
27 except ImportError:
28 use_curses = False
29 else:
30 # Curses on Solaris may not be complete, so we can't use it there
31 use_curses = hasattr(curses,'initscr') No newline at end of file
@@ -0,0 +1,106 b''
1 # encoding: utf-8
2 """Utilities for working with data structures like lists, dicts and tuples.
3 """
4
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2008-2009 The IPython Development Team
7 #
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING, distributed as part of this software.
10 #-----------------------------------------------------------------------------
11
12 #-----------------------------------------------------------------------------
13 # Imports
14 #-----------------------------------------------------------------------------
15
16 import types
17
18 #-----------------------------------------------------------------------------
19 # Code
20 #-----------------------------------------------------------------------------
21
22 def uniq_stable(elems):
23 """uniq_stable(elems) -> list
24
25 Return from an iterable, a list of all the unique elements in the input,
26 but maintaining the order in which they first appear.
27
28 A naive solution to this problem which just makes a dictionary with the
29 elements as keys fails to respect the stability condition, since
30 dictionaries are unsorted by nature.
31
32 Note: All elements in the input must be valid dictionary keys for this
33 routine to work, as it internally uses a dictionary for efficiency
34 reasons."""
35
36 unique = []
37 unique_dict = {}
38 for nn in elems:
39 if nn not in unique_dict:
40 unique.append(nn)
41 unique_dict[nn] = None
42 return unique
43
44
45 def sort_compare(lst1, lst2, inplace=1):
46 """Sort and compare two lists.
47
48 By default it does it in place, thus modifying the lists. Use inplace = 0
49 to avoid that (at the cost of temporary copy creation)."""
50 if not inplace:
51 lst1 = lst1[:]
52 lst2 = lst2[:]
53 lst1.sort(); lst2.sort()
54 return lst1 == lst2
55
56
57 def list2dict(lst):
58 """Takes a list of (key,value) pairs and turns it into a dict."""
59
60 dic = {}
61 for k,v in lst: dic[k] = v
62 return dic
63
64
65 def list2dict2(lst, default=''):
66 """Takes a list and turns it into a dict.
67 Much slower than list2dict, but more versatile. This version can take
68 lists with sublists of arbitrary length (including sclars)."""
69
70 dic = {}
71 for elem in lst:
72 if type(elem) in (types.ListType,types.TupleType):
73 size = len(elem)
74 if size == 0:
75 pass
76 elif size == 1:
77 dic[elem] = default
78 else:
79 k,v = elem[0], elem[1:]
80 if len(v) == 1: v = v[0]
81 dic[k] = v
82 else:
83 dic[elem] = default
84 return dic
85
86
87 def flatten(seq):
88 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
89
90 return [x for subseq in seq for x in subseq]
91
92
93 def get_slice(seq, start=0, stop=None, step=1):
94 """Get a slice of a sequence with variable step. Specify start,stop,step."""
95 if stop == None:
96 stop = len(seq)
97 item = lambda i: seq[i]
98 return map(item,xrange(start,stop,step))
99
100
101 def chop(seq, size):
102 """Chop a sequence into chunks of the given size."""
103 chunk = lambda i: seq[i:i+size]
104 return map(chunk,xrange(0,len(seq),size))
105
106
@@ -0,0 +1,46 b''
1 # encoding: utf-8
2 """Decorators that don't go anywhere else.
3
4 This module contains misc. decorators that don't really go with another module
5 in :mod:`IPython.utils`. Beore putting something here please see if it should
6 go into another topical module in :mod:`IPython.utils`.
7 """
8
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2009 The IPython Development Team
11 #
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
20 #-----------------------------------------------------------------------------
21 # Code
22 #-----------------------------------------------------------------------------
23
24 def flag_calls(func):
25 """Wrap a function to detect and flag when it gets called.
26
27 This is a decorator which takes a function and wraps it in a function with
28 a 'called' attribute. wrapper.called is initialized to False.
29
30 The wrapper.called attribute is set to False right before each call to the
31 wrapped function, so if the call fails it remains False. After the call
32 completes, wrapper.called is set to True and the output is returned.
33
34 Testing for truth in wrapper.called allows you to determine if a call to
35 func() was attempted and succeeded."""
36
37 def wrapper(*args,**kw):
38 wrapper.called = False
39 out = func(*args,**kw)
40 wrapper.called = True
41 return out
42
43 wrapper.called = False
44 wrapper.__doc__ = func.__doc__
45 return wrapper
46
@@ -0,0 +1,82 b''
1 # encoding: utf-8
2 """A fancy version of Python's builtin :func:`dir` function.
3 """
4
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2008-2009 The IPython Development Team
7 #
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING, distributed as part of this software.
10 #-----------------------------------------------------------------------------
11
12 #-----------------------------------------------------------------------------
13 # Imports
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Code
18 #-----------------------------------------------------------------------------
19
20 def get_class_members(cls):
21 ret = dir(cls)
22 if hasattr(cls,'__bases__'):
23 for base in cls.__bases__:
24 ret.extend(get_class_members(base))
25 return ret
26
27
28 def dir2(obj):
29 """dir2(obj) -> list of strings
30
31 Extended version of the Python builtin dir(), which does a few extra
32 checks, and supports common objects with unusual internals that confuse
33 dir(), such as Traits and PyCrust.
34
35 This version is guaranteed to return only a list of true strings, whereas
36 dir() returns anything that objects inject into themselves, even if they
37 are later not really valid for attribute access (many extension libraries
38 have such bugs).
39 """
40
41 # Start building the attribute list via dir(), and then complete it
42 # with a few extra special-purpose calls.
43 words = dir(obj)
44
45 if hasattr(obj,'__class__'):
46 words.append('__class__')
47 words.extend(get_class_members(obj.__class__))
48 #if '__base__' in words: 1/0
49
50 # Some libraries (such as traits) may introduce duplicates, we want to
51 # track and clean this up if it happens
52 may_have_dupes = False
53
54 # this is the 'dir' function for objects with Enthought's traits
55 if hasattr(obj, 'trait_names'):
56 try:
57 words.extend(obj.trait_names())
58 may_have_dupes = True
59 except TypeError:
60 # This will happen if `obj` is a class and not an instance.
61 pass
62
63 # Support for PyCrust-style _getAttributeNames magic method.
64 if hasattr(obj, '_getAttributeNames'):
65 try:
66 words.extend(obj._getAttributeNames())
67 may_have_dupes = True
68 except TypeError:
69 # `obj` is a class and not an instance. Ignore
70 # this error.
71 pass
72
73 if may_have_dupes:
74 # eliminate possible duplicates, as some traits may also
75 # appear as normal attributes in the dir() call.
76 words = list(set(words))
77 words.sort()
78
79 # filter out non-string attributes which may be stuffed by dir() calls
80 # and poor coding in third-party modules
81 return [w for w in words if isinstance(w, basestring)]
82
@@ -0,0 +1,75 b''
1 # encoding: utf-8
2 """
3 A utility for handling the reloading of doctest.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import sys
18
19 #-----------------------------------------------------------------------------
20 # Code
21 #-----------------------------------------------------------------------------
22
23 def dhook_wrap(func,*a,**k):
24 """Wrap a function call in a sys.displayhook controller.
25
26 Returns a wrapper around func which calls func, with all its arguments and
27 keywords unmodified, using the default sys.displayhook. Since IPython
28 modifies sys.displayhook, it breaks the behavior of certain systems that
29 rely on the default behavior, notably doctest.
30 """
31
32 def f(*a,**k):
33
34 dhook_s = sys.displayhook
35 sys.displayhook = sys.__displayhook__
36 try:
37 out = func(*a,**k)
38 finally:
39 sys.displayhook = dhook_s
40
41 return out
42
43 f.__doc__ = func.__doc__
44 return f
45
46
47 def doctest_reload():
48 """Properly reload doctest to reuse it interactively.
49
50 This routine:
51
52 - imports doctest but does NOT reload it (see below).
53
54 - resets its global 'master' attribute to None, so that multiple uses of
55 the module interactively don't produce cumulative reports.
56
57 - Monkeypatches its core test runner method to protect it from IPython's
58 modified displayhook. Doctest expects the default displayhook behavior
59 deep down, so our modification breaks it completely. For this reason, a
60 hard monkeypatch seems like a reasonable solution rather than asking
61 users to manually use a different doctest runner when under IPython.
62
63 Notes
64 -----
65
66 This function *used to* reload doctest, but this has been disabled because
67 reloading doctest unconditionally can cause massive breakage of other
68 doctest-dependent modules already in memory, such as those for IPython's
69 own testing system. The name wasn't changed to avoid breaking people's
70 code, but the reload call isn't actually made anymore."""
71
72 import doctest
73 doctest.master = None
74 doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run)
75
@@ -0,0 +1,85 b''
1 # encoding: utf-8
2 """
3 Utilities for working with stack frames.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import sys
18
19 #-----------------------------------------------------------------------------
20 # Code
21 #-----------------------------------------------------------------------------
22
23 def extract_vars(*names,**kw):
24 """Extract a set of variables by name from another frame.
25
26 :Parameters:
27 - `*names`: strings
28 One or more variable names which will be extracted from the caller's
29 frame.
30
31 :Keywords:
32 - `depth`: integer (0)
33 How many frames in the stack to walk when looking for your variables.
34
35
36 Examples:
37
38 In [2]: def func(x):
39 ...: y = 1
40 ...: print extract_vars('x','y')
41 ...:
42
43 In [3]: func('hello')
44 {'y': 1, 'x': 'hello'}
45 """
46
47 depth = kw.get('depth',0)
48
49 callerNS = sys._getframe(depth+1).f_locals
50 return dict((k,callerNS[k]) for k in names)
51
52
53 def extract_vars_above(*names):
54 """Extract a set of variables by name from another frame.
55
56 Similar to extractVars(), but with a specified depth of 1, so that names
57 are exctracted exactly from above the caller.
58
59 This is simply a convenience function so that the very common case (for us)
60 of skipping exactly 1 frame doesn't have to construct a special dict for
61 keyword passing."""
62
63 callerNS = sys._getframe(2).f_locals
64 return dict((k,callerNS[k]) for k in names)
65
66
67 def debugx(expr,pre_msg=''):
68 """Print the value of an expression from the caller's frame.
69
70 Takes an expression, evaluates it in the caller's frame and prints both
71 the given expression and the resulting value (as well as a debug mark
72 indicating the name of the calling function. The input must be of a form
73 suitable for eval().
74
75 An optional message can be passed, which will be prepended to the printed
76 expr->value pair."""
77
78 cf = sys._getframe(1)
79 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
80 eval(expr,cf.f_globals,cf.f_locals))
81
82
83 # deactivate it by uncommenting the following line, which makes it a no-op
84 #def debugx(expr,pre_msg=''): pass
85
@@ -0,0 +1,292 b''
1 # encoding: utf-8
2 """
3 IO related utilities.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import sys
18 import tempfile
19
20 from IPython.external.Itpl import itpl, printpl
21
22 #-----------------------------------------------------------------------------
23 # Code
24 #-----------------------------------------------------------------------------
25
26
27 class IOStream:
28
29 def __init__(self,stream,fallback):
30 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
31 stream = fallback
32 self.stream = stream
33 self._swrite = stream.write
34 self.flush = stream.flush
35
36 def write(self,data):
37 try:
38 self._swrite(data)
39 except:
40 try:
41 # print handles some unicode issues which may trip a plain
42 # write() call. Attempt to emulate write() by using a
43 # trailing comma
44 print >> self.stream, data,
45 except:
46 # if we get here, something is seriously broken.
47 print >> sys.stderr, \
48 'ERROR - failed to write data to stream:', self.stream
49
50 def writeln(self, data):
51 self.write(data)
52 self.write('\n')
53
54 def close(self):
55 pass
56
57
58 class IOTerm:
59 """ Term holds the file or file-like objects for handling I/O operations.
60
61 These are normally just sys.stdin, sys.stdout and sys.stderr but for
62 Windows they can can replaced to allow editing the strings before they are
63 displayed."""
64
65 # In the future, having IPython channel all its I/O operations through
66 # this class will make it easier to embed it into other environments which
67 # are not a normal terminal (such as a GUI-based shell)
68 def __init__(self,cin=None,cout=None,cerr=None):
69 self.cin = IOStream(cin,sys.stdin)
70 self.cout = IOStream(cout,sys.stdout)
71 self.cerr = IOStream(cerr,sys.stderr)
72
73
74 # Global variable to be used for all I/O
75 Term = IOTerm()
76
77
78 import IPython.utils.rlineimpl as readline
79 # Remake Term to use the readline i/o facilities
80 if sys.platform == 'win32' and readline.have_readline:
81
82 Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile)
83
84
85 class Tee(object):
86 """A class to duplicate an output stream to stdout/err.
87
88 This works in a manner very similar to the Unix 'tee' command.
89
90 When the object is closed or deleted, it closes the original file given to
91 it for duplication.
92 """
93 # Inspired by:
94 # http://mail.python.org/pipermail/python-list/2007-May/442737.html
95
96 def __init__(self, file_or_name, mode=None, channel='stdout'):
97 """Construct a new Tee object.
98
99 Parameters
100 ----------
101 file_or_name : filename or open filehandle (writable)
102 File that will be duplicated
103
104 mode : optional, valid mode for open().
105 If a filename was give, open with this mode.
106
107 channel : str, one of ['stdout', 'stderr']
108 """
109 if channel not in ['stdout', 'stderr']:
110 raise ValueError('Invalid channel spec %s' % channel)
111
112 if hasattr(file, 'write') and hasattr(file, 'seek'):
113 self.file = file_or_name
114 else:
115 self.file = open(file_or_name, mode)
116 self.channel = channel
117 self.ostream = getattr(sys, channel)
118 setattr(sys, channel, self)
119 self._closed = False
120
121 def close(self):
122 """Close the file and restore the channel."""
123 self.flush()
124 setattr(sys, self.channel, self.ostream)
125 self.file.close()
126 self._closed = True
127
128 def write(self, data):
129 """Write data to both channels."""
130 self.file.write(data)
131 self.ostream.write(data)
132 self.ostream.flush()
133
134 def flush(self):
135 """Flush both channels."""
136 self.file.flush()
137 self.ostream.flush()
138
139 def __del__(self):
140 if not self._closed:
141 self.close()
142
143
144 def file_read(filename):
145 """Read a file and close it. Returns the file source."""
146 fobj = open(filename,'r');
147 source = fobj.read();
148 fobj.close()
149 return source
150
151
152 def file_readlines(filename):
153 """Read a file and close it. Returns the file source using readlines()."""
154 fobj = open(filename,'r');
155 lines = fobj.readlines();
156 fobj.close()
157 return lines
158
159
160 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
161 """Take multiple lines of input.
162
163 A list with each line of input as a separate element is returned when a
164 termination string is entered (defaults to a single '.'). Input can also
165 terminate via EOF (^D in Unix, ^Z-RET in Windows).
166
167 Lines of input which end in \\ are joined into single entries (and a
168 secondary continuation prompt is issued as long as the user terminates
169 lines with \\). This allows entering very long strings which are still
170 meant to be treated as single entities.
171 """
172
173 try:
174 if header:
175 header += '\n'
176 lines = [raw_input(header + ps1)]
177 except EOFError:
178 return []
179 terminate = [terminate_str]
180 try:
181 while lines[-1:] != terminate:
182 new_line = raw_input(ps1)
183 while new_line.endswith('\\'):
184 new_line = new_line[:-1] + raw_input(ps2)
185 lines.append(new_line)
186
187 return lines[:-1] # don't return the termination command
188 except EOFError:
189 print
190 return lines
191
192
193 def raw_input_ext(prompt='', ps2='... '):
194 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
195
196 line = raw_input(prompt)
197 while line.endswith('\\'):
198 line = line[:-1] + raw_input(ps2)
199 return line
200
201
202 def ask_yes_no(prompt,default=None):
203 """Asks a question and returns a boolean (y/n) answer.
204
205 If default is given (one of 'y','n'), it is used if the user input is
206 empty. Otherwise the question is repeated until an answer is given.
207
208 An EOF is treated as the default answer. If there is no default, an
209 exception is raised to prevent infinite loops.
210
211 Valid answers are: y/yes/n/no (match is not case sensitive)."""
212
213 answers = {'y':True,'n':False,'yes':True,'no':False}
214 ans = None
215 while ans not in answers.keys():
216 try:
217 ans = raw_input(prompt+' ').lower()
218 if not ans: # response was an empty string
219 ans = default
220 except KeyboardInterrupt:
221 pass
222 except EOFError:
223 if default in answers.keys():
224 ans = default
225 print
226 else:
227 raise
228
229 return answers[ans]
230
231
232 class NLprinter:
233 """Print an arbitrarily nested list, indicating index numbers.
234
235 An instance of this class called nlprint is available and callable as a
236 function.
237
238 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
239 and using 'sep' to separate the index from the value. """
240
241 def __init__(self):
242 self.depth = 0
243
244 def __call__(self,lst,pos='',**kw):
245 """Prints the nested list numbering levels."""
246 kw.setdefault('indent',' ')
247 kw.setdefault('sep',': ')
248 kw.setdefault('start',0)
249 kw.setdefault('stop',len(lst))
250 # we need to remove start and stop from kw so they don't propagate
251 # into a recursive call for a nested list.
252 start = kw['start']; del kw['start']
253 stop = kw['stop']; del kw['stop']
254 if self.depth == 0 and 'header' in kw.keys():
255 print kw['header']
256
257 for idx in range(start,stop):
258 elem = lst[idx]
259 if type(elem)==type([]):
260 self.depth += 1
261 self.__call__(elem,itpl('$pos$idx,'),**kw)
262 self.depth -= 1
263 else:
264 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
265
266 nlprint = NLprinter()
267
268
269 def temp_pyfile(src, ext='.py'):
270 """Make a temporary python file, return filename and filehandle.
271
272 Parameters
273 ----------
274 src : string or list of strings (no need for ending newlines if list)
275 Source code to be written to the file.
276
277 ext : optional, string
278 Extension for the generated file.
279
280 Returns
281 -------
282 (filename, open filehandle)
283 It is the caller's responsibility to close the open file and unlink it.
284 """
285 fname = tempfile.mkstemp(ext)[1]
286 f = open(fname,'w')
287 f.write(src)
288 f.flush()
289 return fname, f
290
291
292
@@ -0,0 +1,342 b''
1 # encoding: utf-8
2 """
3 Utilities for path handling.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import os
18 import sys
19
20 import IPython
21 from IPython.utils.process import xsys
22 from IPython.utils.importstring import import_item
23
24 #-----------------------------------------------------------------------------
25 # Code
26 #-----------------------------------------------------------------------------
27
28
29 def _get_long_path_name(path):
30 """Dummy no-op."""
31 return path
32
33
34 if sys.platform == 'win32':
35 def _get_long_path_name(path):
36 """Get a long path name (expand ~) on Windows using ctypes.
37
38 Examples
39 --------
40
41 >>> get_long_path_name('c:\\docume~1')
42 u'c:\\\\Documents and Settings'
43
44 """
45 try:
46 import ctypes
47 except ImportError:
48 raise ImportError('you need to have ctypes installed for this to work')
49 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
50 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
51 ctypes.c_uint ]
52
53 buf = ctypes.create_unicode_buffer(260)
54 rv = _GetLongPathName(path, buf, 260)
55 if rv == 0 or rv > 260:
56 return path
57 else:
58 return buf.value
59
60
61 def get_long_path_name(path):
62 """Expand a path into its long form.
63
64 On Windows this expands any ~ in the paths. On other platforms, it is
65 a null operation.
66 """
67 return _get_long_path_name(path)
68
69
70 def get_py_filename(name):
71 """Return a valid python filename in the current directory.
72
73 If the given name is not a file, it adds '.py' and searches again.
74 Raises IOError with an informative message if the file isn't found."""
75
76 name = os.path.expanduser(name)
77 if not os.path.isfile(name) and not name.endswith('.py'):
78 name += '.py'
79 if os.path.isfile(name):
80 return name
81 else:
82 raise IOError,'File `%s` not found.' % name
83
84
85 def filefind(filename, path_dirs=None):
86 """Find a file by looking through a sequence of paths.
87
88 This iterates through a sequence of paths looking for a file and returns
89 the full, absolute path of the first occurence of the file. If no set of
90 path dirs is given, the filename is tested as is, after running through
91 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
92
93 filefind('myfile.txt')
94
95 will find the file in the current working dir, but::
96
97 filefind('~/myfile.txt')
98
99 Will find the file in the users home directory. This function does not
100 automatically try any paths, such as the cwd or the user's home directory.
101
102 Parameters
103 ----------
104 filename : str
105 The filename to look for.
106 path_dirs : str, None or sequence of str
107 The sequence of paths to look for the file in. If None, the filename
108 need to be absolute or be in the cwd. If a string, the string is
109 put into a sequence and the searched. If a sequence, walk through
110 each element and join with ``filename``, calling :func:`expandvars`
111 and :func:`expanduser` before testing for existence.
112
113 Returns
114 -------
115 Raises :exc:`IOError` or returns absolute path to file.
116 """
117
118 # If paths are quoted, abspath gets confused, strip them...
119 filename = filename.strip('"').strip("'")
120 # If the input is an absolute path, just check it exists
121 if os.path.isabs(filename) and os.path.isfile(filename):
122 return filename
123
124 if path_dirs is None:
125 path_dirs = ("",)
126 elif isinstance(path_dirs, basestring):
127 path_dirs = (path_dirs,)
128
129 for path in path_dirs:
130 if path == '.': path = os.getcwd()
131 testname = expand_path(os.path.join(path, filename))
132 if os.path.isfile(testname):
133 return os.path.abspath(testname)
134
135 raise IOError("File %r does not exist in any of the search paths: %r" %
136 (filename, path_dirs) )
137
138
139 class HomeDirError(Exception):
140 pass
141
142
143 def get_home_dir():
144 """Return the closest possible equivalent to a 'home' directory.
145
146 * On POSIX, we try $HOME.
147 * On Windows we try:
148 - %HOME%: rare, but some people with unix-like setups may have defined it
149 - %HOMESHARE%
150 - %HOMEDRIVE\%HOMEPATH%
151 - %USERPROFILE%
152 - Registry hack
153 * On Dos C:\
154
155 Currently only Posix and NT are implemented, a HomeDirError exception is
156 raised for all other OSes.
157 """
158
159 isdir = os.path.isdir
160 env = os.environ
161
162 # first, check py2exe distribution root directory for _ipython.
163 # This overrides all. Normally does not exist.
164
165 if hasattr(sys, "frozen"): #Is frozen by py2exe
166 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
167 root, rest = IPython.__file__.lower().split('library.zip')
168 else:
169 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
170 root=os.path.abspath(root).rstrip('\\')
171 if isdir(os.path.join(root, '_ipython')):
172 os.environ["IPYKITROOT"] = root
173 return root.decode(sys.getfilesystemencoding())
174
175 if os.name == 'posix':
176 # Linux, Unix, AIX, OS X
177 try:
178 homedir = env['HOME']
179 except KeyError:
180 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
181 else:
182 return homedir.decode(sys.getfilesystemencoding())
183 elif os.name == 'nt':
184 # Now for win9x, XP, Vista, 7?
185 # For some strange reason all of these return 'nt' for os.name.
186 # First look for a network home directory. This will return the UNC
187 # path (\\server\\Users\%username%) not the mapped path (Z:\). This
188 # is needed when running IPython on cluster where all paths have to
189 # be UNC.
190 try:
191 # A user with a lot of unix tools in win32 may have defined $HOME,
192 # honor it if it exists, but otherwise let the more typical
193 # %HOMESHARE% variable be used.
194 homedir = env.get('HOME')
195 if homedir is None:
196 homedir = env['HOMESHARE']
197 except KeyError:
198 pass
199 else:
200 if isdir(homedir):
201 return homedir.decode(sys.getfilesystemencoding())
202
203 # Now look for a local home directory
204 try:
205 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
206 except KeyError:
207 pass
208 else:
209 if isdir(homedir):
210 return homedir.decode(sys.getfilesystemencoding())
211
212 # Now the users profile directory
213 try:
214 homedir = os.path.join(env['USERPROFILE'])
215 except KeyError:
216 pass
217 else:
218 if isdir(homedir):
219 return homedir.decode(sys.getfilesystemencoding())
220
221 # Use the registry to get the 'My Documents' folder.
222 try:
223 import _winreg as wreg
224 key = wreg.OpenKey(
225 wreg.HKEY_CURRENT_USER,
226 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
227 )
228 homedir = wreg.QueryValueEx(key,'Personal')[0]
229 key.Close()
230 except:
231 pass
232 else:
233 if isdir(homedir):
234 return homedir.decode(sys.getfilesystemencoding())
235
236 # If all else fails, raise HomeDirError
237 raise HomeDirError('No valid home directory could be found')
238 elif os.name == 'dos':
239 # Desperate, may do absurd things in classic MacOS. May work under DOS.
240 return 'C:\\'.decode(sys.getfilesystemencoding())
241 else:
242 raise HomeDirError('No valid home directory could be found for your OS')
243
244
245 def get_ipython_dir():
246 """Get the IPython directory for this platform and user.
247
248 This uses the logic in `get_home_dir` to find the home directory
249 and the adds .ipython to the end of the path.
250 """
251 ipdir_def = '.ipython'
252 print get_home_dir
253 home_dir = get_home_dir()
254 print home_dir
255 #import pdb; pdb.set_trace() # dbg
256 ipdir = os.environ.get(
257 'IPYTHON_DIR', os.environ.get(
258 'IPYTHONDIR', os.path.join(home_dir, ipdir_def)
259 )
260 )
261 return ipdir.decode(sys.getfilesystemencoding())
262
263
264 def get_ipython_package_dir():
265 """Get the base directory where IPython itself is installed."""
266 ipdir = os.path.dirname(IPython.__file__)
267 return ipdir.decode(sys.getfilesystemencoding())
268
269
270 def get_ipython_module_path(module_str):
271 """Find the path to an IPython module in this version of IPython.
272
273 This will always find the version of the module that is in this importable
274 IPython package. This will always return the path to the ``.py``
275 version of the module.
276 """
277 if module_str == 'IPython':
278 return os.path.join(get_ipython_package_dir(), '__init__.py')
279 mod = import_item(module_str)
280 the_path = mod.__file__.replace('.pyc', '.py')
281 the_path = the_path.replace('.pyo', '.py')
282 return the_path.decode(sys.getfilesystemencoding())
283
284
285 def expand_path(s):
286 """Expand $VARS and ~names in a string, like a shell
287
288 :Examples:
289
290 In [2]: os.environ['FOO']='test'
291
292 In [3]: expand_path('variable FOO is $FOO')
293 Out[3]: 'variable FOO is test'
294 """
295 # This is a pretty subtle hack. When expand user is given a UNC path
296 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
297 # the $ to get (\\server\share\%username%). I think it considered $
298 # alone an empty var. But, we need the $ to remains there (it indicates
299 # a hidden share).
300 if os.name=='nt':
301 s = s.replace('$\\', 'IPYTHON_TEMP')
302 s = os.path.expandvars(os.path.expanduser(s))
303 if os.name=='nt':
304 s = s.replace('IPYTHON_TEMP', '$\\')
305 return s
306
307
308 def target_outdated(target,deps):
309 """Determine whether a target is out of date.
310
311 target_outdated(target,deps) -> 1/0
312
313 deps: list of filenames which MUST exist.
314 target: single filename which may or may not exist.
315
316 If target doesn't exist or is older than any file listed in deps, return
317 true, otherwise return false.
318 """
319 try:
320 target_time = os.path.getmtime(target)
321 except os.error:
322 return 1
323 for dep in deps:
324 dep_time = os.path.getmtime(dep)
325 if dep_time > target_time:
326 #print "For target",target,"Dep failed:",dep # dbg
327 #print "times (dep,tar):",dep_time,target_time # dbg
328 return 1
329 return 0
330
331
332 def target_update(target,deps,cmd):
333 """Update a target with a given command given a list of dependencies.
334
335 target_update(target,deps,cmd) -> runs cmd if target is outdated.
336
337 This is just a wrapper around target_outdated() which calls the given
338 command if target is outdated."""
339
340 if target_outdated(target,deps):
341 xsys(cmd)
342
@@ -0,0 +1,368 b''
1 # encoding: utf-8
2 """
3 Utilities for working with external processes.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import os
18 import sys
19 import shlex
20 import subprocess
21
22 from IPython.utils.terminal import set_term_title
23
24 #-----------------------------------------------------------------------------
25 # Code
26 #-----------------------------------------------------------------------------
27
28
29 class FindCmdError(Exception):
30 pass
31
32
33 def _find_cmd(cmd):
34 """Find the full path to a command using which."""
35 return os.popen('which %s' % cmd).read().strip()
36
37
38 if os.name == 'posix':
39 def _find_cmd(cmd):
40 """Find the full path to a command using which."""
41 return getoutputerror('/usr/bin/env which %s' % cmd)[0]
42
43
44 if sys.platform == 'win32':
45 def _find_cmd(cmd):
46 """Find the full path to a .bat or .exe using the win32api module."""
47 try:
48 from win32api import SearchPath
49 except ImportError:
50 raise ImportError('you need to have pywin32 installed for this to work')
51 else:
52 PATH = os.environ['PATH']
53 extensions = ['.exe', '.com', '.bat', '.py']
54 path = None
55 for ext in extensions:
56 try:
57 path = SearchPath(PATH,cmd + ext)[0]
58 except:
59 pass
60 if path is None:
61 raise OSError("command %r not found" % cmd)
62 else:
63 return path
64
65
66 def find_cmd(cmd):
67 """Find absolute path to executable cmd in a cross platform manner.
68
69 This function tries to determine the full path to a command line program
70 using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the
71 time it will use the version that is first on the users `PATH`. If
72 cmd is `python` return `sys.executable`.
73
74 Warning, don't use this to find IPython command line programs as there
75 is a risk you will find the wrong one. Instead find those using the
76 following code and looking for the application itself::
77
78 from IPython.utils.path import get_ipython_module_path
79 from IPython.utils.process import pycmd2argv
80 argv = pycmd2argv(get_ipython_module_path('IPython.core.ipapp'))
81
82 Parameters
83 ----------
84 cmd : str
85 The command line program to look for.
86 """
87 if cmd == 'python':
88 return os.path.abspath(sys.executable)
89 try:
90 path = _find_cmd(cmd)
91 except OSError:
92 raise FindCmdError('command could not be found: %s' % cmd)
93 # which returns empty if not found
94 if path == '':
95 raise FindCmdError('command could not be found: %s' % cmd)
96 return os.path.abspath(path)
97
98
99 def pycmd2argv(cmd):
100 r"""Take the path of a python command and return a list (argv-style).
101
102 This only works on Python based command line programs and will find the
103 location of the ``python`` executable using ``sys.executable`` to make
104 sure the right version is used.
105
106 For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
107 .com or .bat, and [, cmd] otherwise.
108
109 Parameters
110 ----------
111 cmd : string
112 The path of the command.
113
114 Returns
115 -------
116 argv-style list.
117 """
118 ext = os.path.splitext(cmd)[1]
119 if ext in ['.exe', '.com', '.bat']:
120 return [cmd]
121 else:
122 if sys.platform == 'win32':
123 # The -u option here turns on unbuffered output, which is required
124 # on Win32 to prevent wierd conflict and problems with Twisted.
125 # Also, use sys.executable to make sure we are picking up the
126 # right python exe.
127 return [sys.executable, '-u', cmd]
128 else:
129 return [sys.executable, cmd]
130
131
132 def arg_split(s, posix=False):
133 """Split a command line's arguments in a shell-like manner.
134
135 This is a modified version of the standard library's shlex.split()
136 function, but with a default of posix=False for splitting, so that quotes
137 in inputs are respected."""
138
139 # XXX - there may be unicode-related problems here!!! I'm not sure that
140 # shlex is truly unicode-safe, so it might be necessary to do
141 #
142 # s = s.encode(sys.stdin.encoding)
143 #
144 # first, to ensure that shlex gets a normal string. Input from anyone who
145 # knows more about unicode and shlex than I would be good to have here...
146 lex = shlex.shlex(s, posix=posix)
147 lex.whitespace_split = True
148 return list(lex)
149
150
151 def system(cmd, verbose=0, debug=0, header=''):
152 """Execute a system command, return its exit status.
153
154 Options:
155
156 - verbose (0): print the command to be executed.
157
158 - debug (0): only print, do not actually execute.
159
160 - header (''): Header to print on screen prior to the executed command (it
161 is only prepended to the command, no newlines are added).
162
163 Note: a stateful version of this function is available through the
164 SystemExec class."""
165
166 stat = 0
167 if verbose or debug: print header+cmd
168 sys.stdout.flush()
169 if not debug: stat = os.system(cmd)
170 return stat
171
172
173 def abbrev_cwd():
174 """ Return abbreviated version of cwd, e.g. d:mydir """
175 cwd = os.getcwd().replace('\\','/')
176 drivepart = ''
177 tail = cwd
178 if sys.platform == 'win32':
179 if len(cwd) < 4:
180 return cwd
181 drivepart,tail = os.path.splitdrive(cwd)
182
183
184 parts = tail.split('/')
185 if len(parts) > 2:
186 tail = '/'.join(parts[-2:])
187
188 return (drivepart + (
189 cwd == '/' and '/' or tail))
190
191
192 # This function is used by ipython in a lot of places to make system calls.
193 # We need it to be slightly different under win32, due to the vagaries of
194 # 'network shares'. A win32 override is below.
195
196 def shell(cmd, verbose=0, debug=0, header=''):
197 """Execute a command in the system shell, always return None.
198
199 Options:
200
201 - verbose (0): print the command to be executed.
202
203 - debug (0): only print, do not actually execute.
204
205 - header (''): Header to print on screen prior to the executed command (it
206 is only prepended to the command, no newlines are added).
207
208 Note: this is similar to system(), but it returns None so it can
209 be conveniently used in interactive loops without getting the return value
210 (typically 0) printed many times."""
211
212 stat = 0
213 if verbose or debug: print header+cmd
214 # flush stdout so we don't mangle python's buffering
215 sys.stdout.flush()
216
217 if not debug:
218 set_term_title("IPy " + cmd)
219 os.system(cmd)
220 set_term_title("IPy " + abbrev_cwd())
221
222 # override shell() for win32 to deal with network shares
223 if os.name in ('nt','dos'):
224
225 shell_ori = shell
226
227 def shell(cmd, verbose=0, debug=0, header=''):
228 if os.getcwd().startswith(r"\\"):
229 path = os.getcwd()
230 # change to c drive (cannot be on UNC-share when issuing os.system,
231 # as cmd.exe cannot handle UNC addresses)
232 os.chdir("c:")
233 # issue pushd to the UNC-share and then run the command
234 try:
235 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
236 finally:
237 os.chdir(path)
238 else:
239 shell_ori(cmd,verbose,debug,header)
240
241 shell.__doc__ = shell_ori.__doc__
242
243
244 def getoutput(cmd, verbose=0, debug=0, header='', split=0):
245 """Dummy substitute for perl's backquotes.
246
247 Executes a command and returns the output.
248
249 Accepts the same arguments as system(), plus:
250
251 - split(0): if true, the output is returned as a list split on newlines.
252
253 Note: a stateful version of this function is available through the
254 SystemExec class.
255
256 This is pretty much deprecated and rarely used, getoutputerror may be
257 what you need.
258
259 """
260
261 if verbose or debug: print header+cmd
262 if not debug:
263 pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
264 output = pipe.read()
265 # stipping last \n is here for backwards compat.
266 if output.endswith('\n'):
267 output = output[:-1]
268 if split:
269 return output.split('\n')
270 else:
271 return output
272
273
274 # for compatibility with older naming conventions
275 xsys = system
276
277
278 def getoutputerror(cmd, verbose=0, debug=0, header='', split=0):
279 """Return (standard output,standard error) of executing cmd in a shell.
280
281 Accepts the same arguments as system(), plus:
282
283 - split(0): if true, each of stdout/err is returned as a list split on
284 newlines.
285
286 Note: a stateful version of this function is available through the
287 SystemExec class."""
288
289 if verbose or debug: print header+cmd
290 if not cmd:
291 if split:
292 return [],[]
293 else:
294 return '',''
295 if not debug:
296 p = subprocess.Popen(cmd, shell=True,
297 stdin=subprocess.PIPE,
298 stdout=subprocess.PIPE,
299 stderr=subprocess.PIPE,
300 close_fds=True)
301 pin, pout, perr = (p.stdin, p.stdout, p.stderr)
302
303 tout = pout.read().rstrip()
304 terr = perr.read().rstrip()
305 pin.close()
306 pout.close()
307 perr.close()
308 if split:
309 return tout.split('\n'),terr.split('\n')
310 else:
311 return tout,terr
312
313
314 class SystemExec:
315 """Access the system and getoutput functions through a stateful interface.
316
317 Note: here we refer to the system and getoutput functions from this
318 library, not the ones from the standard python library.
319
320 This class offers the system and getoutput functions as methods, but the
321 verbose, debug and header parameters can be set for the instance (at
322 creation time or later) so that they don't need to be specified on each
323 call.
324
325 For efficiency reasons, there's no way to override the parameters on a
326 per-call basis other than by setting instance attributes. If you need
327 local overrides, it's best to directly call system() or getoutput().
328
329 The following names are provided as alternate options:
330 - xsys: alias to system
331 - bq: alias to getoutput
332
333 An instance can then be created as:
334 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
335 """
336
337 def __init__(self, verbose=0, debug=0, header='', split=0):
338 """Specify the instance's values for verbose, debug and header."""
339 self.verbose = verbose
340 self.debug = debug
341 self.header = header
342 self.split = split
343
344 def system(self, cmd):
345 """Stateful interface to system(), with the same keyword parameters."""
346
347 system(cmd, self.verbose, self.debug, self.header)
348
349 def shell(self, cmd):
350 """Stateful interface to shell(), with the same keyword parameters."""
351
352 shell(cmd, self.verbose, self.debug, self.header)
353
354 xsys = system # alias
355
356 def getoutput(self, cmd):
357 """Stateful interface to getoutput()."""
358
359 return getoutput(cmd, self.verbose, self.debug, self.header, self.split)
360
361 def getoutputerror(self, cmd):
362 """Stateful interface to getoutputerror()."""
363
364 return getoutputerror(cmd, self.verbose, self.debug, self.header, self.split)
365
366 bq = getoutput # alias
367
368
@@ -0,0 +1,100 b''
1 # encoding: utf-8
2 """
3 Utilities for getting information about a system.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import os
18 import platform
19 import sys
20 import subprocess
21
22 from IPython.core import release
23
24 #-----------------------------------------------------------------------------
25 # Code
26 #-----------------------------------------------------------------------------
27
28 def sys_info():
29 """Return useful information about IPython and the system, as a string.
30
31 Examples
32 --------
33 In [1]: print(sys_info())
34 IPython version: 0.11.bzr.r1340 # random
35 BZR revision : 1340
36 Platform info : os.name -> posix, sys.platform -> linux2
37 : Linux-2.6.31-17-generic-i686-with-Ubuntu-9.10-karmic
38 Python info : 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
39 [GCC 4.4.1]
40 """
41 out = []
42 out.append('IPython version: %s' % release.version)
43 out.append('BZR revision : %s' % release.revision)
44 out.append('Platform info : os.name -> %s, sys.platform -> %s' %
45 (os.name,sys.platform) )
46 out.append(' : %s' % platform.platform())
47 out.append('Python info : %s' % sys.version)
48 out.append('') # ensure closing newline
49 return '\n'.join(out)
50
51
52 def _num_cpus_unix():
53 """Return the number of active CPUs on a Unix system."""
54 return os.sysconf("SC_NPROCESSORS_ONLN")
55
56
57 def _num_cpus_darwin():
58 """Return the number of active CPUs on a Darwin system."""
59 p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
60 return p.stdout.read()
61
62
63 def _num_cpus_windows():
64 """Return the number of active CPUs on a Windows system."""
65 return os.environ.get("NUMBER_OF_PROCESSORS")
66
67
68 def num_cpus():
69 """Return the effective number of CPUs in the system as an integer.
70
71 This cross-platform function makes an attempt at finding the total number of
72 available CPUs in the system, as returned by various underlying system and
73 python calls.
74
75 If it can't find a sensible answer, it returns 1 (though an error *may* make
76 it return a large positive number that's actually incorrect).
77 """
78
79 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
80 # for the names of the keys we needed to look up for this function. This
81 # code was inspired by their equivalent function.
82
83 ncpufuncs = {'Linux':_num_cpus_unix,
84 'Darwin':_num_cpus_darwin,
85 'Windows':_num_cpus_windows,
86 # On Vista, python < 2.5.2 has a bug and returns 'Microsoft'
87 # See http://bugs.python.org/issue1082 for details.
88 'Microsoft':_num_cpus_windows,
89 }
90
91 ncpufunc = ncpufuncs.get(platform.system(),
92 # default to unix version (Solaris, AIX, etc)
93 _num_cpus_unix)
94
95 try:
96 ncpus = max(1,int(ncpufunc()))
97 except:
98 ncpus = 1
99 return ncpus
100
@@ -0,0 +1,162 b''
1 # encoding: utf-8
2 """
3 Utilities for working with terminals.
4
5 Authors:
6
7 * Brian E. Granger
8 * Fernando Perez
9 * Alexander Belchenko (e-mail: bialix AT ukr.net)
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 import os
24 import struct
25 import sys
26 import warnings
27
28 #-----------------------------------------------------------------------------
29 # Code
30 #-----------------------------------------------------------------------------
31
32 # This variable is part of the expected API of the module:
33 ignore_termtitle = True
34
35
36 def _term_clear():
37 pass
38
39
40 if os.name == 'posix':
41 def _term_clear():
42 os.system('clear')
43
44
45 if sys.platform == 'win32':
46 def _term_clear():
47 os.system('cls')
48
49
50 def term_clear():
51 _term_clear()
52
53
54 def toggle_set_term_title(val):
55 """Control whether set_term_title is active or not.
56
57 set_term_title() allows writing to the console titlebar. In embedded
58 widgets this can cause problems, so this call can be used to toggle it on
59 or off as needed.
60
61 The default state of the module is for the function to be disabled.
62
63 Parameters
64 ----------
65 val : bool
66 If True, set_term_title() actually writes to the terminal (using the
67 appropriate platform-specific module). If False, it is a no-op.
68 """
69 global ignore_termtitle
70 ignore_termtitle = not(val)
71
72
73 def _set_term_title(*args,**kw):
74 """Dummy no-op."""
75 pass
76
77
78 def _set_term_title_xterm(title):
79 """ Change virtual terminal title in xterm-workalikes """
80 sys.stdout.write('\033]0;%s\007' % title)
81
82 if os.name == 'posix':
83 TERM = os.environ.get('TERM','')
84 if (TERM == 'xterm') or (TERM == 'xterm-color'):
85 _set_term_title = _set_term_title_xterm
86
87
88 if sys.platform == 'win32':
89 try:
90 import ctypes
91
92 SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
93 SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
94
95 def _set_term_title(title):
96 """Set terminal title using ctypes to access the Win32 APIs."""
97 SetConsoleTitleW(title)
98 except ImportError:
99 def _set_term_title(title):
100 """Set terminal title using the 'title' command."""
101 global ignore_termtitle
102
103 try:
104 # Cannot be on network share when issuing system commands
105 curr = os.getcwd()
106 os.chdir("C:")
107 ret = os.system("title " + title)
108 finally:
109 os.chdir(curr)
110 if ret:
111 # non-zero return code signals error, don't try again
112 ignore_termtitle = True
113
114
115 def set_term_title(title):
116 """Set terminal title using the necessary platform-dependent calls."""
117 if ignore_termtitle:
118 return
119 _set_term_title(title)
120
121
122 def freeze_term_title():
123 warnings.warn("This function is deprecated, use toggle_set_term_title()")
124 global ignore_termtitle
125 ignore_termtitle = True
126
127
128 def get_terminal_size(defaultx=80, defaulty=25):
129 return defaultx, defaulty
130
131
132 if sys.platform == 'win32':
133 def get_terminal_size(defaultx=80, defaulty=25):
134 """Return size of current terminal console.
135
136 This function try to determine actual size of current working
137 console window and return tuple (sizex, sizey) if success,
138 or default size (defaultx, defaulty) otherwise.
139
140 Dependencies: ctypes should be installed.
141
142 Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
143 """
144 try:
145 import ctypes
146 except ImportError:
147 return defaultx, defaulty
148
149 h = ctypes.windll.kernel32.GetStdHandle(-11)
150 csbi = ctypes.create_string_buffer(22)
151 res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
152
153 if res:
154 (bufx, bufy, curx, cury, wattr,
155 left, top, right, bottom, maxx, maxy) = struct.unpack(
156 "hhhhHhhhhhh", csbi.raw)
157 sizex = right - left + 1
158 sizey = bottom - top + 1
159 return (sizex, sizey)
160 else:
161 return (defaultx, defaulty)
162
@@ -0,0 +1,61 b''
1 # encoding: utf-8
2 """Tests for io.py"""
3
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2008 The IPython Development Team
6 #
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
14
15 import sys
16
17 from cStringIO import StringIO
18
19 import nose.tools as nt
20
21 from IPython.testing import decorators as dec
22 from IPython.utils.io import Tee
23
24 #-----------------------------------------------------------------------------
25 # Tests
26 #-----------------------------------------------------------------------------
27
28
29 def test_tee_simple():
30 "Very simple check with stdout only"
31 chan = StringIO()
32 text = 'Hello'
33 tee = Tee(chan, channel='stdout')
34 print >> chan, text,
35 nt.assert_equal(chan.getvalue(), text)
36
37
38 class TeeTestCase(dec.ParametricTestCase):
39
40 def tchan(self, channel, check='close'):
41 trap = StringIO()
42 chan = StringIO()
43 text = 'Hello'
44
45 std_ori = getattr(sys, channel)
46 setattr(sys, channel, trap)
47
48 tee = Tee(chan, channel=channel)
49 print >> chan, text,
50 setattr(sys, channel, std_ori)
51 trap_val = trap.getvalue()
52 nt.assert_equals(chan.getvalue(), text)
53 if check=='close':
54 tee.close()
55 else:
56 del tee
57
58 def test(self):
59 for chan in ['stdout', 'stderr']:
60 for check in ['close', 'del']:
61 yield self.tchan(chan, check)
@@ -0,0 +1,473 b''
1 # encoding: utf-8
2 """
3 Utilities for working with strings and text.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import __main__
18
19 import os
20 import re
21 import shutil
22 import types
23
24 from IPython.external.path import path
25
26 from IPython.utils.generics import result_display
27 from IPython.utils.io import nlprint
28 from IPython.utils.data import flatten
29
30 #-----------------------------------------------------------------------------
31 # Code
32 #-----------------------------------------------------------------------------
33
34 StringTypes = types.StringTypes
35
36
37 def unquote_ends(istr):
38 """Remove a single pair of quotes from the endpoints of a string."""
39
40 if not istr:
41 return istr
42 if (istr[0]=="'" and istr[-1]=="'") or \
43 (istr[0]=='"' and istr[-1]=='"'):
44 return istr[1:-1]
45 else:
46 return istr
47
48
49 class LSString(str):
50 """String derivative with a special access attributes.
51
52 These are normal strings, but with the special attributes:
53
54 .l (or .list) : value as list (split on newlines).
55 .n (or .nlstr): original value (the string itself).
56 .s (or .spstr): value as whitespace-separated string.
57 .p (or .paths): list of path objects
58
59 Any values which require transformations are computed only once and
60 cached.
61
62 Such strings are very useful to efficiently interact with the shell, which
63 typically only understands whitespace-separated options for commands."""
64
65 def get_list(self):
66 try:
67 return self.__list
68 except AttributeError:
69 self.__list = self.split('\n')
70 return self.__list
71
72 l = list = property(get_list)
73
74 def get_spstr(self):
75 try:
76 return self.__spstr
77 except AttributeError:
78 self.__spstr = self.replace('\n',' ')
79 return self.__spstr
80
81 s = spstr = property(get_spstr)
82
83 def get_nlstr(self):
84 return self
85
86 n = nlstr = property(get_nlstr)
87
88 def get_paths(self):
89 try:
90 return self.__paths
91 except AttributeError:
92 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
93 return self.__paths
94
95 p = paths = property(get_paths)
96
97
98 def print_lsstring(arg):
99 """ Prettier (non-repr-like) and more informative printer for LSString """
100 print "LSString (.p, .n, .l, .s available). Value:"
101 print arg
102
103
104 print_lsstring = result_display.when_type(LSString)(print_lsstring)
105
106
107 class SList(list):
108 """List derivative with a special access attributes.
109
110 These are normal lists, but with the special attributes:
111
112 .l (or .list) : value as list (the list itself).
113 .n (or .nlstr): value as a string, joined on newlines.
114 .s (or .spstr): value as a string, joined on spaces.
115 .p (or .paths): list of path objects
116
117 Any values which require transformations are computed only once and
118 cached."""
119
120 def get_list(self):
121 return self
122
123 l = list = property(get_list)
124
125 def get_spstr(self):
126 try:
127 return self.__spstr
128 except AttributeError:
129 self.__spstr = ' '.join(self)
130 return self.__spstr
131
132 s = spstr = property(get_spstr)
133
134 def get_nlstr(self):
135 try:
136 return self.__nlstr
137 except AttributeError:
138 self.__nlstr = '\n'.join(self)
139 return self.__nlstr
140
141 n = nlstr = property(get_nlstr)
142
143 def get_paths(self):
144 try:
145 return self.__paths
146 except AttributeError:
147 self.__paths = [path(p) for p in self if os.path.exists(p)]
148 return self.__paths
149
150 p = paths = property(get_paths)
151
152 def grep(self, pattern, prune = False, field = None):
153 """ Return all strings matching 'pattern' (a regex or callable)
154
155 This is case-insensitive. If prune is true, return all items
156 NOT matching the pattern.
157
158 If field is specified, the match must occur in the specified
159 whitespace-separated field.
160
161 Examples::
162
163 a.grep( lambda x: x.startswith('C') )
164 a.grep('Cha.*log', prune=1)
165 a.grep('chm', field=-1)
166 """
167
168 def match_target(s):
169 if field is None:
170 return s
171 parts = s.split()
172 try:
173 tgt = parts[field]
174 return tgt
175 except IndexError:
176 return ""
177
178 if isinstance(pattern, basestring):
179 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
180 else:
181 pred = pattern
182 if not prune:
183 return SList([el for el in self if pred(match_target(el))])
184 else:
185 return SList([el for el in self if not pred(match_target(el))])
186
187 def fields(self, *fields):
188 """ Collect whitespace-separated fields from string list
189
190 Allows quick awk-like usage of string lists.
191
192 Example data (in var a, created by 'a = !ls -l')::
193 -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
194 drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
195
196 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
197 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
198 (note the joining by space).
199 a.fields(-1) is ['ChangeLog', 'IPython']
200
201 IndexErrors are ignored.
202
203 Without args, fields() just split()'s the strings.
204 """
205 if len(fields) == 0:
206 return [el.split() for el in self]
207
208 res = SList()
209 for el in [f.split() for f in self]:
210 lineparts = []
211
212 for fd in fields:
213 try:
214 lineparts.append(el[fd])
215 except IndexError:
216 pass
217 if lineparts:
218 res.append(" ".join(lineparts))
219
220 return res
221
222 def sort(self,field= None, nums = False):
223 """ sort by specified fields (see fields())
224
225 Example::
226 a.sort(1, nums = True)
227
228 Sorts a by second field, in numerical order (so that 21 > 3)
229
230 """
231
232 #decorate, sort, undecorate
233 if field is not None:
234 dsu = [[SList([line]).fields(field), line] for line in self]
235 else:
236 dsu = [[line, line] for line in self]
237 if nums:
238 for i in range(len(dsu)):
239 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
240 try:
241 n = int(numstr)
242 except ValueError:
243 n = 0;
244 dsu[i][0] = n
245
246
247 dsu.sort()
248 return SList([t[1] for t in dsu])
249
250
251 def print_slist(arg):
252 """ Prettier (non-repr-like) and more informative printer for SList """
253 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
254 if hasattr(arg, 'hideonce') and arg.hideonce:
255 arg.hideonce = False
256 return
257
258 nlprint(arg)
259
260
261 print_slist = result_display.when_type(SList)(print_slist)
262
263
264 def esc_quotes(strng):
265 """Return the input string with single and double quotes escaped out"""
266
267 return strng.replace('"','\\"').replace("'","\\'")
268
269
270 def make_quoted_expr(s):
271 """Return string s in appropriate quotes, using raw string if possible.
272
273 XXX - example removed because it caused encoding errors in documentation
274 generation. We need a new example that doesn't contain invalid chars.
275
276 Note the use of raw string and padding at the end to allow trailing
277 backslash.
278 """
279
280 tail = ''
281 tailpadding = ''
282 raw = ''
283 if "\\" in s:
284 raw = 'r'
285 if s.endswith('\\'):
286 tail = '[:-1]'
287 tailpadding = '_'
288 if '"' not in s:
289 quote = '"'
290 elif "'" not in s:
291 quote = "'"
292 elif '"""' not in s and not s.endswith('"'):
293 quote = '"""'
294 elif "'''" not in s and not s.endswith("'"):
295 quote = "'''"
296 else:
297 # give up, backslash-escaped string will do
298 return '"%s"' % esc_quotes(s)
299 res = raw + quote + s + tailpadding + quote + tail
300 return res
301
302
303 def qw(words,flat=0,sep=None,maxsplit=-1):
304 """Similar to Perl's qw() operator, but with some more options.
305
306 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
307
308 words can also be a list itself, and with flat=1, the output will be
309 recursively flattened.
310
311 Examples:
312
313 >>> qw('1 2')
314 ['1', '2']
315
316 >>> qw(['a b','1 2',['m n','p q']])
317 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
318
319 >>> qw(['a b','1 2',['m n','p q']],flat=1)
320 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
321 """
322
323 if type(words) in StringTypes:
324 return [word.strip() for word in words.split(sep,maxsplit)
325 if word and not word.isspace() ]
326 if flat:
327 return flatten(map(qw,words,[1]*len(words)))
328 return map(qw,words)
329
330
331 def qwflat(words,sep=None,maxsplit=-1):
332 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
333 return qw(words,1,sep,maxsplit)
334
335
336 def qw_lol(indata):
337 """qw_lol('a b') -> [['a','b']],
338 otherwise it's just a call to qw().
339
340 We need this to make sure the modules_some keys *always* end up as a
341 list of lists."""
342
343 if type(indata) in StringTypes:
344 return [qw(indata)]
345 else:
346 return qw(indata)
347
348
349 def grep(pat,list,case=1):
350 """Simple minded grep-like function.
351 grep(pat,list) returns occurrences of pat in list, None on failure.
352
353 It only does simple string matching, with no support for regexps. Use the
354 option case=0 for case-insensitive matching."""
355
356 # This is pretty crude. At least it should implement copying only references
357 # to the original data in case it's big. Now it copies the data for output.
358 out=[]
359 if case:
360 for term in list:
361 if term.find(pat)>-1: out.append(term)
362 else:
363 lpat=pat.lower()
364 for term in list:
365 if term.lower().find(lpat)>-1: out.append(term)
366
367 if len(out): return out
368 else: return None
369
370
371 def dgrep(pat,*opts):
372 """Return grep() on dir()+dir(__builtins__).
373
374 A very common use of grep() when working interactively."""
375
376 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
377
378
379 def idgrep(pat):
380 """Case-insensitive dgrep()"""
381
382 return dgrep(pat,0)
383
384
385 def igrep(pat,list):
386 """Synonym for case-insensitive grep."""
387
388 return grep(pat,list,case=0)
389
390
391 def indent(str,nspaces=4,ntabs=0):
392 """Indent a string a given number of spaces or tabstops.
393
394 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
395 """
396 if str is None:
397 return
398 ind = '\t'*ntabs+' '*nspaces
399 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
400 if outstr.endswith(os.linesep+ind):
401 return outstr[:-len(ind)]
402 else:
403 return outstr
404
405 def native_line_ends(filename,backup=1):
406 """Convert (in-place) a file to line-ends native to the current OS.
407
408 If the optional backup argument is given as false, no backup of the
409 original file is left. """
410
411 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
412
413 bak_filename = filename + backup_suffixes[os.name]
414
415 original = open(filename).read()
416 shutil.copy2(filename,bak_filename)
417 try:
418 new = open(filename,'wb')
419 new.write(os.linesep.join(original.splitlines()))
420 new.write(os.linesep) # ALWAYS put an eol at the end of the file
421 new.close()
422 except:
423 os.rename(bak_filename,filename)
424 if not backup:
425 try:
426 os.remove(bak_filename)
427 except:
428 pass
429
430
431 def list_strings(arg):
432 """Always return a list of strings, given a string or list of strings
433 as input.
434
435 :Examples:
436
437 In [7]: list_strings('A single string')
438 Out[7]: ['A single string']
439
440 In [8]: list_strings(['A single string in a list'])
441 Out[8]: ['A single string in a list']
442
443 In [9]: list_strings(['A','list','of','strings'])
444 Out[9]: ['A', 'list', 'of', 'strings']
445 """
446
447 if isinstance(arg,basestring): return [arg]
448 else: return arg
449
450
451 def marquee(txt='',width=78,mark='*'):
452 """Return the input string centered in a 'marquee'.
453
454 :Examples:
455
456 In [16]: marquee('A test',40)
457 Out[16]: '**************** A test ****************'
458
459 In [17]: marquee('A test',40,'-')
460 Out[17]: '---------------- A test ----------------'
461
462 In [18]: marquee('A test',40,' ')
463 Out[18]: ' A test '
464
465 """
466 if not txt:
467 return (mark*width)[:width]
468 nmark = (width-len(txt)-2)/len(mark)/2
469 if nmark < 0: nmark =0
470 marks = mark*nmark
471 return '%s %s %s' % (marks,txt,marks)
472
473
@@ -0,0 +1,116 b''
1 # encoding: utf-8
2 """
3 Utilities for timing code execution.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import time
18
19 #-----------------------------------------------------------------------------
20 # Code
21 #-----------------------------------------------------------------------------
22
23 # If possible (Unix), use the resource module instead of time.clock()
24 try:
25 import resource
26 def clocku():
27 """clocku() -> floating point number
28
29 Return the *USER* CPU time in seconds since the start of the process.
30 This is done via a call to resource.getrusage, so it avoids the
31 wraparound problems in time.clock()."""
32
33 return resource.getrusage(resource.RUSAGE_SELF)[0]
34
35 def clocks():
36 """clocks() -> floating point number
37
38 Return the *SYSTEM* CPU time in seconds since the start of the process.
39 This is done via a call to resource.getrusage, so it avoids the
40 wraparound problems in time.clock()."""
41
42 return resource.getrusage(resource.RUSAGE_SELF)[1]
43
44 def clock():
45 """clock() -> floating point number
46
47 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
48 the process. This is done via a call to resource.getrusage, so it
49 avoids the wraparound problems in time.clock()."""
50
51 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
52 return u+s
53
54 def clock2():
55 """clock2() -> (t_user,t_system)
56
57 Similar to clock(), but return a tuple of user/system times."""
58 return resource.getrusage(resource.RUSAGE_SELF)[:2]
59 except ImportError:
60 # There is no distinction of user/system time under windows, so we just use
61 # time.clock() for everything...
62 clocku = clocks = clock = time.clock
63 def clock2():
64 """Under windows, system CPU time can't be measured.
65
66 This just returns clock() and zero."""
67 return time.clock(),0.0
68
69
70 def timings_out(reps,func,*args,**kw):
71 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
72
73 Execute a function reps times, return a tuple with the elapsed total
74 CPU time in seconds, the time per call and the function's output.
75
76 Under Unix, the return value is the sum of user+system time consumed by
77 the process, computed via the resource module. This prevents problems
78 related to the wraparound effect which the time.clock() function has.
79
80 Under Windows the return value is in wall clock seconds. See the
81 documentation for the time module for more details."""
82
83 reps = int(reps)
84 assert reps >=1, 'reps must be >= 1'
85 if reps==1:
86 start = clock()
87 out = func(*args,**kw)
88 tot_time = clock()-start
89 else:
90 rng = xrange(reps-1) # the last time is executed separately to store output
91 start = clock()
92 for dummy in rng: func(*args,**kw)
93 out = func(*args,**kw) # one last time
94 tot_time = clock()-start
95 av_time = tot_time / reps
96 return tot_time,av_time,out
97
98
99 def timings(reps,func,*args,**kw):
100 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
101
102 Execute a function reps times, return a tuple with the elapsed total CPU
103 time in seconds and the time per call. These are just the first two values
104 in timings_out()."""
105
106 return timings_out(reps,func,*args,**kw)[0:2]
107
108
109 def timing(func,*args,**kw):
110 """timing(func,*args,**kw) -> t_total
111
112 Execute a function once, return the elapsed total CPU time in
113 seconds. This is just the first value in timings_out()."""
114
115 return timings_out(1,func,*args,**kw)[0]
116
@@ -0,0 +1,66 b''
1 # encoding: utf-8
2 """
3 Utilities for warnings. Shoudn't we just use the built in warnings module.
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import sys
18
19 from IPython.utils.io import Term
20
21 #-----------------------------------------------------------------------------
22 # Code
23 #-----------------------------------------------------------------------------
24
25 def warn(msg,level=2,exit_val=1):
26 """Standard warning printer. Gives formatting consistency.
27
28 Output is sent to Term.cerr (sys.stderr by default).
29
30 Options:
31
32 -level(2): allows finer control:
33 0 -> Do nothing, dummy function.
34 1 -> Print message.
35 2 -> Print 'WARNING:' + message. (Default level).
36 3 -> Print 'ERROR:' + message.
37 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
38
39 -exit_val (1): exit value returned by sys.exit() for a level 4
40 warning. Ignored for all other levels."""
41
42 if level>0:
43 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
44 print >> Term.cerr, '%s%s' % (header[level],msg)
45 if level == 4:
46 print >> Term.cerr,'Exiting.\n'
47 sys.exit(exit_val)
48
49
50 def info(msg):
51 """Equivalent to warn(msg,level=1)."""
52
53 warn(msg,level=1)
54
55
56 def error(msg):
57 """Equivalent to warn(msg,level=3)."""
58
59 warn(msg,level=3)
60
61
62 def fatal(msg,exit_val=1):
63 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
64
65 warn(msg,exit_val=exit_val,level=4)
66
@@ -30,14 +30,14 b" if sys.version[0:3] < '2.5':"
30
30
31
31
32 # Make it easy to import extensions - they are always directly on pythonpath.
32 # Make it easy to import extensions - they are always directly on pythonpath.
33 # Therefore, non-IPython modules can be added to extensions directory
33 # Therefore, non-IPython modules can be added to extensions directory.
34 # This should probably be in ipapp.py.
34 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
35 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
35
36
36 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
37 # Setup the top level names
38 # Setup the top level names
38 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
39
40
40 # In some cases, these are causing circular imports.
41 from .config.loader import Config
41 from .config.loader import Config
42 from .core import release
42 from .core import release
43 from .core.application import Application
43 from .core.application import Application
@@ -23,7 +23,7 b' import os'
23 import sys
23 import sys
24
24
25 from IPython.external import argparse
25 from IPython.external import argparse
26 from IPython.utils.genutils import filefind
26 from IPython.utils.path import filefind
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Exceptions
29 # Exceptions
@@ -28,9 +28,9 b' import sys'
28 from IPython.core.component import Component
28 from IPython.core.component import Component
29 from IPython.core.splitinput import split_user_input
29 from IPython.core.splitinput import split_user_input
30
30
31 from IPython.utils.traitlets import CBool, List, Instance
31 from IPython.utils.traitlets import List
32 from IPython.utils.genutils import error
33 from IPython.utils.autoattr import auto_attr
32 from IPython.utils.autoattr import auto_attr
33 from IPython.utils.warn import warn, error
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Utilities
36 # Utilities
@@ -33,7 +33,7 b' import os'
33 import sys
33 import sys
34
34
35 from IPython.core import release, crashhandler
35 from IPython.core import release, crashhandler
36 from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
36 from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
37 from IPython.config.loader import (
37 from IPython.config.loader import (
38 PyFileConfigLoader,
38 PyFileConfigLoader,
39 ArgParseConfigLoader,
39 ArgParseConfigLoader,
@@ -69,19 +69,20 b' used, and this module (and the readline module) are silently inactive.'
69 import __builtin__
69 import __builtin__
70 import __main__
70 import __main__
71 import glob
71 import glob
72 import inspect
72 import itertools
73 import itertools
73 import keyword
74 import keyword
74 import os
75 import os
75 import re
76 import re
76 import shlex
77 import shlex
77 import sys
78 import sys
78 import types
79
79
80 import IPython.utils.rlineimpl as readline
81 from IPython.core.error import TryNext
80 from IPython.core.error import TryNext
82 from IPython.core.prefilter import ESC_MAGIC
81 from IPython.core.prefilter import ESC_MAGIC
83 from IPython.utils import generics
82 from IPython.utils import generics
84 from IPython.utils.genutils import debugx, dir2
83 from IPython.utils.frame import debugx
84 from IPython.utils.dir2 import dir2
85 import IPython.utils.rlineimpl as readline
85
86
86 #-----------------------------------------------------------------------------
87 #-----------------------------------------------------------------------------
87 # Globals
88 # Globals
@@ -216,7 +217,6 b' class Completer:'
216 with a __getattr__ hook is evaluated.
217 with a __getattr__ hook is evaluated.
217
218
218 """
219 """
219 import re
220
220
221 #print 'Completer->attr_matches, txt=%r' % text # dbg
221 #print 'Completer->attr_matches, txt=%r' % text # dbg
222 # Another option, seems to work great. Catches things like ''.<tab>
222 # Another option, seems to work great. Catches things like ''.<tab>
@@ -27,7 +27,7 b' from weakref import WeakValueDictionary'
27 from IPython.utils.importstring import import_item
27 from IPython.utils.importstring import import_item
28 from IPython.config.loader import Config
28 from IPython.config.loader import Config
29 from IPython.utils.traitlets import (
29 from IPython.utils.traitlets import (
30 HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This
30 HasTraitlets, MetaHasTraitlets, Instance, This
31 )
31 )
32
32
33
33
@@ -26,7 +26,7 b' from pprint import pformat'
26 # Our own
26 # Our own
27 from IPython.core import release
27 from IPython.core import release
28 from IPython.core import ultratb
28 from IPython.core import ultratb
29 from IPython.utils.genutils import sys_info
29 from IPython.utils.sysinfo import sys_info
30
30
31 from IPython.external.Itpl import itpl
31 from IPython.external.Itpl import itpl
32
32
@@ -26,15 +26,13 b' http://www.python.org/2.2.3/license.html"""'
26 #*****************************************************************************
26 #*****************************************************************************
27
27
28 import bdb
28 import bdb
29 import cmd
30 import linecache
29 import linecache
31 import os
32 import sys
30 import sys
33
31
34 from IPython.utils import PyColorize
32 from IPython.utils import PyColorize
35 from IPython.core import ipapi
33 from IPython.core import ipapi
36 from IPython.utils import coloransi
34 from IPython.utils import coloransi
37 from IPython.utils.genutils import Term
35 from IPython.utils.io import Term
38 from IPython.core.excolors import exception_colors
36 from IPython.core.excolors import exception_colors
39
37
40 # See if we can use pydb.
38 # See if we can use pydb.
@@ -24,8 +24,6 b' import sys'
24
24
25 from IPython.core.component import Component
25 from IPython.core.component import Component
26
26
27 from IPython.utils.autoattr import auto_attr
28
29 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
30 # Classes and functions
28 # Classes and functions
31 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
@@ -24,6 +24,7 b' Notes'
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 from __future__ import with_statement
26 from __future__ import with_statement
27 import __main__
27
28
28 import sys
29 import sys
29 from contextlib import nested
30 from contextlib import nested
@@ -33,7 +34,7 b' from IPython.core.iplib import InteractiveShell'
33 from IPython.core.ipapp import load_default_config
34 from IPython.core.ipapp import load_default_config
34
35
35 from IPython.utils.traitlets import Bool, Str, CBool
36 from IPython.utils.traitlets import Bool, Str, CBool
36 from IPython.utils.genutils import ask_yes_no
37 from IPython.utils.io import ask_yes_no
37
38
38
39
39 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
@@ -10,8 +10,6 b' Color schemes for exception handling code in IPython.'
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
11 #*****************************************************************************
12
12
13 #****************************************************************************
14 # Required modules
15 from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme
13 from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme
16
14
17 def exception_colors():
15 def exception_colors():
@@ -5,7 +5,8 b''
5 import fnmatch
5 import fnmatch
6 import os
6 import os
7
7
8 from IPython.utils.genutils import Term, ask_yes_no, warn
8 from IPython.utils.io import Term, ask_yes_no
9 from IPython.utils.warn import warn
9 from IPython.core import ipapi
10 from IPython.core import ipapi
10
11
11 def magic_history(self, parameter_s = ''):
12 def magic_history(self, parameter_s = ''):
@@ -43,9 +43,12 b' somewhere in your configuration files or ipython command line.'
43
43
44 import os, bisect
44 import os, bisect
45 import sys
45 import sys
46 from IPython.utils.genutils import Term, shell
46
47 from pprint import PrettyPrinter
47 from pprint import PrettyPrinter
48
48
49 from IPython.utils.io import Term
50 from IPython.utils.process import shell
51
49 from IPython.core.error import TryNext
52 from IPython.core.error import TryNext
50
53
51 # List here all the default hooks. For now it's just the editor functions
54 # List here all the default hooks. For now it's just the editor functions
@@ -18,8 +18,6 b' has been made into a component, this module will be sent to deathrow.'
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 from IPython.core.error import TryNext, UsageError, IPythonCoreError
22
23 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
24 # Classes and functions
22 # Classes and functions
25 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
@@ -36,7 +36,7 b' from IPython.config.loader import ('
36 # NoConfigDefault,
36 # NoConfigDefault,
37 )
37 )
38 from IPython.lib import inputhook
38 from IPython.lib import inputhook
39 from IPython.utils.genutils import filefind, get_ipython_dir
39 from IPython.utils.path import filefind, get_ipython_dir
40 from . import usage
40 from . import usage
41
41
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
@@ -499,7 +499,6 b' class IPythonApp(Application):'
499 self._run_exec_lines()
499 self._run_exec_lines()
500 self._run_exec_files()
500 self._run_exec_files()
501 self._run_cmd_line_code()
501 self._run_cmd_line_code()
502 self._configure_xmode()
503
502
504 def _enable_gui_pylab(self):
503 def _enable_gui_pylab(self):
505 """Enable GUI event loop integration, taking pylab into account."""
504 """Enable GUI event loop integration, taking pylab into account."""
@@ -624,11 +623,6 b' class IPythonApp(Application):'
624 self.log.warn("Error in executing file in user namespace: %s" % fname)
623 self.log.warn("Error in executing file in user namespace: %s" % fname)
625 self.shell.showtraceback()
624 self.shell.showtraceback()
626
625
627 def _configure_xmode(self):
628 # XXX - shouldn't this be read from the config? I'm still a little
629 # lost with all the details of handling the new config guys...
630 self.shell.InteractiveTB.set_mode(mode=self.shell.xmode)
631
632 def start_app(self):
626 def start_app(self):
633 if self.master_config.Global.interact:
627 if self.master_config.Global.interact:
634 self.log.debug("Starting IPython's mainloop...")
628 self.log.debug("Starting IPython's mainloop...")
@@ -20,7 +20,6 b' from __future__ import with_statement'
20 from __future__ import absolute_import
20 from __future__ import absolute_import
21
21
22 import __builtin__
22 import __builtin__
23 import StringIO
24 import bdb
23 import bdb
25 import codeop
24 import codeop
26 import exceptions
25 import exceptions
@@ -47,29 +46,35 b' from IPython.core.logger import Logger'
47 from IPython.core.magic import Magic
46 from IPython.core.magic import Magic
48 from IPython.core.prefilter import PrefilterManager
47 from IPython.core.prefilter import PrefilterManager
49 from IPython.core.prompts import CachedOutput
48 from IPython.core.prompts import CachedOutput
50 from IPython.core.pylabtools import pylab_activate
51 from IPython.core.usage import interactive_usage, default_banner
49 from IPython.core.usage import interactive_usage, default_banner
50 import IPython.core.hooks
52 from IPython.external.Itpl import ItplNS
51 from IPython.external.Itpl import ItplNS
53 from IPython.lib.inputhook import enable_gui
52 from IPython.lib.inputhook import enable_gui
54 from IPython.lib.backgroundjobs import BackgroundJobManager
53 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.lib.pylabtools import pylab_activate
55 from IPython.utils import PyColorize
55 from IPython.utils import PyColorize
56 from IPython.utils import pickleshare
56 from IPython.utils import pickleshare
57 from IPython.utils.genutils import get_ipython_dir
57 from IPython.utils.doctestreload import doctest_reload
58 from IPython.utils.ipstruct import Struct
58 from IPython.utils.ipstruct import Struct
59 from IPython.utils.platutils import toggle_set_term_title, set_term_title
59 from IPython.utils.io import Term, ask_yes_no
60 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
61 from IPython.utils.process import (
62 abbrev_cwd,
63 getoutput,
64 getoutputerror
65 )
66 # import IPython.utils.rlineimpl as readline
60 from IPython.utils.strdispatch import StrDispatch
67 from IPython.utils.strdispatch import StrDispatch
61 from IPython.utils.syspathcontext import prepended_to_syspath
68 from IPython.utils.syspathcontext import prepended_to_syspath
62
69 from IPython.utils.terminal import toggle_set_term_title, set_term_title
63 # XXX - need to clean up this import * line
70 from IPython.utils.warn import warn, error, fatal
64 from IPython.utils.genutils import *
65
66 # from IPython.utils import growl
67 # growl.start("IPython")
68
69 from IPython.utils.traitlets import (
71 from IPython.utils.traitlets import (
70 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
72 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
71 )
73 )
72
74
75 # from IPython.utils import growl
76 # growl.start("IPython")
77
73 #-----------------------------------------------------------------------------
78 #-----------------------------------------------------------------------------
74 # Globals
79 # Globals
75 #-----------------------------------------------------------------------------
80 #-----------------------------------------------------------------------------
@@ -658,7 +663,6 b' class InteractiveShell(Component, Magic):'
658 self.strdispatchers = {}
663 self.strdispatchers = {}
659
664
660 # Set all default hooks, defined in the IPython.hooks module.
665 # Set all default hooks, defined in the IPython.hooks module.
661 import IPython.core.hooks
662 hooks = IPython.core.hooks
666 hooks = IPython.core.hooks
663 for hook_name in hooks.__all__:
667 for hook_name in hooks.__all__:
664 # default hooks have priority 100, i.e. low; user hooks should have
668 # default hooks have priority 100, i.e. low; user hooks should have
@@ -1164,7 +1168,9 b' class InteractiveShell(Component, Magic):'
1164 Convert func into callable that saves & restores
1168 Convert func into callable that saves & restores
1165 history around the call """
1169 history around the call """
1166
1170
1167 if not self.has_readline:
1171 if self.has_readline:
1172 from IPython.utils import rlineimpl as readline
1173 else:
1168 return func
1174 return func
1169
1175
1170 def wrapper():
1176 def wrapper():
@@ -1198,6 +1204,9 b' class InteractiveShell(Component, Magic):'
1198 # and add any custom exception handlers the user may have specified
1204 # and add any custom exception handlers the user may have specified
1199 self.set_custom_exc(*custom_exceptions)
1205 self.set_custom_exc(*custom_exceptions)
1200
1206
1207 # Set the exception mode
1208 self.InteractiveTB.set_mode(mode=self.xmode)
1209
1201 def set_custom_exc(self,exc_tuple,handler):
1210 def set_custom_exc(self,exc_tuple,handler):
1202 """set_custom_exc(exc_tuple,handler)
1211 """set_custom_exc(exc_tuple,handler)
1203
1212
@@ -7,7 +7,7 b''
7 # the file COPYING, distributed as part of this software.
7 # the file COPYING, distributed as part of this software.
8 #*****************************************************************************
8 #*****************************************************************************
9
9
10 from IPython.utils.genutils import Term
10 from IPython.utils.io import Term
11 from IPython.core.autocall import IPyAutocall
11 from IPython.core.autocall import IPyAutocall
12
12
13 class Macro(IPyAutocall):
13 class Macro(IPyAutocall):
@@ -1,35 +1,33 b''
1 # -*- coding: utf-8 -*-
1 # encoding: utf-8
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3 """
3 """
4
4
5 #*****************************************************************************
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 #
8 # Copyright (C) 2008-2009 The IPython Development Team
9
9 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
12 #-----------------------------------------------------------------------------
12
13
13 #****************************************************************************
14 #-----------------------------------------------------------------------------
14 # Modules and globals
15 # Imports
16 #-----------------------------------------------------------------------------
15
17
16 # Python standard modules
17 import __builtin__
18 import __builtin__
18 import bdb
19 import bdb
19 import inspect
20 import inspect
20 import os
21 import os
21 import pdb
22 import pydoc
23 import sys
22 import sys
24 import shutil
23 import shutil
25 import re
24 import re
26 import tempfile
27 import time
25 import time
28 import cPickle as pickle
29 import textwrap
26 import textwrap
27 import types
30 from cStringIO import StringIO
28 from cStringIO import StringIO
31 from getopt import getopt,GetoptError
29 from getopt import getopt,GetoptError
32 from pprint import pprint, pformat
30 from pprint import pformat
33
31
34 # cProfile was added in Python2.5
32 # cProfile was added in Python2.5
35 try:
33 try:
@@ -42,10 +40,7 b' except ImportError:'
42 except ImportError:
40 except ImportError:
43 profile = pstats = None
41 profile = pstats = None
44
42
45 # Homebrewed
46 import IPython
43 import IPython
47 import IPython.utils.generics
48
49 from IPython.core import debugger, oinspect
44 from IPython.core import debugger, oinspect
50 from IPython.core.error import TryNext
45 from IPython.core.error import TryNext
51 from IPython.core.error import UsageError
46 from IPython.core.error import UsageError
@@ -53,20 +48,24 b' from IPython.core.fakemodule import FakeModule'
53 from IPython.core.macro import Macro
48 from IPython.core.macro import Macro
54 from IPython.core.page import page
49 from IPython.core.page import page
55 from IPython.core.prefilter import ESC_MAGIC
50 from IPython.core.prefilter import ESC_MAGIC
56 from IPython.core.pylabtools import mpl_runner
51 from IPython.lib.pylabtools import mpl_runner
57 from IPython.lib.inputhook import enable_gui
52 from IPython.lib.inputhook import enable_gui
58 from IPython.external.Itpl import Itpl, itpl, printpl,itplns
53 from IPython.external.Itpl import itpl, printpl
59 from IPython.testing import decorators as testdec
54 from IPython.testing import decorators as testdec
60 from IPython.utils import platutils
55 from IPython.utils.io import Term, file_read, nlprint
61 from IPython.utils import wildcard
56 from IPython.utils.path import get_py_filename
62 from IPython.utils.PyColorize import Parser
57 from IPython.utils.process import arg_split, abbrev_cwd
58 from IPython.utils.terminal import set_term_title
59 from IPython.utils.text import LSString, SList, StringTypes
60 from IPython.utils.timing import clock, clock2
61 from IPython.utils.warn import warn, error
63 from IPython.utils.ipstruct import Struct
62 from IPython.utils.ipstruct import Struct
63 import IPython.utils.generics
64
64
65 # XXX - We need to switch to explicit imports here with genutils
65 #-----------------------------------------------------------------------------
66 from IPython.utils.genutils import *
67
68 #***************************************************************************
69 # Utility functions
66 # Utility functions
67 #-----------------------------------------------------------------------------
68
70 def on_off(tag):
69 def on_off(tag):
71 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
70 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
72 return ['OFF','ON'][tag]
71 return ['OFF','ON'][tag]
@@ -94,6 +93,9 b' def compress_dhist(dh):'
94 # on construction of the main InteractiveShell object. Something odd is going
93 # on construction of the main InteractiveShell object. Something odd is going
95 # on with super() calls, Component and the MRO... For now leave it as-is, but
94 # on with super() calls, Component and the MRO... For now leave it as-is, but
96 # eventually this needs to be clarified.
95 # eventually this needs to be clarified.
96 # BG: This is because InteractiveShell inherits from this, but is itself a
97 # Component. This messes up the MRO in some way. The fix is that we need to
98 # make Magic a component that InteractiveShell does not subclass.
97
99
98 class Magic:
100 class Magic:
99 """Magic functions for InteractiveShell.
101 """Magic functions for InteractiveShell.
@@ -277,7 +279,7 b' python-profiler package from non-free.""")'
277 def arg_err(self,func):
279 def arg_err(self,func):
278 """Print docstring if incorrect arguments were passed"""
280 """Print docstring if incorrect arguments were passed"""
279 print 'Error in arguments:'
281 print 'Error in arguments:'
280 print OInspect.getdoc(func)
282 print oinspect.getdoc(func)
281
283
282 def format_latex(self,strng):
284 def format_latex(self,strng):
283 """Format a string for latex inclusion."""
285 """Format a string for latex inclusion."""
@@ -1170,7 +1172,7 b' Currently the magic system has the following functions:\\n"""'
1170 started = logger.logstart(logfname,loghead,logmode,
1172 started = logger.logstart(logfname,loghead,logmode,
1171 log_output,timestamp,log_raw_input)
1173 log_output,timestamp,log_raw_input)
1172 except:
1174 except:
1173 rc.opts.logfile = old_logfile
1175 self.shell.logfile = old_logfile
1174 warn("Couldn't start log: %s" % sys.exc_info()[1])
1176 warn("Couldn't start log: %s" % sys.exc_info()[1])
1175 else:
1177 else:
1176 # log input history up to this point, optionally interleaving
1178 # log input history up to this point, optionally interleaving
@@ -2811,7 +2813,7 b' Defaulting color scheme to \'NoColor\'"""'
2811 try:
2813 try:
2812 os.chdir(os.path.expanduser(ps))
2814 os.chdir(os.path.expanduser(ps))
2813 if self.shell.term_title:
2815 if self.shell.term_title:
2814 platutils.set_term_title('IPython: ' + abbrev_cwd())
2816 set_term_title('IPython: ' + abbrev_cwd())
2815 except OSError:
2817 except OSError:
2816 print sys.exc_info()[1]
2818 print sys.exc_info()[1]
2817 else:
2819 else:
@@ -2824,7 +2826,7 b' Defaulting color scheme to \'NoColor\'"""'
2824 else:
2826 else:
2825 os.chdir(self.shell.home_dir)
2827 os.chdir(self.shell.home_dir)
2826 if self.shell.term_title:
2828 if self.shell.term_title:
2827 platutils.set_term_title('IPython: ' + '~')
2829 set_term_title('IPython: ' + '~')
2828 cwd = os.getcwd()
2830 cwd = os.getcwd()
2829 dhist = self.shell.user_ns['_dh']
2831 dhist = self.shell.user_ns['_dh']
2830
2832
@@ -27,10 +27,11 b' import sys'
27 import types
27 import types
28
28
29 # IPython's own
29 # IPython's own
30 from IPython.utils import PyColorize
31 from IPython.utils.genutils import indent, Term
32 from IPython.core.page import page
30 from IPython.core.page import page
33 from IPython.external.Itpl import itpl
31 from IPython.external.Itpl import itpl
32 from IPython.utils import PyColorize
33 from IPython.utils.io import Term
34 from IPython.utils.text import indent
34 from IPython.utils.wildcard import list_namespace
35 from IPython.utils.wildcard import list_namespace
35 from IPython.utils.coloransi import *
36 from IPython.utils.coloransi import *
36
37
@@ -30,15 +30,15 b' rid of that dependency, we could move it there.'
30 import os
30 import os
31 import re
31 import re
32 import sys
32 import sys
33 import tempfile
33
34
34 from IPython.core import ipapi
35 from IPython.core import ipapi
35 from IPython.core.error import TryNext
36 from IPython.core.error import TryNext
36 from IPython.utils.genutils import (
37 from IPython.utils.cursesimport import use_curses
37 chop, Term, USE_CURSES
38 from IPython.utils.data import chop
38 )
39 from IPython.utils.io import Term
39
40 from IPython.utils.process import xsys
40 if os.name == "nt":
41 from IPython.utils.terminal import get_terminal_size
41 from IPython.utils.winconsole import get_console_size
42
42
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
@@ -47,7 +47,7 b' if os.name == "nt":'
47
47
48 esc_re = re.compile(r"(\x1b[^m]+m)")
48 esc_re = re.compile(r"(\x1b[^m]+m)")
49
49
50 def page_dumb(strng,start=0,screen_lines=25):
50 def page_dumb(strng, start=0, screen_lines=25):
51 """Very dumb 'pager' in Python, for when nothing else works.
51 """Very dumb 'pager' in Python, for when nothing else works.
52
52
53 Only moves forward, same interface as page(), except for pager_cmd and
53 Only moves forward, same interface as page(), except for pager_cmd and
@@ -69,8 +69,8 b' def page_dumb(strng,start=0,screen_lines=25):'
69 last_escape = esc_list[-1]
69 last_escape = esc_list[-1]
70 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
70 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
71
71
72 #----------------------------------------------------------------------------
72
73 def page(strng,start=0,screen_lines=0,pager_cmd = None):
73 def page(strng, start=0, screen_lines=0, pager_cmd=None):
74 """Print a string, piping through a pager after a certain length.
74 """Print a string, piping through a pager after a certain length.
75
75
76 The screen_lines parameter specifies the number of *usable* lines of your
76 The screen_lines parameter specifies the number of *usable* lines of your
@@ -93,7 +93,7 b' def page(strng,start=0,screen_lines=0,pager_cmd = None):'
93
93
94 # Some routines may auto-compute start offsets incorrectly and pass a
94 # Some routines may auto-compute start offsets incorrectly and pass a
95 # negative value. Offset to 0 for robustness.
95 # negative value. Offset to 0 for robustness.
96 start = max(0,start)
96 start = max(0, start)
97
97
98 # first, try the hook
98 # first, try the hook
99 ip = ipapi.get()
99 ip = ipapi.get()
@@ -120,19 +120,16 b' def page(strng,start=0,screen_lines=0,pager_cmd = None):'
120 # terminals. If someone later feels like refining it, it's not hard.
120 # terminals. If someone later feels like refining it, it's not hard.
121 numlines = max(num_newlines,int(len_str/80)+1)
121 numlines = max(num_newlines,int(len_str/80)+1)
122
122
123 if os.name == "nt":
123 screen_lines_def = get_terminal_size()[1]
124 screen_lines_def = get_console_size(defaulty=25)[1]
125 else:
126 screen_lines_def = 25 # default value if we can't auto-determine
127
124
128 # auto-determine screen size
125 # auto-determine screen size
129 if screen_lines <= 0:
126 if screen_lines <= 0:
130 if TERM=='xterm' or TERM=='xterm-color':
127 if TERM=='xterm' or TERM=='xterm-color':
131 use_curses = USE_CURSES
128 local_use_curses = use_curses
132 else:
129 else:
133 # curses causes problems on many terminals other than xterm.
130 # curses causes problems on many terminals other than xterm.
134 use_curses = False
131 local_use_curses = False
135 if use_curses:
132 if local_use_curses:
136 import termios
133 import termios
137 import curses
134 import curses
138 # There is a bug in curses, where *sometimes* it fails to properly
135 # There is a bug in curses, where *sometimes* it fails to properly
@@ -201,8 +198,8 b' def page(strng,start=0,screen_lines=0,pager_cmd = None):'
201 if retval is not None:
198 if retval is not None:
202 page_dumb(strng,screen_lines=screen_lines)
199 page_dumb(strng,screen_lines=screen_lines)
203
200
204 #----------------------------------------------------------------------------
201
205 def page_file(fname,start = 0, pager_cmd = None):
202 def page_file(fname, start=0, pager_cmd=None):
206 """Page a file, using an optional pager command and starting line.
203 """Page a file, using an optional pager command and starting line.
207 """
204 """
208
205
@@ -221,12 +218,12 b' def page_file(fname,start = 0, pager_cmd = None):'
221 except:
218 except:
222 print 'Unable to show file',`fname`
219 print 'Unable to show file',`fname`
223
220
224 #----------------------------------------------------------------------------
225 def get_pager_cmd(pager_cmd = None):
226 """Return a pager command.
227
221
228 Makes some attempts at finding an OS-correct one."""
222 def get_pager_cmd(pager_cmd=None):
223 """Return a pager command.
229
224
225 Makes some attempts at finding an OS-correct one.
226 """
230 if os.name == 'posix':
227 if os.name == 'posix':
231 default_pager_cmd = 'less -r' # -r for color control sequences
228 default_pager_cmd = 'less -r' # -r for color control sequences
232 elif os.name in ['nt','dos']:
229 elif os.name in ['nt','dos']:
@@ -239,8 +236,8 b' def get_pager_cmd(pager_cmd = None):'
239 pager_cmd = default_pager_cmd
236 pager_cmd = default_pager_cmd
240 return pager_cmd
237 return pager_cmd
241
238
242 #-----------------------------------------------------------------------------
239
243 def get_pager_start(pager,start):
240 def get_pager_start(pager, start):
244 """Return the string for paging files with an offset.
241 """Return the string for paging files with an offset.
245
242
246 This is the '+N' argument which less and more (under Unix) accept.
243 This is the '+N' argument which less and more (under Unix) accept.
@@ -255,8 +252,8 b' def get_pager_start(pager,start):'
255 start_string = ''
252 start_string = ''
256 return start_string
253 return start_string
257
254
258 #----------------------------------------------------------------------------
255
259 # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch()
256 # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch()
260 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
257 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
261 import msvcrt
258 import msvcrt
262 def page_more():
259 def page_more():
@@ -280,7 +277,7 b' else:'
280 else:
277 else:
281 return True
278 return True
282
279
283 #----------------------------------------------------------------------------
280
284 def snip_print(str,width = 75,print_full = 0,header = ''):
281 def snip_print(str,width = 75,print_full = 0,header = ''):
285 """Print a string snipping the midsection to fit in width.
282 """Print a string snipping the midsection to fit in width.
286
283
@@ -305,4 +302,5 b" def snip_print(str,width = 75,print_full = 0,header = ''):"
305 if snip and print_full == 2:
302 if snip and print_full == 2:
306 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
303 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
307 page(str)
304 page(str)
308 return snip No newline at end of file
305 return snip
306
@@ -27,10 +27,7 b' Authors:'
27
27
28 import __builtin__
28 import __builtin__
29 import codeop
29 import codeop
30 import keyword
31 import os
32 import re
30 import re
33 import sys
34
31
35 from IPython.core.alias import AliasManager
32 from IPython.core.alias import AliasManager
36 from IPython.core.autocall import IPyAutocall
33 from IPython.core.autocall import IPyAutocall
@@ -39,7 +36,8 b' from IPython.core.splitinput import split_user_input'
39 from IPython.core.page import page
36 from IPython.core.page import page
40
37
41 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool
38 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool
42 from IPython.utils.genutils import make_quoted_expr, Term
39 from IPython.utils.io import Term
40 from IPython.utils.text import make_quoted_expr
43 from IPython.utils.autoattr import auto_attr
41 from IPython.utils.autoattr import auto_attr
44
42
45 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
@@ -158,11 +156,12 b' class LineInfo(object):'
158 without worrying about *further* damaging state.
156 without worrying about *further* damaging state.
159 """
157 """
160 if not self._oinfo:
158 if not self._oinfo:
159 # ip.shell._ofind is actually on the Magic class!
161 self._oinfo = ip.shell._ofind(self.ifun)
160 self._oinfo = ip.shell._ofind(self.ifun)
162 return self._oinfo
161 return self._oinfo
163
162
164 def __str__(self):
163 def __str__(self):
165 return "Lineinfo [%s|%s|%s]" %(self.pre,self.ifun,self.the_rest)
164 return "Lineinfo [%s|%s|%s]" %(self.pre, self.ifun, self.the_rest)
166
165
167
166
168 #-----------------------------------------------------------------------------
167 #-----------------------------------------------------------------------------
@@ -12,23 +12,20 b' Classes for handling input/output prompts.'
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Required modules
15
16 import __builtin__
16 import __builtin__
17 import os
17 import os
18 import re
18 import socket
19 import socket
19 import sys
20 import sys
20 import time
21
21
22 # IPython's own
23 from IPython.utils import coloransi
24 from IPython.core import release
22 from IPython.core import release
25 from IPython.external.Itpl import ItplNS
23 from IPython.external.Itpl import ItplNS
26 from IPython.core.error import TryNext
24 from IPython.core.error import TryNext
27 from IPython.utils.ipstruct import Struct
25 from IPython.utils import coloransi
28 from IPython.core.macro import Macro
29 import IPython.utils.generics
26 import IPython.utils.generics
30
27 from IPython.utils.warn import warn
31 from IPython.utils.genutils import *
28 from IPython.utils.io import Term
32
29
33 #****************************************************************************
30 #****************************************************************************
34 #Color schemes for Prompts.
31 #Color schemes for Prompts.
@@ -19,7 +19,11 b' Authors'
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import sys
22
23 #-----------------------------------------------------------------------------
24 # Code
25 #-----------------------------------------------------------------------------
26
23
27
24 class Quitter(object):
28 class Quitter(object):
25 """Simple class to handle exit, similar to Python 2.5's.
29 """Simple class to handle exit, similar to Python 2.5's.
@@ -40,4 +44,4 b' class Quitter(object):'
40 # Repr MUST return a string, else display like pprint hooks get confused
44 # Repr MUST return a string, else display like pprint hooks get confused
41 def __repr__(self):
45 def __repr__(self):
42 self.shell.ask_exit()
46 self.shell.ask_exit()
43 return 'Bye.'
47 return ''
@@ -8,19 +8,15 b' from __future__ import absolute_import'
8 # Imports
8 # Imports
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 # stdlib
12 import os
11 import os
13 import sys
12 import sys
14 import tempfile
13 import tempfile
15 import types
14 import types
16 from cStringIO import StringIO
15 from cStringIO import StringIO
17
16
18 # third-party
19 import nose.tools as nt
17 import nose.tools as nt
20
18
21 # our own
19 from IPython.utils.path import get_long_path_name
22 from IPython.utils import genutils
23 from IPython.utils.platutils import find_cmd, get_long_path_name
24 from IPython.testing import decorators as dec
20 from IPython.testing import decorators as dec
25 from IPython.testing import tools as tt
21 from IPython.testing import tools as tt
26
22
@@ -12,17 +12,12 b' from __future__ import absolute_import'
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # stdlib
16 import os
15 import os
17 import sys
16 import sys
18 import tempfile
17 import tempfile
19
18
20 # third-party
21 import nose.tools as nt
19 import nose.tools as nt
22
20
23 # our own
24 from IPython.utils.platutils import find_cmd
25 from IPython.utils import genutils
26 from IPython.testing import decorators as dec
21 from IPython.testing import decorators as dec
27 from IPython.testing import tools as tt
22 from IPython.testing import tools as tt
28
23
@@ -142,10 +137,10 b' class TestMagicRunSimple(tt.TempFileMixin):'
142 _ip.runlines('t = isinstance(f(), foo)')
137 _ip.runlines('t = isinstance(f(), foo)')
143 nt.assert_true(_ip.user_ns['t'])
138 nt.assert_true(_ip.user_ns['t'])
144
139
145 # We have to skip these in win32 because genutils.getoutputerr() crashes,
140 # We have to skip these in win32 because getoutputerr() crashes,
146 # due to the fact that subprocess does not support close_fds when
141 # due to the fact that subprocess does not support close_fds when
147 # redirecting stdout/err. So unless someone who knows more tells us how to
142 # redirecting stdout/err. So unless someone who knows more tells us how to
148 # implement genutils.getoutputerr() in win32, we're stuck avoiding these.
143 # implement getoutputerr() in win32, we're stuck avoiding these.
149 @dec.skip_win32
144 @dec.skip_win32
150 def test_obj_del(self):
145 def test_obj_del(self):
151 """Test that object's __del__ methods are called on exit."""
146 """Test that object's __del__ methods are called on exit."""
@@ -93,9 +93,10 b' from inspect import getsourcefile, getfile, getmodule,\\'
93 from IPython.utils import PyColorize
93 from IPython.utils import PyColorize
94 from IPython.core import debugger, ipapi
94 from IPython.core import debugger, ipapi
95 from IPython.core.display_trap import DisplayTrap
95 from IPython.core.display_trap import DisplayTrap
96 from IPython.utils.ipstruct import Struct
97 from IPython.core.excolors import exception_colors
96 from IPython.core.excolors import exception_colors
98 from IPython.utils.genutils import Term, uniq_stable, error, info
97 from IPython.utils.data import uniq_stable
98 from IPython.utils.io import Term
99 from IPython.utils.warn import info, error
99
100
100 # Globals
101 # Globals
101 # amount of space to put line numbers before verbose tracebacks
102 # amount of space to put line numbers before verbose tracebacks
@@ -53,7 +53,7 b" __all__ = ['Gnuplot','gp','gp_new','Data','File','Func','GridData',"
53 'pm3d_config','eps_fix_bbox']
53 'pm3d_config','eps_fix_bbox']
54
54
55 import os,tempfile,sys
55 import os,tempfile,sys
56 from IPython.utils.genutils import getoutput
56 from IPython.utils.process import getoutput
57
57
58 #---------------------------------------------------------------------------
58 #---------------------------------------------------------------------------
59 # Notes on mouse support for Gnuplot.py
59 # Notes on mouse support for Gnuplot.py
@@ -133,10 +133,10 b' from IPython.external import simplegeneric'
133 from IPython.external import path
133 from IPython.external import path
134
134
135 try:
135 try:
136 from IPython.utils import genutils
136 from IPython.utils.io import Term
137 from IPython.utils import generics
137 from IPython.utils import generics
138 except ImportError:
138 except ImportError:
139 genutils = None
139 Term = None
140 generics = None
140 generics = None
141
141
142 from IPython.core import ipapi
142 from IPython.core import ipapi
@@ -2168,7 +2168,7 b' class idump(Display):'
2168 self.datasepchar = "|"
2168 self.datasepchar = "|"
2169
2169
2170 def display(self):
2170 def display(self):
2171 stream = genutils.Term.cout
2171 stream = Term.cout
2172 allattrs = []
2172 allattrs = []
2173 attrset = set()
2173 attrset = set()
2174 colwidths = {}
2174 colwidths = {}
@@ -54,7 +54,7 b' from enthought.traits import api as T'
54 # IPython imports
54 # IPython imports
55 from IPython.core.error import TryNext
55 from IPython.core.error import TryNext
56 from IPython.core.ipapi import get as ipget
56 from IPython.core.ipapi import get as ipget
57 from IPython.utils.genutils import dir2
57 from IPython.utils.dir2 import dir2
58 try:
58 try:
59 set
59 set
60 except:
60 except:
@@ -14,7 +14,9 b' from IPython.core.iplib import InteractiveShell'
14 from IPython.utils.ipstruct import Struct
14 from IPython.utils.ipstruct import Struct
15 import Queue,thread,threading,signal
15 import Queue,thread,threading,signal
16 from signal import signal, SIGINT
16 from signal import signal, SIGINT
17 from IPython.utils.genutils import Term,warn,error,flag_calls, ask_yes_no
17 from IPython.utils.io import Term, ask_yes_no
18 from IPython.utils.warn import warn, error
19 from IPython.utils.decorators import flag_calls
18 from IPython.core import shellglobals
20 from IPython.core import shellglobals
19
21
20 def install_gtk2():
22 def install_gtk2():
@@ -39,7 +39,7 b' from IPython.core.error import TryNext'
39 from IPython.external import pretty
39 from IPython.external import pretty
40 from IPython.core.component import Component
40 from IPython.core.component import Component
41 from IPython.utils.traitlets import Bool, List
41 from IPython.utils.traitlets import Bool, List
42 from IPython.utils.genutils import Term
42 from IPython.utils.io import Term
43 from IPython.utils.autoattr import auto_attr
43 from IPython.utils.autoattr import auto_attr
44 from IPython.utils.importstring import import_item
44 from IPython.utils.importstring import import_item
45
45
@@ -213,7 +213,7 b' def main():'
213 print "\n".join(expand(sys.argv[1:])),
213 print "\n".join(expand(sys.argv[1:])),
214
214
215 def mglob_f(self, arg):
215 def mglob_f(self, arg):
216 from IPython.utils.genutils import SList
216 from IPython.utils.text import SList
217 if arg.strip():
217 if arg.strip():
218 return SList(expand(arg))
218 return SList(expand(arg))
219 print "Please specify pattern!"
219 print "Please specify pattern!"
@@ -31,7 +31,7 b' from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap'
31
31
32 from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap
32 from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap
33
33
34 from IPython.utils.genutils import Term
34 from IPython.utils.io import Term
35
35
36 from linefrontendbase import LineFrontEndBase, common_prefix
36 from linefrontendbase import LineFrontEndBase, common_prefix
37
37
@@ -25,7 +25,7 b' from thread_ex import ThreadEx'
25
25
26 import IPython
26 import IPython
27 from IPython.core import iplib, ipapp
27 from IPython.core import iplib, ipapp
28 from IPython.utils import genutils
28 from IPython.utils.io import Term
29
29
30 ##############################################################################
30 ##############################################################################
31 class _Helper(object):
31 class _Helper(object):
@@ -141,11 +141,11 b' class NonBlockingIPShell(object):'
141 #only one instance can be instanciated else tehre will be
141 #only one instance can be instanciated else tehre will be
142 #cin/cout/cerr clash...
142 #cin/cout/cerr clash...
143 if cin:
143 if cin:
144 genutils.Term.cin = cin
144 Term.cin = cin
145 if cout:
145 if cout:
146 genutils.Term.cout = cout
146 Term.cout = cout
147 if cerr:
147 if cerr:
148 genutils.Term.cerr = cerr
148 Term.cerr = cerr
149
149
150 excepthook = sys.excepthook
150 excepthook = sys.excepthook
151
151
@@ -471,7 +471,7 b' class NonBlockingIPShell(object):'
471 '''
471 '''
472
472
473 orig_stdout = sys.stdout
473 orig_stdout = sys.stdout
474 sys.stdout = genutils.Term.cout
474 sys.stdout = Term.cout
475 #self.sys_displayhook_ori = sys.displayhook
475 #self.sys_displayhook_ori = sys.displayhook
476 #sys.displayhook = self.displayhook
476 #sys.displayhook = self.displayhook
477
477
@@ -27,7 +27,6 b' The main classes in this module are:'
27 # Imports
27 # Imports
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 from cStringIO import StringIO
31 import sys
30 import sys
32 import warnings
31 import warnings
33
32
@@ -33,7 +33,7 b' from IPython.kernel.twistedutil import ('
33 sleep_deferred
33 sleep_deferred
34 )
34 )
35 from IPython.utils.importstring import import_item
35 from IPython.utils.importstring import import_item
36 from IPython.utils.genutils import get_ipython_dir
36 from IPython.utils.path import get_ipython_dir
37
37
38 from twisted.internet import defer
38 from twisted.internet import defer
39 from twisted.internet.defer import inlineCallbacks, returnValue
39 from twisted.internet.defer import inlineCallbacks, returnValue
@@ -15,7 +15,7 b' __docformat__ = "restructuredtext en"'
15 # Imports
15 # Imports
16 #-------------------------------------------------------------------------------
16 #-------------------------------------------------------------------------------
17
17
18 from zope.interface import Interface, implements
18 from zope.interface import Interface
19
19
20 class IFCClientInterfaceProvider(Interface):
20 class IFCClientInterfaceProvider(Interface):
21
21
@@ -24,13 +24,14 b' import warnings'
24
24
25 from twisted.python import log
25 from twisted.python import log
26
26
27 from IPython.core import release
28 from IPython.config.loader import PyFileConfigLoader
27 from IPython.config.loader import PyFileConfigLoader
29 from IPython.core.application import Application
28 from IPython.core.application import Application
30 from IPython.core.component import Component
29 from IPython.core.component import Component
31 from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
30 from IPython.utils.path import (
32 from IPython.utils.traitlets import Unicode, Bool
31 get_ipython_package_dir,
33 from IPython.utils import genutils
32 expand_path
33 )
34 from IPython.utils.traitlets import Unicode
34
35
35 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
36 # Warnings control
37 # Warnings control
@@ -225,7 +226,7 b' class ClusterDir(Component):'
225 The path of the cluster directory. This is expanded using
226 The path of the cluster directory. This is expanded using
226 :func:`IPython.utils.genutils.expand_path`.
227 :func:`IPython.utils.genutils.expand_path`.
227 """
228 """
228 cluster_dir = genutils.expand_path(cluster_dir)
229 cluster_dir = expand_path(cluster_dir)
229 if not os.path.isdir(cluster_dir):
230 if not os.path.isdir(cluster_dir):
230 raise ClusterDirError('Cluster directory not found: %s' % cluster_dir)
231 raise ClusterDirError('Cluster directory not found: %s' % cluster_dir)
231 return ClusterDir(cluster_dir)
232 return ClusterDir(cluster_dir)
@@ -316,7 +317,7 b' class ApplicationWithClusterDir(Application):'
316 cluster_dir = self.command_line_config.Global.cluster_dir
317 cluster_dir = self.command_line_config.Global.cluster_dir
317 except AttributeError:
318 except AttributeError:
318 cluster_dir = self.default_config.Global.cluster_dir
319 cluster_dir = self.default_config.Global.cluster_dir
319 cluster_dir = genutils.expand_path(cluster_dir)
320 cluster_dir = expand_path(cluster_dir)
320 try:
321 try:
321 self.cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
322 self.cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
322 except ClusterDirError:
323 except ClusterDirError:
@@ -389,7 +390,7 b' class ApplicationWithClusterDir(Application):'
389 pdir = self.cluster_dir_obj.pid_dir
390 pdir = self.cluster_dir_obj.pid_dir
390 self.pid_dir = config.Global.pid_dir = pdir
391 self.pid_dir = config.Global.pid_dir = pdir
391 self.log.info("Cluster directory set to: %s" % self.cluster_dir)
392 self.log.info("Cluster directory set to: %s" % self.cluster_dir)
392 config.Global.work_dir = unicode(genutils.expand_path(config.Global.work_dir))
393 config.Global.work_dir = unicode(expand_path(config.Global.work_dir))
393 # Change to the working directory. We do this just before construct
394 # Change to the working directory. We do this just before construct
394 # is called so all the components there have the right working dir.
395 # is called so all the components there have the right working dir.
395 self.to_work_dir()
396 self.to_work_dir()
@@ -24,9 +24,7 b' __docformat__ = "restructuredtext en"'
24 import linecache
24 import linecache
25 import sys
25 import sys
26
26
27 from twisted.internet.error import ConnectionRefusedError
27 from IPython.core.ultratb import findsource
28
29 from IPython.core.ultratb import _fixed_getinnerframes, findsource
30 from IPython.core import ipapi
28 from IPython.core import ipapi
31
29
32 from IPython.kernel import error
30 from IPython.kernel import error
@@ -37,20 +37,18 b' __docformat__ = "restructuredtext en"'
37 # Imports
37 # Imports
38 #-------------------------------------------------------------------------------
38 #-------------------------------------------------------------------------------
39
39
40 import os, sys
40 import os
41
41
42 from twisted.application import service
42 from twisted.application import service
43 from twisted.internet import defer, reactor
43 from twisted.python import log
44 from twisted.python import log, components
45 from zope.interface import Interface, implements, Attribute
44 from zope.interface import Interface, implements, Attribute
46 import zope.interface as zi
47
45
48 from IPython.kernel.engineservice import \
46 from IPython.kernel.engineservice import \
49 IEngineCore, \
47 IEngineCore, \
50 IEngineSerialized, \
48 IEngineSerialized, \
51 IEngineQueued
49 IEngineQueued
52
50
53 from IPython.utils.genutils import get_ipython_dir
51 from IPython.utils.path import get_ipython_dir
54 from IPython.kernel import codeutil
52 from IPython.kernel import codeutil
55
53
56 #-------------------------------------------------------------------------------
54 #-------------------------------------------------------------------------------
@@ -21,6 +21,8 b' __docformat__ = "restructuredtext en"'
21
21
22 # Required modules
22 # Required modules
23 import __builtin__
23 import __builtin__
24 import os
25 import re
24 import socket
26 import socket
25 import sys
27 import sys
26
28
@@ -30,7 +32,8 b' from IPython.external.Itpl import ItplNS'
30 from IPython.utils import coloransi
32 from IPython.utils import coloransi
31 from IPython.core import release
33 from IPython.core import release
32 from IPython.core.error import TryNext
34 from IPython.core.error import TryNext
33 from IPython.utils.genutils import *
35 from IPython.utils.io import Term
36 from IPython.utils.warn import warn
34 import IPython.utils.generics
37 import IPython.utils.generics
35
38
36 #****************************************************************************
39 #****************************************************************************
@@ -240,7 +243,7 b' class BasePrompt(object):'
240 This must be called every time the color settings change, because the
243 This must be called every time the color settings change, because the
241 prompt_specials global may have changed."""
244 prompt_specials global may have changed."""
242
245
243 import os,time # needed in locals for prompt string handling
246 import os, time # needed in locals for prompt string handling
244 loc = locals()
247 loc = locals()
245 self.p_str = ItplNS('%s%s%s' %
248 self.p_str = ItplNS('%s%s%s' %
246 ('${self.sep}${self.col_p}',
249 ('${self.sep}${self.col_p}',
@@ -17,8 +17,7 b''
17 import os
17 import os
18 import cPickle as pickle
18 import cPickle as pickle
19
19
20 from twisted.python import log, failure
20 from twisted.python import log
21 from twisted.internet import defer
22 from twisted.internet.defer import inlineCallbacks, returnValue
21 from twisted.internet.defer import inlineCallbacks, returnValue
23
22
24 from IPython.kernel.fcutil import find_furl, validate_furl_or_file
23 from IPython.kernel.fcutil import find_furl, validate_furl_or_file
@@ -19,14 +19,11 b' __docformat__ = "restructuredtext en"'
19 # Imports
19 # Imports
20 #-------------------------------------------------------------------------------
20 #-------------------------------------------------------------------------------
21
21
22 import os, time
23 import cPickle as pickle
22 import cPickle as pickle
24
23
25 from twisted.python import components, log, failure
24 from twisted.python import components, log, failure
26 from twisted.python.failure import Failure
25 from twisted.internet import defer, threads
27 from twisted.internet import defer, reactor, threads
26 from zope.interface import Interface, implements
28 from twisted.internet.interfaces import IProtocolFactory
29 from zope.interface import Interface, implements, Attribute
30
27
31 from twisted.internet.base import DelayedCall
28 from twisted.internet.base import DelayedCall
32 DelayedCall.debug = True
29 DelayedCall.debug = True
@@ -35,24 +32,20 b' from foolscap import Referenceable, DeadReferenceError'
35 from foolscap.referenceable import RemoteReference
32 from foolscap.referenceable import RemoteReference
36
33
37 from IPython.kernel.pbutil import packageFailure, unpackageFailure
34 from IPython.kernel.pbutil import packageFailure, unpackageFailure
38 from IPython.kernel.util import printer
39 from IPython.kernel.twistedutil import gatherBoth
40 from IPython.kernel import newserialized
41 from IPython.kernel.error import ProtocolError
42 from IPython.kernel import controllerservice
43 from IPython.kernel.controllerservice import IControllerBase
35 from IPython.kernel.controllerservice import IControllerBase
44 from IPython.kernel.engineservice import \
36 from IPython.kernel.engineservice import (
45 IEngineBase, \
37 IEngineBase,
46 IEngineQueued, \
38 IEngineQueued,
47 EngineService, \
48 StrictDict
39 StrictDict
49 from IPython.kernel.pickleutil import \
40 )
50 can, \
41 from IPython.kernel.pickleutil import (
51 canDict, \
42 can,
52 canSequence, \
43 canDict,
53 uncan, \
44 canSequence,
54 uncanDict, \
45 uncan,
46 uncanDict,
55 uncanSequence
47 uncanSequence
48 )
56
49
57
50
58 #-------------------------------------------------------------------------------
51 #-------------------------------------------------------------------------------
@@ -18,20 +18,18 b' The IPython controller application.'
18 from __future__ import with_statement
18 from __future__ import with_statement
19
19
20 import copy
20 import copy
21 import os
22 import sys
21 import sys
23
22
24 from twisted.application import service
23 from twisted.application import service
25 from twisted.internet import reactor
24 from twisted.internet import reactor
26 from twisted.python import log
25 from twisted.python import log
27
26
28 from IPython.config.loader import Config, NoConfigDefault
27 from IPython.config.loader import Config
29 from IPython.core import release
30 from IPython.core.application import Application
28 from IPython.core.application import Application
31 from IPython.kernel import controllerservice
29 from IPython.kernel import controllerservice
32 from IPython.kernel.clusterdir import ApplicationWithClusterDir
30 from IPython.kernel.clusterdir import ApplicationWithClusterDir
33 from IPython.kernel.fcutil import FCServiceFactory
31 from IPython.kernel.fcutil import FCServiceFactory
34 from IPython.utils.traitlets import Str, Instance, Unicode
32 from IPython.utils.traitlets import Instance, Unicode
35
33
36 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
37 # Default interfaces
35 # Default interfaces
@@ -21,11 +21,15 b' import sys'
21
21
22 from IPython.core.component import Component
22 from IPython.core.component import Component
23 from IPython.external import Itpl
23 from IPython.external import Itpl
24 from IPython.utils.traitlets import Str, Int, List, Unicode, Enum
24 from IPython.utils.traitlets import Str, Int, List, Unicode
25 from IPython.utils.platutils import find_cmd
25 from IPython.utils.path import get_ipython_module_path
26 from IPython.kernel.twistedutil import gatherBoth, make_deferred, sleep_deferred
26 from IPython.utils.process import find_cmd, pycmd2argv
27 from IPython.kernel.twistedutil import (
28 gatherBoth,
29 make_deferred,
30 sleep_deferred
31 )
27 from IPython.kernel.winhpcjob import (
32 from IPython.kernel.winhpcjob import (
28 WinHPCJob, WinHPCTask,
29 IPControllerTask, IPEngineTask,
33 IPControllerTask, IPEngineTask,
30 IPControllerJob, IPEngineSetJob
34 IPControllerJob, IPEngineSetJob
31 )
35 )
@@ -38,46 +42,23 b' from twisted.internet.error import ProcessDone, ProcessTerminated'
38 from twisted.python import log
42 from twisted.python import log
39 from twisted.python.failure import Failure
43 from twisted.python.failure import Failure
40
44
45
41 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
42 # Utilities
47 # Paths to the kernel apps
43 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
44
49
45
50
46 def find_controller_cmd():
51 ipcluster_cmd_argv = pycmd2argv(get_ipython_module_path(
47 """Find the command line ipcontroller program in a cross platform way."""
52 'IPython.kernel.ipclusterapp'
48 if sys.platform == 'win32':
53 ))
49 # This logic is needed because the ipcontroller script doesn't
50 # always get installed in the same way or in the same location.
51 from IPython.kernel import ipcontrollerapp
52 script_location = ipcontrollerapp.__file__.replace('.pyc', '.py')
53 # The -u option here turns on unbuffered output, which is required
54 # on Win32 to prevent wierd conflict and problems with Twisted.
55 # Also, use sys.executable to make sure we are picking up the
56 # right python exe.
57 cmd = [sys.executable, '-u', script_location]
58 else:
59 # ipcontroller has to be on the PATH in this case.
60 cmd = ['ipcontroller']
61 return cmd
62
63
64 def find_engine_cmd():
65 """Find the command line ipengine program in a cross platform way."""
66 if sys.platform == 'win32':
67 # This logic is needed because the ipengine script doesn't
68 # always get installed in the same way or in the same location.
69 from IPython.kernel import ipengineapp
70 script_location = ipengineapp.__file__.replace('.pyc', '.py')
71 # The -u option here turns on unbuffered output, which is required
72 # on Win32 to prevent wierd conflict and problems with Twisted.
73 # Also, use sys.executable to make sure we are picking up the
74 # right python exe.
75 cmd = [sys.executable, '-u', script_location]
76 else:
77 # ipcontroller has to be on the PATH in this case.
78 cmd = ['ipengine']
79 return cmd
80
54
55 ipengine_cmd_argv = pycmd2argv(get_ipython_module_path(
56 'IPython.kernel.ipengineapp'
57 ))
58
59 ipcontroller_cmd_argv = pycmd2argv(get_ipython_module_path(
60 'IPython.kernel.ipcontrollerapp'
61 ))
81
62
82 #-----------------------------------------------------------------------------
63 #-----------------------------------------------------------------------------
83 # Base launchers and errors
64 # Base launchers and errors
@@ -333,7 +314,7 b' class LocalProcessLauncher(BaseLauncher):'
333 class LocalControllerLauncher(LocalProcessLauncher):
314 class LocalControllerLauncher(LocalProcessLauncher):
334 """Launch a controller as a regular external process."""
315 """Launch a controller as a regular external process."""
335
316
336 controller_cmd = List(find_controller_cmd(), config=True)
317 controller_cmd = List(ipcontroller_cmd_argv, config=True)
337 # Command line arguments to ipcontroller.
318 # Command line arguments to ipcontroller.
338 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
319 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
339
320
@@ -351,7 +332,7 b' class LocalControllerLauncher(LocalProcessLauncher):'
351 class LocalEngineLauncher(LocalProcessLauncher):
332 class LocalEngineLauncher(LocalProcessLauncher):
352 """Launch a single engine as a regular externall process."""
333 """Launch a single engine as a regular externall process."""
353
334
354 engine_cmd = List(find_engine_cmd(), config=True)
335 engine_cmd = List(ipengine_cmd_argv, config=True)
355 # Command line arguments for ipengine.
336 # Command line arguments for ipengine.
356 engine_args = List(
337 engine_args = List(
357 ['--log-to-file','--log-level', '40'], config=True
338 ['--log-to-file','--log-level', '40'], config=True
@@ -462,7 +443,7 b' class MPIExecLauncher(LocalProcessLauncher):'
462 class MPIExecControllerLauncher(MPIExecLauncher):
443 class MPIExecControllerLauncher(MPIExecLauncher):
463 """Launch a controller using mpiexec."""
444 """Launch a controller using mpiexec."""
464
445
465 controller_cmd = List(find_controller_cmd(), config=True)
446 controller_cmd = List(ipcontroller_cmd_argv, config=True)
466 # Command line arguments to ipcontroller.
447 # Command line arguments to ipcontroller.
467 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
448 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
468 n = Int(1, config=False)
449 n = Int(1, config=False)
@@ -481,7 +462,7 b' class MPIExecControllerLauncher(MPIExecLauncher):'
481
462
482 class MPIExecEngineSetLauncher(MPIExecLauncher):
463 class MPIExecEngineSetLauncher(MPIExecLauncher):
483
464
484 engine_cmd = List(find_engine_cmd(), config=True)
465 engine_cmd = List(ipengine_cmd_argv, config=True)
485 # Command line arguments for ipengine.
466 # Command line arguments for ipengine.
486 engine_args = List(
467 engine_args = List(
487 ['--log-to-file','--log-level', '40'], config=True
468 ['--log-to-file','--log-level', '40'], config=True
@@ -831,28 +812,10 b' class PBSEngineSetLauncher(PBSLauncher):'
831 #-----------------------------------------------------------------------------
812 #-----------------------------------------------------------------------------
832
813
833
814
834 def find_ipcluster_cmd():
835 """Find the command line ipcluster program in a cross platform way."""
836 if sys.platform == 'win32':
837 # This logic is needed because the ipcluster script doesn't
838 # always get installed in the same way or in the same location.
839 from IPython.kernel import ipclusterapp
840 script_location = ipclusterapp.__file__.replace('.pyc', '.py')
841 # The -u option here turns on unbuffered output, which is required
842 # on Win32 to prevent wierd conflict and problems with Twisted.
843 # Also, use sys.executable to make sure we are picking up the
844 # right python exe.
845 cmd = [sys.executable, '-u', script_location]
846 else:
847 # ipcontroller has to be on the PATH in this case.
848 cmd = ['ipcluster']
849 return cmd
850
851
852 class IPClusterLauncher(LocalProcessLauncher):
815 class IPClusterLauncher(LocalProcessLauncher):
853 """Launch the ipcluster program in an external process."""
816 """Launch the ipcluster program in an external process."""
854
817
855 ipcluster_cmd = List(find_ipcluster_cmd(), config=True)
818 ipcluster_cmd = List(ipcluster_cmd_argv, config=True)
856 # Command line arguments to pass to ipcluster.
819 # Command line arguments to pass to ipcluster.
857 ipcluster_args = List(
820 ipcluster_args = List(
858 ['--clean-logs', '--log-to-file', '--log-level', '40'], config=True)
821 ['--clean-logs', '--log-to-file', '--log-level', '40'], config=True)
@@ -21,7 +21,7 b' __docformat__ = "restructuredtext en"'
21
21
22 import types
22 import types
23
23
24 from IPython.utils.genutils import flatten as genutil_flatten
24 from IPython.utils.data import flatten as utils_flatten
25
25
26 #-------------------------------------------------------------------------------
26 #-------------------------------------------------------------------------------
27 # Figure out which array packages are present and their array types
27 # Figure out which array packages are present and their array types
@@ -87,7 +87,7 b' class Map:'
87 return m['module'].concatenate(listOfPartitions)
87 return m['module'].concatenate(listOfPartitions)
88 # Next try for Python sequence types
88 # Next try for Python sequence types
89 if isinstance(testObject, (types.ListType, types.TupleType)):
89 if isinstance(testObject, (types.ListType, types.TupleType)):
90 return genutil_flatten(listOfPartitions)
90 return utils_flatten(listOfPartitions)
91 # If we have scalars, just return listOfPartitions
91 # If we have scalars, just return listOfPartitions
92 return listOfPartitions
92 return listOfPartitions
93
93
@@ -18,8 +18,7 b' __docformat__ = "restructuredtext en"'
18 from types import FunctionType
18 from types import FunctionType
19 from zope.interface import Interface, implements
19 from zope.interface import Interface, implements
20 from IPython.kernel.task import MapTask
20 from IPython.kernel.task import MapTask
21 from IPython.kernel.twistedutil import DeferredList, gatherBoth
21 from IPython.kernel.twistedutil import gatherBoth
22 from IPython.kernel.util import printer
23 from IPython.kernel.error import collect_exceptions
22 from IPython.kernel.error import collect_exceptions
24
23
25 #----------------------------------------------------------------------------
24 #----------------------------------------------------------------------------
@@ -27,24 +27,17 b' __docformat__ = "restructuredtext en"'
27 # Imports
27 # Imports
28 #-------------------------------------------------------------------------------
28 #-------------------------------------------------------------------------------
29
29
30 from new import instancemethod
31 from types import FunctionType
32
33 from twisted.application import service
34 from twisted.internet import defer, reactor
30 from twisted.internet import defer, reactor
35 from twisted.python import log, components, failure
31 from twisted.python import log, components, failure
36 from zope.interface import Interface, implements, Attribute
32 from zope.interface import Interface, implements
37
33
38 from IPython.utils import growl
39 from IPython.kernel.util import printer
40 from IPython.kernel.twistedutil import gatherBoth
34 from IPython.kernel.twistedutil import gatherBoth
41 from IPython.kernel import map as Map
42 from IPython.kernel import error
35 from IPython.kernel import error
43 from IPython.kernel.pendingdeferred import PendingDeferredManager, two_phase
36 from IPython.kernel.pendingdeferred import PendingDeferredManager, two_phase
44 from IPython.kernel.controllerservice import \
37 from IPython.kernel.controllerservice import (
45 ControllerAdapterBase, \
38 ControllerAdapterBase,
46 ControllerService, \
47 IControllerBase
39 IControllerBase
40 )
48
41
49
42
50 #-------------------------------------------------------------------------------
43 #-------------------------------------------------------------------------------
@@ -22,12 +22,11 b' from types import FunctionType'
22
22
23 from zope.interface import Interface, implements
23 from zope.interface import Interface, implements
24 from twisted.internet import defer
24 from twisted.internet import defer
25 from twisted.python import components, failure, log
25 from twisted.python import components, failure
26
26
27 from foolscap import Referenceable
27 from foolscap import Referenceable
28
28
29 from IPython.kernel import error
29 from IPython.kernel import error
30 from IPython.kernel.util import printer
31 from IPython.kernel import map as Map
30 from IPython.kernel import map as Map
32 from IPython.kernel.parallelfunction import ParallelFunction
31 from IPython.kernel.parallelfunction import ParallelFunction
33 from IPython.kernel.mapper import (
32 from IPython.kernel.mapper import (
@@ -36,14 +35,15 b' from IPython.kernel.mapper import ('
36 IMapper
35 IMapper
37 )
36 )
38 from IPython.kernel.twistedutil import gatherBoth
37 from IPython.kernel.twistedutil import gatherBoth
39 from IPython.kernel.multiengine import (MultiEngine,
38 from IPython.kernel.multiengine import (
40 IMultiEngine,
39 IMultiEngine,
41 IFullSynchronousMultiEngine,
40 IFullSynchronousMultiEngine,
42 ISynchronousMultiEngine)
41 ISynchronousMultiEngine)
43 from IPython.kernel.multiengineclient import wrapResultList
44 from IPython.kernel.pendingdeferred import PendingDeferredManager
42 from IPython.kernel.pendingdeferred import PendingDeferredManager
45 from IPython.kernel.pickleutil import (can, canDict,
43 from IPython.kernel.pickleutil import (
46 canSequence, uncan, uncanDict, uncanSequence)
44 canDict,
45 canSequence, uncanDict, uncanSequence
46 )
47
47
48 from IPython.kernel.clientinterfaces import (
48 from IPython.kernel.clientinterfaces import (
49 IFCClientInterfaceProvider,
49 IFCClientInterfaceProvider,
@@ -19,7 +19,6 b' import cPickle as pickle'
19
19
20 from twisted.python.failure import Failure
20 from twisted.python.failure import Failure
21 from twisted.python import failure
21 from twisted.python import failure
22 import threading, sys
23
22
24 from IPython.kernel import pbconfig
23 from IPython.kernel import pbconfig
25 from IPython.kernel.error import PBMessageSizeError, UnpickleableException
24 from IPython.kernel.error import PBMessageSizeError, UnpickleableException
@@ -58,7 +57,7 b' def unpackageFailure(r):'
58 result = pickle.loads(r[8:])
57 result = pickle.loads(r[8:])
59 except pickle.PickleError:
58 except pickle.PickleError:
60 return failure.Failure( \
59 return failure.Failure( \
61 FailureUnpickleable("Could not unpickle failure."))
60 UnpickleableException("Could not unpickle failure."))
62 else:
61 else:
63 return result
62 return result
64 return r
63 return r
@@ -22,15 +22,11 b' __docformat__ = "restructuredtext en"'
22 # Imports
22 # Imports
23 #-------------------------------------------------------------------------------
23 #-------------------------------------------------------------------------------
24
24
25 from twisted.application import service
25 from twisted.internet import defer
26 from twisted.internet import defer, reactor
26 from twisted.python import failure
27 from twisted.python import log, components, failure
28 from zope.interface import Interface, implements, Attribute
29
27
30 from IPython.kernel.twistedutil import gatherBoth
31 from IPython.kernel import error
28 from IPython.kernel import error
32 from IPython.external import guid
29 from IPython.external import guid
33 from IPython.utils import growl
34
30
35 class PendingDeferredManager(object):
31 class PendingDeferredManager(object):
36 """A class to track pending deferreds.
32 """A class to track pending deferreds.
@@ -16,7 +16,6 b' __docformat__ = "restructuredtext en"'
16 #-------------------------------------------------------------------------------
16 #-------------------------------------------------------------------------------
17
17
18 from types import FunctionType
18 from types import FunctionType
19 from twisted.python import log
20
19
21 class CannedObject(object):
20 class CannedObject(object):
22 pass
21 pass
@@ -19,19 +19,18 b' __docformat__ = "restructuredtext en"'
19 # Tell nose to skip the testing of this module
19 # Tell nose to skip the testing of this module
20 __test__ = {}
20 __test__ = {}
21
21
22 import copy, time
22 import time
23 from types import FunctionType
23 from types import FunctionType
24
24
25 import zope.interface as zi, string
25 import zope.interface as zi
26 from twisted.internet import defer, reactor
26 from twisted.internet import defer, reactor
27 from twisted.python import components, log, failure
27 from twisted.python import components, log, failure
28
28
29 from IPython.kernel.util import printer
30 from IPython.kernel import engineservice as es, error
29 from IPython.kernel import engineservice as es, error
31 from IPython.kernel import controllerservice as cs
30 from IPython.kernel import controllerservice as cs
32 from IPython.kernel.twistedutil import gatherBoth, DeferredList
31 from IPython.kernel.twistedutil import DeferredList
33
32
34 from IPython.kernel.pickleutil import can, uncan, CannedFunction
33 from IPython.kernel.pickleutil import can, uncan
35
34
36 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
37 # Definition of the Task objects
36 # Definition of the Task objects
@@ -19,10 +19,10 b' __docformat__ = "restructuredtext en"'
19 #-------------------------------------------------------------------------------
19 #-------------------------------------------------------------------------------
20
20
21 from zope.interface import Interface, implements
21 from zope.interface import Interface, implements
22 from twisted.python import components, log
22 from twisted.python import components
23
23
24 from IPython.kernel.twistedutil import blockingCallFromThread
24 from IPython.kernel.twistedutil import blockingCallFromThread
25 from IPython.kernel import task, error
25 from IPython.kernel import task
26 from IPython.kernel.mapper import (
26 from IPython.kernel.mapper import (
27 SynchronousTaskMapper,
27 SynchronousTaskMapper,
28 ITaskMapperFactory,
28 ITaskMapperFactory,
@@ -19,17 +19,14 b' __docformat__ = "restructuredtext en"'
19 #-------------------------------------------------------------------------------
19 #-------------------------------------------------------------------------------
20
20
21 import cPickle as pickle
21 import cPickle as pickle
22 import xmlrpclib, copy
23
22
24 from zope.interface import Interface, implements
23 from zope.interface import Interface, implements
25 from twisted.internet import defer
24 from twisted.internet import defer
26 from twisted.python import components, failure
25 from twisted.python import components
27
26
28 from foolscap import Referenceable
27 from foolscap import Referenceable
29
28
30 from IPython.kernel.twistedutil import blockingCallFromThread
29 from IPython.kernel import task as taskmodule
31 from IPython.kernel import error, task as taskmodule, taskclient
32 from IPython.kernel.pickleutil import can, uncan
33 from IPython.kernel.clientinterfaces import (
30 from IPython.kernel.clientinterfaces import (
34 IFCClientInterfaceProvider,
31 IFCClientInterfaceProvider,
35 IBlockingClientAdaptor
32 IBlockingClientAdaptor
@@ -15,7 +15,7 b''
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import os, sys
17 import os, sys
18 import threading, Queue, atexit
18 import threading, Queue
19
19
20 import twisted
20 import twisted
21 from twisted.internet import defer, reactor
21 from twisted.internet import defer, reactor
@@ -23,12 +23,10 b' import re'
23 import uuid
23 import uuid
24
24
25 from xml.etree import ElementTree as ET
25 from xml.etree import ElementTree as ET
26 from xml.dom import minidom
27
26
28 from IPython.core.component import Component
27 from IPython.core.component import Component
29 from IPython.external import Itpl
30 from IPython.utils.traitlets import (
28 from IPython.utils.traitlets import (
31 Str, Int, List, Unicode, Instance,
29 Str, Int, List, Instance,
32 Enum, Bool, CStr
30 Enum, Bool, CStr
33 )
31 )
34
32
@@ -31,7 +31,7 b' import sys'
31 import threading
31 import threading
32
32
33 from IPython.core.ultratb import AutoFormattedTB
33 from IPython.core.ultratb import AutoFormattedTB
34 from IPython.utils.genutils import warn,error
34 from IPython.utils.warn import warn, error
35
35
36 class BackgroundJobManager:
36 class BackgroundJobManager:
37 """Class to manage a pool of backgrounded threaded jobs.
37 """Class to manage a pool of backgrounded threaded jobs.
@@ -176,7 +176,8 b' import shlex'
176 import sys
176 import sys
177
177
178 from IPython.utils.PyColorize import Parser
178 from IPython.utils.PyColorize import Parser
179 from IPython.utils.genutils import marquee, file_read, file_readlines, Term
179 from IPython.utils.io import file_read, file_readlines, Term
180 from IPython.utils.text import marquee
180
181
181 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
182 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
182
183
@@ -543,7 +544,7 b' class ClearMixin(object):'
543 """Method called before executing each block.
544 """Method called before executing each block.
544
545
545 This one simply clears the screen."""
546 This one simply clears the screen."""
546 from IPython.utils.platutils import term_clear
547 from IPython.utils.terminal import term_clear
547 term_clear()
548 term_clear()
548
549
549 class ClearDemo(ClearMixin,Demo):
550 class ClearDemo(ClearMixin,Demo):
@@ -12,10 +12,12 b' Fernando Perez.'
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
16 # Imports
17 # Imports
17 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
18 from IPython.utils.genutils import flag_calls
19
20 from IPython.utils.decorators import flag_calls
19
21
20 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
21 # Main classes and functions
23 # Main classes and functions
@@ -50,7 +50,7 b' del InteractiveShell,prefilter_shell'
50
50
51 # Provide pysh and further shell-oriented services
51 # Provide pysh and further shell-oriented services
52 import os,sys,shutil
52 import os,sys,shutil
53 from IPython.utils.genutils import system,shell,getoutput,getoutputerror
53 from IPython.utils.process import system,shell,getoutput,getoutputerror
54
54
55 # Short aliases for getting shell output as a string and a list
55 # Short aliases for getting shell output as a string and a list
56 sout = getoutput
56 sout = getoutput
@@ -10,6 +10,7 b' var = !ls'
10
10
11 from IPython.core import ipapi
11 from IPython.core import ipapi
12 from IPython.core.error import TryNext
12 from IPython.core.error import TryNext
13 from IPython.utils.text import make_quoted_expr
13 from IPython.utils.genutils import *
14 from IPython.utils.genutils import *
14
15
15 ip = ipapi.get()
16 ip = ipapi.get()
@@ -12,7 +12,7 b' do the same in default completer.'
12 from IPython.core import ipapi
12 from IPython.core import ipapi
13 from IPython.core.error import TryNext
13 from IPython.core.error import TryNext
14 from IPython.utils import generics
14 from IPython.utils import generics
15 from IPython.utils.genutils import dir2
15 from IPython.utils.dir2 import dir2
16
16
17 def attr_matches(self, text):
17 def attr_matches(self, text):
18 """Compute matches when text contains a dot.
18 """Compute matches when text contains a dot.
@@ -16,6 +16,7 b' import pickleshare'
16 import inspect,pickle,os,sys,textwrap
16 import inspect,pickle,os,sys,textwrap
17 from IPython.core.fakemodule import FakeModule
17 from IPython.core.fakemodule import FakeModule
18 from IPython.utils.ipstruct import Struct
18 from IPython.utils.ipstruct import Struct
19 from IPython.utils.warn import error
19
20
20
21
21 def refresh_variables(ip, key=None):
22 def refresh_variables(ip, key=None):
@@ -1,6 +1,6 b''
1 import inspect
1 import inspect
2 from IPython.core import ipapi
2 from IPython.core import ipapi
3 from IPython.utils.genutils import arg_split
3 from IPython.utils.process import arg_split
4 ip = ipapi.get()
4 ip = ipapi.get()
5
5
6 from IPython.core import debugger
6 from IPython.core import debugger
@@ -45,10 +45,9 b' from subprocess import *'
45 import os,shlex,sys,time
45 import os,shlex,sys,time
46 import threading,Queue
46 import threading,Queue
47
47
48 from IPython.utils import genutils
49
50 from IPython.core import ipapi
48 from IPython.core import ipapi
51 from IPython.core.error import TryNext
49 from IPython.core.error import TryNext
50 from IPython.utils.text import make_quoted_expr
52
51
53 if os.name == 'nt':
52 if os.name == 'nt':
54 def kill_process(pid):
53 def kill_process(pid):
@@ -126,8 +125,8 b' def jobctrl_prefilter_f(self,line):'
126
125
127 line = ip.expand_aliases(fn,rest)
126 line = ip.expand_aliases(fn,rest)
128 if not _jobq:
127 if not _jobq:
129 return 'get_ipython().startjob(%s)' % genutils.make_quoted_expr(line)
128 return 'get_ipython().startjob(%s)' % make_quoted_expr(line)
130 return 'get_ipython().jobq(%s)' % genutils.make_quoted_expr(line)
129 return 'get_ipython().jobq(%s)' % make_quoted_expr(line)
131
130
132 raise TryNext
131 raise TryNext
133
132
@@ -1,6 +1,17 b''
1 """Testing support (tools to test IPython itself).
1 """Testing support (tools to test IPython itself).
2 """
2 """
3
3
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2009 The IPython Development Team
6 #
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Functions
13 #-----------------------------------------------------------------------------
14
4 # User-level entry point for testing
15 # User-level entry point for testing
5 def test():
16 def test():
6 """Run the entire IPython test suite.
17 """Run the entire IPython test suite.
@@ -4,6 +4,17 b' This is just so we can use 2.6 features when running in 2.5, the code below is'
4 copied verbatim from the stdlib's collections and doctest modules.
4 copied verbatim from the stdlib's collections and doctest modules.
5 """
5 """
6
6
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2009 The IPython Development Team
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
7 from keyword import iskeyword as _iskeyword
18 from keyword import iskeyword as _iskeyword
8 from operator import itemgetter as _itemgetter
19 from operator import itemgetter as _itemgetter
9 import sys as _sys
20 import sys as _sys
@@ -1,10 +1,17 b''
1 """Implementation of the parametric test support for Python 2.x
1 """Implementation of the parametric test support for Python 2.x
2 """
2 """
3
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2009 The IPython Development Team
6 #
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
10
3 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
4 # Imports
12 # Imports
5 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
6
14
7 # Stdlib
8 import unittest
15 import unittest
9 from compiler.consts import CO_GENERATOR
16 from compiler.consts import CO_GENERATOR
10
17
@@ -3,11 +3,18 b''
3 Thanks for the py3 version to Robert Collins, from the Testing in Python
3 Thanks for the py3 version to Robert Collins, from the Testing in Python
4 mailing list.
4 mailing list.
5 """
5 """
6
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2009 The IPython Development Team
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
13
6 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
7 # Imports
15 # Imports
8 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
9
17
10 # Stdlib
11 import unittest
18 import unittest
12 from unittest import TestSuite
19 from unittest import TestSuite
13
20
@@ -9,18 +9,22 b' done.'
9 from __future__ import absolute_import
9 from __future__ import absolute_import
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Module imports
12 # Copyright (C) 2009 The IPython Development Team
13 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17
18 #-----------------------------------------------------------------------------
19 # Imports
13 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
14
21
15 # From the standard library
16 import __builtin__
22 import __builtin__
17 import commands
23 import commands
18 import new
19 import os
24 import os
20 import sys
25 import sys
21
26
22 from . import tools
27 from . import tools
23 from IPython.utils.genutils import Term
24
28
25 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
26 # Functions
30 # Functions
@@ -35,7 +39,7 b' class py_file_finder(object):'
35 self.test_filename = test_filename
39 self.test_filename = test_filename
36
40
37 def __call__(self,name):
41 def __call__(self,name):
38 from IPython.utils.genutils import get_py_filename
42 from IPython.utils.path import get_py_filename
39 try:
43 try:
40 return get_py_filename(name)
44 return get_py_filename(name)
41 except IOError:
45 except IOError:
@@ -17,13 +17,19 b' will change in the future.'
17 """
17 """
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Module imports
20 # Copyright (C) 2009 The IPython Development Team
21 #
22 # Distributed under the terms of the BSD License. The full license is in
23 # the file COPYING, distributed as part of this software.
24 #-----------------------------------------------------------------------------
25
26 #-----------------------------------------------------------------------------
27 # Imports
21 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
22
29
23 # Stdlib
30 # Stdlib
24 import os
31 import os
25 import os.path as path
32 import os.path as path
26 import platform
27 import signal
33 import signal
28 import sys
34 import sys
29 import subprocess
35 import subprocess
@@ -50,11 +56,11 b' import nose.plugins.builtin'
50 from nose.core import TestProgram
56 from nose.core import TestProgram
51
57
52 # Our own imports
58 # Our own imports
53 from IPython.core import release
59 from IPython.utils.path import get_ipython_module_path
54 from IPython.utils import genutils
60 from IPython.utils.process import find_cmd, pycmd2argv
55 from IPython.utils.platutils import find_cmd, FindCmdError
61 from IPython.utils.sysinfo import sys_info
62
56 from IPython.testing import globalipapp
63 from IPython.testing import globalipapp
57 from IPython.testing import tools
58 from IPython.testing.plugin.ipdoctest import IPythonDoctest
64 from IPython.testing.plugin.ipdoctest import IPythonDoctest
59
65
60 pjoin = path.join
66 pjoin = path.join
@@ -124,7 +130,7 b" have['gobject'] = test_for('gobject')"
124 def report():
130 def report():
125 """Return a string with a summary report of test-related variables."""
131 """Return a string with a summary report of test-related variables."""
126
132
127 out = [ genutils.sys_info() ]
133 out = [ sys_info() ]
128
134
129 out.append('\nRunning from an installed IPython: %s\n' % INSTALLED)
135 out.append('\nRunning from an installed IPython: %s\n' % INSTALLED)
130
136
@@ -198,18 +204,12 b' def make_exclude():'
198 if not have['objc']:
204 if not have['objc']:
199 exclusions.append(ipjoin('frontend', 'cocoa'))
205 exclusions.append(ipjoin('frontend', 'cocoa'))
200
206
201 if not sys.platform == 'win32':
202 exclusions.append(ipjoin('utils', 'platutils_win32'))
203
204 # These have to be skipped on win32 because the use echo, rm, cd, etc.
207 # These have to be skipped on win32 because the use echo, rm, cd, etc.
205 # See ticket https://bugs.launchpad.net/bugs/366982
208 # See ticket https://bugs.launchpad.net/bugs/366982
206 if sys.platform == 'win32':
209 if sys.platform == 'win32':
207 exclusions.append(ipjoin('testing', 'plugin', 'test_exampleip'))
210 exclusions.append(ipjoin('testing', 'plugin', 'test_exampleip'))
208 exclusions.append(ipjoin('testing', 'plugin', 'dtexample'))
211 exclusions.append(ipjoin('testing', 'plugin', 'dtexample'))
209
212
210 if not os.name == 'posix':
211 exclusions.append(ipjoin('utils', 'platutils_posix'))
212
213 if not have['pexpect']:
213 if not have['pexpect']:
214 exclusions.extend([ipjoin('scripts', 'irunner'),
214 exclusions.extend([ipjoin('scripts', 'irunner'),
215 ipjoin('lib', 'irunner')])
215 ipjoin('lib', 'irunner')])
@@ -256,19 +256,19 b' class IPTester(object):'
256 p = os.path
256 p = os.path
257 if runner == 'iptest':
257 if runner == 'iptest':
258 if INSTALLED:
258 if INSTALLED:
259 self.runner = tools.cmd2argv(
259 iptest_app = get_ipython_module_path('IPython.testing.iptest')
260 p.abspath(find_cmd('iptest'))) + sys.argv[1:]
260 self.runner = pycmd2argv(iptest_app) + sys.argv[1:]
261 else:
261 else:
262 # Find our own 'iptest' script OS-level entry point. Don't
262 # Find our own 'iptest' script OS-level entry point. Don't
263 # look system-wide, so we are sure we pick up *this one*. And
263 # look system-wide, so we are sure we pick up *this one*. And
264 # pass through to subprocess call our own sys.argv
264 # pass through to subprocess call our own sys.argv
265 ippath = p.abspath(p.join(p.dirname(__file__),'..','..'))
265 ippath = p.abspath(p.join(p.dirname(__file__),'..','..'))
266 script = p.join(ippath, 'iptest.py')
266 script = p.join(ippath, 'iptest.py')
267 self.runner = tools.cmd2argv(script) + sys.argv[1:]
267 self.runner = pycmd2argv(script) + sys.argv[1:]
268
268
269 else:
269 else:
270 # For trial, it needs to be installed system-wide
270 # For trial, it needs to be installed system-wide
271 self.runner = tools.cmd2argv(p.abspath(find_cmd('trial')))
271 self.runner = pycmd2argv(p.abspath(find_cmd('trial')))
272 if params is None:
272 if params is None:
273 params = []
273 params = []
274 if isinstance(params, str):
274 if isinstance(params, str):
@@ -31,7 +31,6 b' from __future__ import absolute_import'
31 # the file COPYING, distributed as part of this software.
31 # the file COPYING, distributed as part of this software.
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33
33
34
35 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
36 # Imports
35 # Imports
37 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
@@ -38,7 +38,7 b' import tempfile'
38
38
39 # IPython-specific libraries
39 # IPython-specific libraries
40 from IPython.lib import irunner
40 from IPython.lib import irunner
41 from IPython.utils.genutils import fatal
41 from IPython.utils.warn import fatal
42
42
43 class IndentOut(object):
43 class IndentOut(object):
44 """A simple output stream that indents all output by a fixed amount.
44 """A simple output stream that indents all output by a fixed amount.
@@ -5,10 +5,25 b' Once this is fixed in upstream nose we can disable it.'
5
5
6 Note: merely importing this module causes the monkeypatch to be applied."""
6 Note: merely importing this module causes the monkeypatch to be applied."""
7
7
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2009 The IPython Development Team
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
8 import unittest
19 import unittest
9 import nose.loader
20 import nose.loader
10 from inspect import ismethod, isfunction
21 from inspect import ismethod, isfunction
11
22
23 #-----------------------------------------------------------------------------
24 # Classes and functions
25 #-----------------------------------------------------------------------------
26
12 def getTestCaseNames(self, testCaseClass):
27 def getTestCaseNames(self, testCaseClass):
13 """Override to select with selector, unless
28 """Override to select with selector, unless
14 config.getTestCaseNamesCompat is True
29 config.getTestCaseNamesCompat is True
@@ -5,10 +5,27 b" parametric code. We just need to double-check that the new code doesn't clash"
5 with Twisted (we know it works with nose and unittest).
5 with Twisted (we know it works with nose and unittest).
6 """
6 """
7
7
8 __all__ = ['parametric','Parametric']
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2009 The IPython Development Team
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
9
19
10 from twisted.trial.unittest import TestCase
20 from twisted.trial.unittest import TestCase
11
21
22 #-----------------------------------------------------------------------------
23 # Classes and functions
24 #-----------------------------------------------------------------------------
25
26 __all__ = ['parametric','Parametric']
27
28
12 def partial(f, *partial_args, **partial_kwargs):
29 def partial(f, *partial_args, **partial_kwargs):
13 """Generate a partial class method.
30 """Generate a partial class method.
14
31
@@ -20,6 +37,7 b' def partial(f, *partial_args, **partial_kwargs):'
20
37
21 return partial_func
38 return partial_func
22
39
40
23 def parametric(f):
41 def parametric(f):
24 """Mark f as a parametric test.
42 """Mark f as a parametric test.
25
43
@@ -27,6 +45,7 b' def parametric(f):'
27 f._parametric = True
45 f._parametric = True
28 return classmethod(f)
46 return classmethod(f)
29
47
48
30 def Parametric(cls):
49 def Parametric(cls):
31 """Register parametric tests with a class.
50 """Register parametric tests with a class.
32
51
@@ -56,3 +75,4 b' def Parametric(cls):'
56
75
57 # rename test generator so it isn't called again by nose
76 # rename test generator so it isn't called again by nose
58 test_gen.im_func.func_name = '__done_' + test_name
77 test_gen.im_func.func_name = '__done_' + test_name
78
@@ -15,22 +15,22 b' Authors'
15 - Fernando Perez <Fernando.Perez@berkeley.edu>
15 - Fernando Perez <Fernando.Perez@berkeley.edu>
16 """
16 """
17
17
18 #*****************************************************************************
18 from __future__ import absolute_import
19 # Copyright (C) 2009 The IPython Development Team
19
20 #-----------------------------------------------------------------------------
21 # Copyright (C) 2009 The IPython Development Team
20 #
22 #
21 # Distributed under the terms of the BSD License. The full license is in
23 # Distributed under the terms of the BSD License. The full license is in
22 # the file COPYING, distributed as part of this software.
24 # the file COPYING, distributed as part of this software.
23 #*****************************************************************************
25 #-----------------------------------------------------------------------------
24
26
25 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
26 # Required modules and packages
28 # Imports
27 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
28 from __future__ import absolute_import
29
30
30 import os
31 import os
31 import re
32 import re
32 import sys
33 import sys
33 import tempfile
34
34
35 try:
35 try:
36 # These tools are used by parts of the runtime, so we make the nose
36 # These tools are used by parts of the runtime, so we make the nose
@@ -41,7 +41,9 b' try:'
41 except ImportError:
41 except ImportError:
42 has_nose = False
42 has_nose = False
43
43
44 from IPython.utils import genutils, platutils
44 from IPython.utils.process import find_cmd, getoutputerror
45 from IPython.utils.text import list_strings
46 from IPython.utils.io import temp_pyfile
45
47
46 from . import decorators as dec
48 from . import decorators as dec
47
49
@@ -107,7 +109,7 b' def full_path(startPath,files):'
107 ['/a.txt']
109 ['/a.txt']
108 """
110 """
109
111
110 files = genutils.list_strings(files)
112 files = list_strings(files)
111 base = os.path.split(startPath)[0]
113 base = os.path.split(startPath)[0]
112 return [ os.path.join(base,f) for f in files ]
114 return [ os.path.join(base,f) for f in files ]
113
115
@@ -156,63 +158,6 b' def parse_test_output(txt):'
156 parse_test_output.__test__ = False
158 parse_test_output.__test__ = False
157
159
158
160
159 def cmd2argv(cmd):
160 r"""Take the path of a command and return a list (argv-style).
161
162 For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
163 .com or .bat, and ['python', cmd] otherwise.
164
165 This is mostly a Windows utility, to deal with the fact that the scripts in
166 Windows get wrapped in .exe entry points, so we have to call them
167 differently.
168
169 Parameters
170 ----------
171 cmd : string
172 The path of the command.
173
174 Returns
175 -------
176 argv-style list.
177
178 Examples
179 --------
180 In [2]: cmd2argv('/usr/bin/ipython')
181 Out[2]: ['python', '/usr/bin/ipython']
182
183 In [3]: cmd2argv(r'C:\Python26\Scripts\ipython.exe')
184 Out[3]: ['C:\\Python26\\Scripts\\ipython.exe']
185 """
186 ext = os.path.splitext(cmd)[1]
187 if ext in ['.exe', '.com', '.bat']:
188 return [cmd]
189 else:
190 return ['python', cmd]
191
192
193 def temp_pyfile(src, ext='.py'):
194 """Make a temporary python file, return filename and filehandle.
195
196 Parameters
197 ----------
198 src : string or list of strings (no need for ending newlines if list)
199 Source code to be written to the file.
200
201 ext : optional, string
202 Extension for the generated file.
203
204 Returns
205 -------
206 (filename, open filehandle)
207 It is the caller's responsibility to close the open file and unlink it.
208 """
209 fname = tempfile.mkstemp(ext)[1]
210 f = open(fname,'w')
211 f.write(src)
212 f.flush()
213 return fname, f
214
215
216 def default_argv():
161 def default_argv():
217 """Return a valid default argv for creating testing instances of ipython"""
162 """Return a valid default argv for creating testing instances of ipython"""
218
163
@@ -256,7 +201,7 b' def ipexec(fname, options=None):'
256 # suite can be run from the source tree without an installed IPython
201 # suite can be run from the source tree without an installed IPython
257 p = os.path
202 p = os.path
258 if INSTALLED:
203 if INSTALLED:
259 ipython_cmd = platutils.find_cmd('ipython')
204 ipython_cmd = find_cmd('ipython')
260 else:
205 else:
261 ippath = p.abspath(p.join(p.dirname(__file__),'..','..'))
206 ippath = p.abspath(p.join(p.dirname(__file__),'..','..'))
262 ipython_script = p.join(ippath, 'ipython.py')
207 ipython_script = p.join(ippath, 'ipython.py')
@@ -265,7 +210,7 b' def ipexec(fname, options=None):'
265 full_fname = p.join(test_dir, fname)
210 full_fname = p.join(test_dir, fname)
266 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
211 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
267 #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
212 #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
268 return genutils.getoutputerror(full_cmd)
213 return getoutputerror(full_cmd)
269
214
270
215
271 def ipexec_validate(fname, expected_out, expected_err='',
216 def ipexec_validate(fname, expected_out, expected_err='',
@@ -1,23 +1,24 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """This file contains utility classes for performing tests with Deferreds.
2 """This file contains utility classes for performing tests with Deferreds.
3 """
3 """
4 __docformat__ = "restructuredtext en"
4 #-----------------------------------------------------------------------------
5 #-------------------------------------------------------------------------------
5 # Copyright (C) 2009 The IPython Development Team
6 # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu>
7 # Brian E Granger <ellisonbg@gmail.com>
8 # Benjamin Ragan-Kelley <benjaminrk@gmail.com>
9 #
6 #
10 # Distributed under the terms of the BSD License. The full license is in
7 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
8 # the file COPYING, distributed as part of this software.
12 #-------------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
13
10
14 #-------------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
15 # Imports
12 # Imports
16 #-------------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
17
14
18 from twisted.trial import unittest
15 from twisted.trial import unittest
19 from twisted.internet import defer
16 from twisted.internet import defer
20
17
18 #-----------------------------------------------------------------------------
19 # Classes and functions
20 #-----------------------------------------------------------------------------
21
21 class DeferredTestCase(unittest.TestCase):
22 class DeferredTestCase(unittest.TestCase):
22
23
23 def assertDeferredEquals(self, deferred, expectedResult,
24 def assertDeferredEquals(self, deferred, expectedResult,
@@ -1,8 +1,9 b''
1 # encoding: utf-8
1 """Generic functions for extending IPython.
2 """Generic functions for extending IPython.
2
3
3 See http://cheeseshop.python.org/pypi/simplegeneric.
4 See http://cheeseshop.python.org/pypi/simplegeneric.
4
5
5 Here is an example from genutils.py::
6 Here is an example from IPython.utils.text::
6
7
7 def print_lsstring(arg):
8 def print_lsstring(arg):
8 "Prettier (non-repr-like) and more informative printer for LSString"
9 "Prettier (non-repr-like) and more informative printer for LSString"
@@ -12,19 +13,37 b' Here is an example from genutils.py::'
12 print_lsstring = result_display.when_type(LSString)(print_lsstring)
13 print_lsstring = result_display.when_type(LSString)(print_lsstring)
13 """
14 """
14
15
16 #-----------------------------------------------------------------------------
17 # Copyright (C) 2008-2009 The IPython Development Team
18 #
19 # Distributed under the terms of the BSD License. The full license is in
20 # the file COPYING, distributed as part of this software.
21 #-----------------------------------------------------------------------------
22
23 #-----------------------------------------------------------------------------
24 # Imports
25 #-----------------------------------------------------------------------------
26
15 from IPython.core.error import TryNext
27 from IPython.core.error import TryNext
16 from IPython.external.simplegeneric import generic
28 from IPython.external.simplegeneric import generic
17
29
30 #-----------------------------------------------------------------------------
31 # Imports
32 #-----------------------------------------------------------------------------
33
34
18 @generic
35 @generic
19 def result_display(result):
36 def result_display(result):
20 """Print the result of computation."""
37 """Print the result of computation."""
21 raise TryNext
38 raise TryNext
22
39
40
23 @generic
41 @generic
24 def inspect_object(obj):
42 def inspect_object(obj):
25 """Called when you do obj?"""
43 """Called when you do obj?"""
26 raise TryNext
44 raise TryNext
27
45
46
28 @generic
47 @generic
29 def complete_object(obj, prev_completions):
48 def complete_object(obj, prev_completions):
30 """Custom completer dispatching for python objects.
49 """Custom completer dispatching for python objects.
@@ -41,3 +60,5 b' def complete_object(obj, prev_completions):'
41 own_attrs + prev_completions.
60 own_attrs + prev_completions.
42 """
61 """
43 raise TryNext
62 raise TryNext
63
64
@@ -1,4 +1,3 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
1 # encoding: utf-8
3 """
2 """
4 A simple utility to import something by its string name.
3 A simple utility to import something by its string name.
@@ -19,9 +19,7 b' Authors:'
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import pprint
22 from IPython.utils.data import list2dict2
23
24 from IPython.utils.genutils import list2dict2
25
23
26 __all__ = ['Struct']
24 __all__ = ['Struct']
27
25
@@ -37,7 +37,6 b' from IPython.external.path import path as Path'
37 import os,stat,time
37 import os,stat,time
38 import cPickle as pickle
38 import cPickle as pickle
39 import UserDict
39 import UserDict
40 import warnings
41 import glob
40 import glob
42
41
43 def gethashfile(key):
42 def gethashfile(key):
@@ -1,10 +1,12 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ Imports and provides the 'correct' version of readline for the platform.
2 """ Imports and provides the 'correct' version of readline for the platform.
3
3
4 Readline is used throughout IPython as 'import IPython.utils.rlineimpl as readline'.
4 Readline is used throughout IPython as::
5
6 import IPython.utils.rlineimpl as readline
5
7
6 In addition to normal readline stuff, this module provides have_readline
8 In addition to normal readline stuff, this module provides have_readline
7 boolean and _outputfile variable used in genutils.
9 boolean and _outputfile variable used in IPython.utils.
8 """
10 """
9
11
10 import sys
12 import sys
@@ -19,9 +19,12 b' Authors:'
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22
23 import sys
22 import sys
24
23
24 #-----------------------------------------------------------------------------
25 # Code
26 #-----------------------------------------------------------------------------
27
25 class appended_to_syspath(object):
28 class appended_to_syspath(object):
26 """A context for appending a directory to sys.path for a second."""
29 """A context for appending a directory to sys.path for a second."""
27
30
@@ -3,21 +3,12 b''
3 def test_import_coloransi():
3 def test_import_coloransi():
4 from IPython.utils import coloransi
4 from IPython.utils import coloransi
5
5
6 def test_import_DPyGetOpt():
7 from IPython.utils import DPyGetOpt
8
9 def test_import_generics():
6 def test_import_generics():
10 from IPython.utils import generics
7 from IPython.utils import generics
11
8
12 def test_import_genutils():
13 from IPython.utils import genutils
14
15 def test_import_ipstruct():
9 def test_import_ipstruct():
16 from IPython.utils import ipstruct
10 from IPython.utils import ipstruct
17
11
18 def test_import_platutils():
19 from IPython.utils import platutils
20
21 def test_import_PyColorize():
12 def test_import_PyColorize():
22 from IPython.utils import PyColorize
13 from IPython.utils import PyColorize
23
14
@@ -33,5 +24,3 b' def test_import_upgradedir():'
33 def test_import_wildcard():
24 def test_import_wildcard():
34 from IPython.utils import wildcard
25 from IPython.utils import wildcard
35
26
36 def test_import_winconsole():
37 from IPython.utils import winconsole
@@ -15,11 +15,7 b''
15
15
16 import unittest
16 import unittest
17
17
18 from IPython.utils.notification import (
18 from IPython.utils.notification import shared_center
19 NotificationCenter,
20 NotificationError,
21 shared_center
22 )
23
19
24 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
25 # Support Classes
21 # Support Classes
@@ -1,8 +1,5 b''
1 # encoding: utf-8
1 # encoding: utf-8
2
2 """Tests for IPython.utils.path.py"""
3 """Tests for genutils.py"""
4
5 __docformat__ = "restructuredtext en"
6
3
7 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008 The IPython Development Team
5 # Copyright (C) 2008 The IPython Development Team
@@ -15,27 +12,29 b' __docformat__ = "restructuredtext en"'
15 # Imports
12 # Imports
16 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
17
14
18 # stdlib
19 import os
15 import os
20 import shutil
16 import shutil
21 import sys
17 import sys
22 import tempfile
18 import tempfile
23 import unittest
24
19
25 from cStringIO import StringIO
26 from os.path import join, abspath, split
20 from os.path import join, abspath, split
27
21
28 # third-party
29 import nose.tools as nt
22 import nose.tools as nt
30
23
31 from nose import with_setup
24 from nose import with_setup
32 from nose.tools import raises
33
25
34 # Our own
35 import IPython
26 import IPython
36 from IPython.utils import genutils
37 from IPython.testing import decorators as dec
27 from IPython.testing import decorators as dec
38 from IPython.testing.decorators import skipif, skip_if_not_win32
28 from IPython.testing.decorators import skip_if_not_win32
29 from IPython.utils.path import (
30 get_home_dir,
31 HomeDirError,
32 get_ipython_dir,
33 get_ipython_package_dir,
34 get_ipython_module_path,
35 filefind,
36 get_long_path_name
37 )
39
38
40 # Platform-dependent imports
39 # Platform-dependent imports
41 try:
40 try:
@@ -88,7 +87,7 b' def setup_environment():'
88 each testfunction needs a pristine environment.
87 each testfunction needs a pristine environment.
89 """
88 """
90 global oldstuff, platformstuff
89 global oldstuff, platformstuff
91 oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__)
90 oldstuff = (env.copy(), os.name, get_home_dir, IPython.__file__)
92
91
93 if os.name == 'nt':
92 if os.name == 'nt':
94 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
93 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
@@ -97,7 +96,7 b' def setup_environment():'
97 def teardown_environment():
96 def teardown_environment():
98 """Restore things that were remebered by the setup_environment function
97 """Restore things that were remebered by the setup_environment function
99 """
98 """
100 (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff
99 (oldenv, os.name, get_home_dir, IPython.__file__,) = oldstuff
101
100
102 for key in env.keys():
101 for key in env.keys():
103 if key not in oldenv:
102 if key not in oldenv:
@@ -112,10 +111,6 b' def teardown_environment():'
112 with_environment = with_setup(setup_environment, teardown_environment)
111 with_environment = with_setup(setup_environment, teardown_environment)
113
112
114
113
115 #
116 # Tests for get_home_dir
117 #
118
119 @skip_if_not_win32
114 @skip_if_not_win32
120 @with_environment
115 @with_environment
121 def test_get_home_dir_1():
116 def test_get_home_dir_1():
@@ -126,7 +121,7 b' def test_get_home_dir_1():'
126 #fake filename for IPython.__init__
121 #fake filename for IPython.__init__
127 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
122 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
128
123
129 home_dir = genutils.get_home_dir()
124 home_dir = get_home_dir()
130 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
125 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
131
126
132 @skip_if_not_win32
127 @skip_if_not_win32
@@ -138,14 +133,14 b' def test_get_home_dir_2():'
138 #fake filename for IPython.__init__
133 #fake filename for IPython.__init__
139 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
134 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
140
135
141 home_dir = genutils.get_home_dir()
136 home_dir = get_home_dir()
142 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
137 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
143
138
144 @with_environment
139 @with_environment
145 def test_get_home_dir_3():
140 def test_get_home_dir_3():
146 """Testcase $HOME is set, then use its value as home directory."""
141 """Testcase $HOME is set, then use its value as home directory."""
147 env["HOME"] = HOME_TEST_DIR
142 env["HOME"] = HOME_TEST_DIR
148 home_dir = genutils.get_home_dir()
143 home_dir = get_home_dir()
149 nt.assert_equal(home_dir, env["HOME"])
144 nt.assert_equal(home_dir, env["HOME"])
150
145
151 @with_environment
146 @with_environment
@@ -155,7 +150,7 b' def test_get_home_dir_4():'
155
150
156 os.name = 'posix'
151 os.name = 'posix'
157 if 'HOME' in env: del env['HOME']
152 if 'HOME' in env: del env['HOME']
158 nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir)
153 nt.assert_raises(HomeDirError, get_home_dir)
159
154
160 @skip_if_not_win32
155 @skip_if_not_win32
161 @with_environment
156 @with_environment
@@ -167,7 +162,7 b' def test_get_home_dir_5():'
167 if 'HOME' in env: del env['HOME']
162 if 'HOME' in env: del env['HOME']
168 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
163 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
169
164
170 home_dir = genutils.get_home_dir()
165 home_dir = get_home_dir()
171 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
166 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
172
167
173 @skip_if_not_win32
168 @skip_if_not_win32
@@ -183,7 +178,7 b' def test_get_home_dir_6():'
183 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST"
178 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST"
184 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
179 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
185
180
186 home_dir = genutils.get_home_dir()
181 home_dir = get_home_dir()
187 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
182 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
188
183
189 # Should we stub wreg fully so we can run the test on all platforms?
184 # Should we stub wreg fully so we can run the test on all platforms?
@@ -211,116 +206,55 b' def test_get_home_dir_7():'
211 wreg.OpenKey = OpenKey
206 wreg.OpenKey = OpenKey
212 wreg.QueryValueEx = QueryValueEx
207 wreg.QueryValueEx = QueryValueEx
213
208
214 home_dir = genutils.get_home_dir()
209 home_dir = get_home_dir()
215 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
210 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
216
211
217 #
218 # Tests for get_ipython_dir
219 #
220
212
221 @with_environment
213 @with_environment
222 def test_get_ipython_dir_1():
214 def test_get_ipython_dir_1():
223 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
215 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
224 env['IPYTHON_DIR'] = "someplace/.ipython"
216 env['IPYTHON_DIR'] = "someplace/.ipython"
225 ipdir = genutils.get_ipython_dir()
217 ipdir = get_ipython_dir()
226 nt.assert_equal(ipdir, "someplace/.ipython")
218 nt.assert_equal(ipdir, "someplace/.ipython")
227
219
228
220
229 @with_environment
221 @with_environment
230 def test_get_ipython_dir_2():
222 def test_get_ipython_dir_2():
231 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
223 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
232 genutils.get_home_dir = lambda : "someplace"
224 get_home_dir = lambda : "someplace"
233 os.name = "posix"
225 os.name = "posix"
234 env.pop('IPYTHON_DIR', None)
226 env.pop('IPYTHON_DIR', None)
235 env.pop('IPYTHONDIR', None)
227 env.pop('IPYTHONDIR', None)
236 ipdir = genutils.get_ipython_dir()
228 ipdir = get_ipython_dir()
237 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
229 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
238
230
239 #
240 # Tests for popkey
241 #
242
243 def test_popkey_1():
244 """test_popkey_1, Basic usage test of popkey
245 """
246 dct = dict(a=1, b=2, c=3)
247 nt.assert_equal(genutils.popkey(dct, "a"), 1)
248 nt.assert_equal(dct, dict(b=2, c=3))
249 nt.assert_equal(genutils.popkey(dct, "b"), 2)
250 nt.assert_equal(dct, dict(c=3))
251 nt.assert_equal(genutils.popkey(dct, "c"), 3)
252 nt.assert_equal(dct, dict())
253
254 def test_popkey_2():
255 """test_popkey_2, Test to see that popkey of non occuring keys
256 generates a KeyError exception
257 """
258 dct = dict(a=1, b=2, c=3)
259 nt.assert_raises(KeyError, genutils.popkey, dct, "d")
260
261 def test_popkey_3():
262 """test_popkey_3, Tests to see that popkey calls returns the correct value
263 and that the key/value was removed from the dict.
264 """
265 dct = dict(a=1, b=2, c=3)
266 nt.assert_equal(genutils.popkey(dct, "A", 13), 13)
267 nt.assert_equal(dct, dict(a=1, b=2, c=3))
268 nt.assert_equal(genutils.popkey(dct, "B", 14), 14)
269 nt.assert_equal(dct, dict(a=1, b=2, c=3))
270 nt.assert_equal(genutils.popkey(dct, "C", 15), 15)
271 nt.assert_equal(dct, dict(a=1, b=2, c=3))
272 nt.assert_equal(genutils.popkey(dct, "a"), 1)
273 nt.assert_equal(dct, dict(b=2, c=3))
274 nt.assert_equal(genutils.popkey(dct, "b"), 2)
275 nt.assert_equal(dct, dict(c=3))
276 nt.assert_equal(genutils.popkey(dct, "c"), 3)
277 nt.assert_equal(dct, dict())
278
279
231
280 def test_filefind():
232 def test_filefind():
281 """Various tests for filefind"""
233 """Various tests for filefind"""
282 f = tempfile.NamedTemporaryFile()
234 f = tempfile.NamedTemporaryFile()
283 print 'fname:',f.name
235 print 'fname:',f.name
284 alt_dirs = genutils.get_ipython_dir()
236 alt_dirs = get_ipython_dir()
285 t = genutils.filefind(f.name,alt_dirs)
237 t = filefind(f.name, alt_dirs)
286 print 'found:',t
238 print 'found:',t
287
239
288
240
289 def test_get_ipython_package_dir():
241 def test_get_ipython_package_dir():
290 ipdir = genutils.get_ipython_package_dir()
242 ipdir = get_ipython_package_dir()
291 nt.assert_true(os.path.isdir(ipdir))
243 nt.assert_true(os.path.isdir(ipdir))
292
244
245 def test_get_ipython_module_path():
246 ipapp_path = get_ipython_module_path('IPython.core.ipapp')
247 nt.assert_true(os.path.isfile(ipapp_path))
293
248
294 def test_tee_simple():
249 @dec.skip_if_not_win32
295 "Very simple check with stdout only"
250 def test_get_long_path_name_win32():
296 chan = StringIO()
251 p = get_long_path_name('c:\\docume~1')
297 text = 'Hello'
252 nt.assert_equals(p,u'c:\\Documents and Settings')
298 tee = genutils.Tee(chan, channel='stdout')
299 print >> chan, text,
300 nt.assert_equal(chan.getvalue(), text)
301
253
254
255 @dec.skip_win32
256 def test_get_long_path_name():
257 p = get_long_path_name('/usr/local')
258 nt.assert_equals(p,'/usr/local')
302
259
303 class TeeTestCase(dec.ParametricTestCase):
304
260
305 def tchan(self, channel, check='close'):
306 trap = StringIO()
307 chan = StringIO()
308 text = 'Hello'
309
310 std_ori = getattr(sys, channel)
311 setattr(sys, channel, trap)
312
313 tee = genutils.Tee(chan, channel=channel)
314 print >> chan, text,
315 setattr(sys, channel, std_ori)
316 trap_val = trap.getvalue()
317 nt.assert_equals(chan.getvalue(), text)
318 if check=='close':
319 tee.close()
320 else:
321 del tee
322
323 def test(self):
324 for chan in ['stdout', 'stderr']:
325 for check in ['close', 'del']:
326 yield self.tchan(chan, check)
@@ -14,12 +14,11 b' Tests for platutils.py'
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import os
18 import sys
17 import sys
19
18
20 import nose.tools as nt
19 import nose.tools as nt
21
20
22 from IPython.utils.platutils import find_cmd, FindCmdError, get_long_path_name
21 from IPython.utils.process import find_cmd, FindCmdError
23 from IPython.testing import decorators as dec
22 from IPython.testing import decorators as dec
24
23
25 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
@@ -60,13 +59,4 b' def test_find_cmd_fail():'
60 nt.assert_raises(FindCmdError,find_cmd,'asdfasdf')
59 nt.assert_raises(FindCmdError,find_cmd,'asdfasdf')
61
60
62
61
63 @dec.skip_if_not_win32
64 def test_get_long_path_name_win32():
65 p = get_long_path_name('c:\\docume~1')
66 nt.assert_equals(p,u'c:\\Documents and Settings')
67
62
68
69 @dec.skip_win32
70 def test_get_long_path_name():
71 p = get_long_path_name('/usr/local')
72 nt.assert_equals(p,'/usr/local')
@@ -22,15 +22,11 b' Authors:'
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 import sys
26 import os
27
28
29 from unittest import TestCase
25 from unittest import TestCase
30
26
31 from IPython.utils.traitlets import (
27 from IPython.utils.traitlets import (
32 HasTraitlets, MetaHasTraitlets, TraitletType, Any,
28 HasTraitlets, MetaHasTraitlets, TraitletType, Any,
33 Int, Long, Float, Complex, Str, Unicode, Bool, TraitletError,
29 Int, Long, Float, Complex, Str, Unicode, TraitletError,
34 Undefined, Type, This, Instance
30 Undefined, Type, This, Instance
35 )
31 )
36
32
@@ -11,7 +11,7 b' try:'
11 except ImportError:
11 except ImportError:
12 from path import path
12 from path import path
13
13
14 import md5,pickle
14 import md5, pickle
15
15
16 def showdiff(old,new):
16 def showdiff(old,new):
17 import difflib
17 import difflib
@@ -14,13 +14,10 b' Authors'
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 import __builtin__
16 import __builtin__
17 import exceptions
18 import pdb
19 import pprint
20 import re
17 import re
21 import types
18 import types
22
19
23 from IPython.utils.genutils import dir2
20 from IPython.utils.dir2 import dir2
24
21
25 def create_typestr2type_dicts(dont_include_in_type2type2str=["lambda"]):
22 def create_typestr2type_dicts(dont_include_in_type2type2str=["lambda"]):
26 """Return dictionaries mapping lower case typename to type objects, from
23 """Return dictionaries mapping lower case typename to type objects, from
@@ -18,7 +18,7 b' overhead of a single task is about 0.001-0.01 seconds.'
18 import random, sys
18 import random, sys
19 from optparse import OptionParser
19 from optparse import OptionParser
20
20
21 from IPython.genutils import time
21 from IPython.utils.timing import time
22 from IPython.kernel import client
22 from IPython.kernel import client
23
23
24 def main():
24 def main():
@@ -51,7 +51,7 b' def main():'
51 print tc.task_controller
51 print tc.task_controller
52 rc.block=True
52 rc.block=True
53 nengines = len(rc.get_ids())
53 nengines = len(rc.get_ids())
54 rc.execute('from IPython.genutils import time')
54 rc.execute('from IPython.utils.timing import time')
55
55
56 # the jobs should take a random time within a range
56 # the jobs should take a random time within a range
57 times = [random.random()*(opts.tmax-opts.tmin)+opts.tmin for i in range(opts.n)]
57 times = [random.random()*(opts.tmax-opts.tmin)+opts.tmin for i in range(opts.n)]
@@ -245,7 +245,7 b' this directory is determined by the following algorithm:'
245
245
246 * If the ``--ipython-dir`` command line flag is given, its value is used.
246 * If the ``--ipython-dir`` command line flag is given, its value is used.
247
247
248 * If not, the value returned by :func:`IPython.utils.genutils.get_ipython_dir`
248 * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir`
249 is used. This function will first look at the :envvar:`IPYTHON_DIR`
249 is used. This function will first look at the :envvar:`IPYTHON_DIR`
250 environment variable and then default to the directory
250 environment variable and then default to the directory
251 :file:`$HOME/.ipython`.
251 :file:`$HOME/.ipython`.
@@ -41,8 +41,8 b' A bit of Python code::'
41
41
42 An interactive Python session::
42 An interactive Python session::
43
43
44 >>> from IPython import genutils
44 >>> from IPython.utils.path import get_ipython_dir
45 >>> genutils.get_ipython_dir()
45 >>> get_ipython_dir()
46 '/home/fperez/.ipython'
46 '/home/fperez/.ipython'
47
47
48 An IPython session:
48 An IPython session:
@@ -166,7 +166,7 b' ipy_user_conf.py.'
166 String lists
166 String lists
167 ============
167 ============
168
168
169 String lists (IPython.genutils.SList) are handy way to process output
169 String lists (IPython.utils.text.SList) are handy way to process output
170 from system commands. They are produced by ``var = !cmd`` syntax.
170 from system commands. They are produced by ``var = !cmd`` syntax.
171
171
172 First, we acquire the output of 'ls -l'::
172 First, we acquire the output of 'ls -l'::
@@ -8,7 +8,7 b' import re'
8 import pydoc
8 import pydoc
9 from StringIO import StringIO
9 from StringIO import StringIO
10 from warnings import warn
10 from warnings import warn
11 4
11
12 class Reader(object):
12 class Reader(object):
13 """A line-based string reader.
13 """A line-based string reader.
14
14
@@ -81,7 +81,7 b" matplotlib.use('Agg')"
81
81
82 # Our own
82 # Our own
83 from IPython import Config, IPythonApp
83 from IPython import Config, IPythonApp
84 from IPython.utils.genutils import Term, Tee
84 from IPython.utils.io import Term, Tee
85
85
86 #-----------------------------------------------------------------------------
86 #-----------------------------------------------------------------------------
87 # Globals
87 # Globals
@@ -208,8 +208,9 b' class EmbeddedSphinxShell(object):'
208 Term.cerr = self.cout
208 Term.cerr = self.cout
209
209
210 # For debugging, so we can see normal output, use this:
210 # For debugging, so we can see normal output, use this:
211 #Term.cout = genutils.Tee(self.cout, channel='stdout') # dbg
211 # from IPython.utils.io import Tee
212 #Term.cerr = genutils.Tee(self.cout, channel='stderr') # dbg
212 #Term.cout = Tee(self.cout, channel='stdout') # dbg
213 #Term.cerr = Tee(self.cout, channel='stderr') # dbg
213
214
214 # Create config object for IPython
215 # Create config object for IPython
215 config = Config()
216 config = Config()
@@ -47,7 +47,7 b" if os.path.exists('MANIFEST'): os.remove('MANIFEST')"
47 from distutils.core import setup
47 from distutils.core import setup
48
48
49 # Our own imports
49 # Our own imports
50 from IPython.utils.genutils import target_update
50 from IPython.utils.path import target_update
51
51
52 from setupbase import (
52 from setupbase import (
53 setup_args,
53 setup_args,
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (690 lines changed) Show them Hide them
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (1894 lines changed) Show them Hide them
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now