Show More
@@ -1,730 +1,731 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.1 or better. |
|
6 | 6 | |
|
7 | 7 | This file contains the main make_IPython() starter function. |
|
8 | 8 | |
|
9 |
$Id: ipmaker.py 92 |
|
|
9 | $Id: ipmaker.py 925 2005-11-15 20:39:39Z fperez $""" | |
|
10 | 10 | |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu> |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #***************************************************************************** |
|
17 | 17 | |
|
18 | 18 | from IPython import Release |
|
19 | 19 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | __version__ = Release.version |
|
22 | 22 | |
|
23 | 23 | credits._Printer__data = """ |
|
24 | 24 | Python: %s |
|
25 | 25 | |
|
26 | 26 | IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users. |
|
27 | 27 | See http://ipython.scipy.org for more information.""" \ |
|
28 | 28 | % credits._Printer__data |
|
29 | 29 | |
|
30 | 30 | copyright._Printer__data += """ |
|
31 | 31 | |
|
32 | 32 | Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray. |
|
33 | 33 | All Rights Reserved.""" |
|
34 | 34 | |
|
35 | 35 | #**************************************************************************** |
|
36 | 36 | # Required modules |
|
37 | 37 | |
|
38 | 38 | # From the standard library |
|
39 | 39 | import __main__, __builtin__ |
|
40 | 40 | import os,sys,types,re |
|
41 | 41 | from pprint import pprint,pformat |
|
42 | 42 | |
|
43 | 43 | # Our own |
|
44 | 44 | from IPython import DPyGetOpt |
|
45 | 45 | from IPython.Struct import Struct |
|
46 | 46 | from IPython.OutputTrap import OutputTrap |
|
47 | 47 | from IPython.ConfigLoader import ConfigLoader |
|
48 | 48 | from IPython.iplib import InteractiveShell,qw_lol,import_fail_info |
|
49 | 49 | from IPython.usage import cmd_line_usage,interactive_usage |
|
50 | 50 | from IPython.Prompts import CachedOutput |
|
51 | 51 | from IPython.genutils import * |
|
52 | 52 | |
|
53 | 53 | #----------------------------------------------------------------------------- |
|
54 | 54 | def make_IPython(argv=None,user_ns=None,debug=1,rc_override=None, |
|
55 | 55 | shell_class=InteractiveShell,embedded=False,**kw): |
|
56 | 56 | """This is a dump of IPython into a single function. |
|
57 | 57 | |
|
58 | 58 | Later it will have to be broken up in a sensible manner. |
|
59 | 59 | |
|
60 | 60 | Arguments: |
|
61 | 61 | |
|
62 | 62 | - argv: a list similar to sys.argv[1:]. It should NOT contain the desired |
|
63 | 63 | script name, b/c DPyGetOpt strips the first argument only for the real |
|
64 | 64 | sys.argv. |
|
65 | 65 | |
|
66 | 66 | - user_ns: a dict to be used as the user's namespace.""" |
|
67 | 67 | |
|
68 | 68 | #---------------------------------------------------------------------- |
|
69 | 69 | # Defaults and initialization |
|
70 | 70 | |
|
71 | 71 | # For developer debugging, deactivates crash handler and uses pdb. |
|
72 | 72 | DEVDEBUG = False |
|
73 | 73 | |
|
74 | 74 | if argv is None: |
|
75 | 75 | argv = sys.argv |
|
76 | 76 | |
|
77 | 77 | # __IP is the main global that lives throughout and represents the whole |
|
78 | 78 | # application. If the user redefines it, all bets are off as to what |
|
79 | 79 | # happens. |
|
80 | 80 | |
|
81 | 81 | # __IP is the name of he global which the caller will have accessible as |
|
82 | 82 | # __IP.name. We set its name via the first parameter passed to |
|
83 | 83 | # InteractiveShell: |
|
84 | 84 | |
|
85 | 85 | IP = shell_class('__IP',user_ns=user_ns,embedded=embedded,**kw) |
|
86 | 86 | |
|
87 | 87 | # Put 'help' in the user namespace |
|
88 | 88 | from site import _Helper |
|
89 | 89 | IP.user_ns['help'] = _Helper() |
|
90 | 90 | |
|
91 | 91 | if DEVDEBUG: |
|
92 | 92 | # For developer debugging only (global flag) |
|
93 | 93 | from IPython import ultraTB |
|
94 | 94 | sys.excepthook = ultraTB.VerboseTB(call_pdb=1) |
|
95 | 95 | else: |
|
96 | 96 | # IPython itself shouldn't crash. This will produce a detailed |
|
97 | 97 | # post-mortem if it does |
|
98 | 98 | from IPython import CrashHandler |
|
99 | 99 | sys.excepthook = CrashHandler.CrashHandler(IP) |
|
100 | 100 | |
|
101 | 101 | IP.BANNER_PARTS = ['Python %s\n' |
|
102 | 102 | 'Type "copyright", "credits" or "license" ' |
|
103 | 103 | 'for more information.\n' |
|
104 | 104 | % (sys.version.split('\n')[0],), |
|
105 | 105 | "IPython %s -- An enhanced Interactive Python." |
|
106 | 106 | % (__version__,), |
|
107 | 107 | """? -> Introduction to IPython's features. |
|
108 | 108 | %magic -> Information about IPython's 'magic' % functions. |
|
109 | 109 | help -> Python's own help system. |
|
110 | 110 | object? -> Details about 'object'. ?object also works, ?? prints more. |
|
111 | 111 | """ ] |
|
112 | 112 | |
|
113 | 113 | IP.usage = interactive_usage |
|
114 | 114 | |
|
115 | 115 | # Platform-dependent suffix and directory names. We use _ipython instead |
|
116 | 116 | # of .ipython under win32 b/c there's software that breaks with .named |
|
117 | 117 | # directories on that platform. |
|
118 | 118 | if os.name == 'posix': |
|
119 | 119 | rc_suffix = '' |
|
120 | 120 | ipdir_def = '.ipython' |
|
121 | 121 | else: |
|
122 | 122 | rc_suffix = '.ini' |
|
123 | 123 | ipdir_def = '_ipython' |
|
124 | 124 | |
|
125 | 125 | # default directory for configuration |
|
126 | 126 | ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR', |
|
127 | 127 | os.path.join(IP.home_dir,ipdir_def))) |
|
128 | 128 | |
|
129 | 129 | # we need the directory where IPython itself is installed |
|
130 | 130 | import IPython |
|
131 | 131 | IPython_dir = os.path.dirname(IPython.__file__) |
|
132 | 132 | del IPython |
|
133 | 133 | |
|
134 | 134 | #------------------------------------------------------------------------- |
|
135 | 135 | # Command line handling |
|
136 | 136 | |
|
137 | 137 | # Valid command line options (uses DPyGetOpt syntax, like Perl's |
|
138 | 138 | # GetOpt::Long) |
|
139 | 139 | |
|
140 | 140 | # Any key not listed here gets deleted even if in the file (like session |
|
141 | 141 | # or profile). That's deliberate, to maintain the rc namespace clean. |
|
142 | 142 | |
|
143 | 143 | # Each set of options appears twice: under _conv only the names are |
|
144 | 144 | # listed, indicating which type they must be converted to when reading the |
|
145 | 145 | # ipythonrc file. And under DPyGetOpt they are listed with the regular |
|
146 | 146 | # DPyGetOpt syntax (=s,=i,:f,etc). |
|
147 | 147 | |
|
148 | 148 | # Make sure there's a space before each end of line (they get auto-joined!) |
|
149 | 149 | cmdline_opts = ('autocall! autoindent! automagic! banner! cache_size|cs=i ' |
|
150 | 150 | 'c=s classic|cl color_info! colors=s confirm_exit! ' |
|
151 | 151 | 'debug! deep_reload! editor=s log|l messages! nosep pdb! ' |
|
152 | 152 | 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s ' |
|
153 | 153 | 'quick screen_length|sl=i prompts_pad_left=i ' |
|
154 | 154 | 'logfile|lf=s logplay|lp=s profile|p=s ' |
|
155 | 155 | 'readline! readline_merge_completions! ' |
|
156 | 156 | 'readline_omit__names! ' |
|
157 | 157 | 'rcfile=s separate_in|si=s separate_out|so=s ' |
|
158 | 158 | 'separate_out2|so2=s xmode=s wildcards_case_sensitive! ' |
|
159 | 159 | 'magic_docstrings system_verbose! ' |
|
160 | 160 | 'multi_line_specials!') |
|
161 | 161 | |
|
162 | 162 | # Options that can *only* appear at the cmd line (not in rcfiles). |
|
163 | 163 | |
|
164 | 164 | # The "ignore" option is a kludge so that Emacs buffers don't crash, since |
|
165 | 165 | # the 'C-c !' command in emacs automatically appends a -i option at the end. |
|
166 | 166 | cmdline_only = ('help ignore|i ipythondir=s Version upgrade ' |
|
167 | 167 | 'gthread! qthread! wthread! pylab! tk!') |
|
168 | 168 | |
|
169 | 169 | # Build the actual name list to be used by DPyGetOpt |
|
170 | 170 | opts_names = qw(cmdline_opts) + qw(cmdline_only) |
|
171 | 171 | |
|
172 | 172 | # Set sensible command line defaults. |
|
173 | 173 | # This should have everything from cmdline_opts and cmdline_only |
|
174 | 174 | opts_def = Struct(autocall = 1, |
|
175 | 175 | autoindent=0, |
|
176 | 176 | automagic = 1, |
|
177 | 177 | banner = 1, |
|
178 | 178 | cache_size = 1000, |
|
179 | 179 | c = '', |
|
180 | 180 | classic = 0, |
|
181 | 181 | colors = 'NoColor', |
|
182 | 182 | color_info = 0, |
|
183 | 183 | confirm_exit = 1, |
|
184 | 184 | debug = 0, |
|
185 | 185 | deep_reload = 0, |
|
186 | 186 | editor = '0', |
|
187 | 187 | help = 0, |
|
188 | 188 | ignore = 0, |
|
189 | 189 | ipythondir = ipythondir, |
|
190 | 190 | log = 0, |
|
191 | 191 | logfile = '', |
|
192 | 192 | logplay = '', |
|
193 | 193 | multi_line_specials = 1, |
|
194 | 194 | messages = 1, |
|
195 | 195 | nosep = 0, |
|
196 | 196 | pdb = 0, |
|
197 | 197 | pprint = 0, |
|
198 | 198 | profile = '', |
|
199 | 199 | prompt_in1 = 'In [\\#]: ', |
|
200 | 200 | prompt_in2 = ' .\\D.: ', |
|
201 | 201 | prompt_out = 'Out[\\#]: ', |
|
202 | 202 | prompts_pad_left = 1, |
|
203 | 203 | quick = 0, |
|
204 | 204 | readline = 1, |
|
205 | 205 | readline_merge_completions = 1, |
|
206 | 206 | readline_omit__names = 0, |
|
207 | 207 | rcfile = 'ipythonrc' + rc_suffix, |
|
208 | 208 | screen_length = 0, |
|
209 | 209 | separate_in = '\n', |
|
210 | 210 | separate_out = '\n', |
|
211 | 211 | separate_out2 = '', |
|
212 | 212 | system_verbose = 0, |
|
213 | 213 | gthread = 0, |
|
214 | 214 | qthread = 0, |
|
215 | 215 | wthread = 0, |
|
216 | 216 | pylab = 0, |
|
217 | 217 | tk = 0, |
|
218 | 218 | upgrade = 0, |
|
219 | 219 | Version = 0, |
|
220 | 220 | xmode = 'Verbose', |
|
221 | 221 | wildcards_case_sensitive = 1, |
|
222 | 222 | magic_docstrings = 0, # undocumented, for doc generation |
|
223 | 223 | ) |
|
224 | 224 | |
|
225 | 225 | # Things that will *only* appear in rcfiles (not at the command line). |
|
226 | 226 | # Make sure there's a space before each end of line (they get auto-joined!) |
|
227 | 227 | rcfile_opts = { qwflat: 'include import_mod import_all execfile ', |
|
228 | 228 | qw_lol: 'import_some ', |
|
229 | 229 | # for things with embedded whitespace: |
|
230 | 230 | list_strings:'execute alias readline_parse_and_bind ', |
|
231 | 231 | # Regular strings need no conversion: |
|
232 | 232 | None:'readline_remove_delims ', |
|
233 | 233 | } |
|
234 | 234 | # Default values for these |
|
235 | 235 | rc_def = Struct(include = [], |
|
236 | 236 | import_mod = [], |
|
237 | 237 | import_all = [], |
|
238 | 238 | import_some = [[]], |
|
239 | 239 | execute = [], |
|
240 | 240 | execfile = [], |
|
241 | 241 | alias = [], |
|
242 | 242 | readline_parse_and_bind = [], |
|
243 | 243 | readline_remove_delims = '', |
|
244 | 244 | ) |
|
245 | 245 | |
|
246 | 246 | # Build the type conversion dictionary from the above tables: |
|
247 | 247 | typeconv = rcfile_opts.copy() |
|
248 | 248 | typeconv.update(optstr2types(cmdline_opts)) |
|
249 | 249 | |
|
250 | 250 | # FIXME: the None key appears in both, put that back together by hand. Ugly! |
|
251 | 251 | typeconv[None] += ' ' + rcfile_opts[None] |
|
252 | 252 | |
|
253 | 253 | # Remove quotes at ends of all strings (used to protect spaces) |
|
254 | 254 | typeconv[unquote_ends] = typeconv[None] |
|
255 | 255 | del typeconv[None] |
|
256 | 256 | |
|
257 | 257 | # Build the list we'll use to make all config decisions with defaults: |
|
258 | 258 | opts_all = opts_def.copy() |
|
259 | 259 | opts_all.update(rc_def) |
|
260 | 260 | |
|
261 | 261 | # Build conflict resolver for recursive loading of config files: |
|
262 | 262 | # - preserve means the outermost file maintains the value, it is not |
|
263 | 263 | # overwritten if an included file has the same key. |
|
264 | 264 | # - add_flip applies + to the two values, so it better make sense to add |
|
265 | 265 | # those types of keys. But it flips them first so that things loaded |
|
266 | 266 | # deeper in the inclusion chain have lower precedence. |
|
267 | 267 | conflict = {'preserve': ' '.join([ typeconv[int], |
|
268 | 268 | typeconv[unquote_ends] ]), |
|
269 | 269 | 'add_flip': ' '.join([ typeconv[qwflat], |
|
270 | 270 | typeconv[qw_lol], |
|
271 | 271 | typeconv[list_strings] ]) |
|
272 | 272 | } |
|
273 | 273 | |
|
274 | 274 | # Now actually process the command line |
|
275 | 275 | getopt = DPyGetOpt.DPyGetOpt() |
|
276 | 276 | getopt.setIgnoreCase(0) |
|
277 | 277 | |
|
278 | 278 | getopt.parseConfiguration(opts_names) |
|
279 | 279 | |
|
280 | 280 | try: |
|
281 | 281 | getopt.processArguments(argv) |
|
282 | 282 | except: |
|
283 | 283 | print cmd_line_usage |
|
284 | 284 | warn('\nError in Arguments: ' + `sys.exc_value`) |
|
285 | 285 | sys.exit(1) |
|
286 | 286 | |
|
287 | 287 | # convert the options dict to a struct for much lighter syntax later |
|
288 | 288 | opts = Struct(getopt.optionValues) |
|
289 | 289 | args = getopt.freeValues |
|
290 | 290 | |
|
291 | 291 | # this is the struct (which has default values at this point) with which |
|
292 | 292 | # we make all decisions: |
|
293 | 293 | opts_all.update(opts) |
|
294 | 294 | |
|
295 | 295 | # Options that force an immediate exit |
|
296 | 296 | if opts_all.help: |
|
297 | 297 | page(cmd_line_usage) |
|
298 | 298 | sys.exit() |
|
299 | 299 | |
|
300 | 300 | if opts_all.Version: |
|
301 | 301 | print __version__ |
|
302 | 302 | sys.exit() |
|
303 | 303 | |
|
304 | 304 | if opts_all.magic_docstrings: |
|
305 | 305 | IP.magic_magic('-latex') |
|
306 | 306 | sys.exit() |
|
307 | 307 | |
|
308 | 308 | # Create user config directory if it doesn't exist. This must be done |
|
309 | 309 | # *after* getting the cmd line options. |
|
310 | 310 | if not os.path.isdir(opts_all.ipythondir): |
|
311 | 311 | IP.user_setup(opts_all.ipythondir,rc_suffix,'install') |
|
312 | 312 | |
|
313 | 313 | # upgrade user config files while preserving a copy of the originals |
|
314 | 314 | if opts_all.upgrade: |
|
315 | 315 | IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade') |
|
316 | 316 | |
|
317 | 317 | # check mutually exclusive options in the *original* command line |
|
318 | 318 | mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'), |
|
319 | 319 | qw('classic profile'),qw('classic rcfile')]) |
|
320 | 320 | |
|
321 | 321 | # default logfilename used when -log is called. |
|
322 | 322 | IP.LOGDEF = 'ipython.log' |
|
323 | 323 | |
|
324 | 324 | #--------------------------------------------------------------------------- |
|
325 | 325 | # Log replay |
|
326 | 326 | |
|
327 | 327 | # if -logplay, we need to 'become' the other session. That basically means |
|
328 | 328 | # replacing the current command line environment with that of the old |
|
329 | 329 | # session and moving on. |
|
330 | 330 | |
|
331 | 331 | # this is needed so that later we know we're in session reload mode, as |
|
332 | 332 | # opts_all will get overwritten: |
|
333 | 333 | load_logplay = 0 |
|
334 | 334 | |
|
335 | 335 | if opts_all.logplay: |
|
336 | 336 | load_logplay = opts_all.logplay |
|
337 | 337 | opts_debug_save = opts_all.debug |
|
338 | 338 | try: |
|
339 | 339 | logplay = open(opts_all.logplay) |
|
340 | 340 | except IOError: |
|
341 | 341 | if opts_all.debug: IP.InteractiveTB() |
|
342 | 342 | warn('Could not open logplay file '+`opts_all.logplay`) |
|
343 | 343 | # restore state as if nothing had happened and move on, but make |
|
344 | 344 | # sure that later we don't try to actually load the session file |
|
345 | 345 | logplay = None |
|
346 | 346 | load_logplay = 0 |
|
347 | 347 | del opts_all.logplay |
|
348 | 348 | else: |
|
349 | 349 | try: |
|
350 | 350 | logplay.readline() |
|
351 | 351 | logplay.readline(); |
|
352 | 352 | # this reloads that session's command line |
|
353 | 353 | cmd = logplay.readline()[6:] |
|
354 | 354 | exec cmd |
|
355 | 355 | # restore the true debug flag given so that the process of |
|
356 | 356 | # session loading itself can be monitored. |
|
357 | 357 | opts.debug = opts_debug_save |
|
358 | 358 | # save the logplay flag so later we don't overwrite the log |
|
359 | 359 | opts.logplay = load_logplay |
|
360 | 360 | # now we must update our own structure with defaults |
|
361 | 361 | opts_all.update(opts) |
|
362 | 362 | # now load args |
|
363 | 363 | cmd = logplay.readline()[6:] |
|
364 | 364 | exec cmd |
|
365 | 365 | logplay.close() |
|
366 | 366 | except: |
|
367 | 367 | logplay.close() |
|
368 | 368 | if opts_all.debug: IP.InteractiveTB() |
|
369 | 369 | warn("Logplay file lacking full configuration information.\n" |
|
370 | 370 | "I'll try to read it, but some things may not work.") |
|
371 | 371 | |
|
372 | 372 | #------------------------------------------------------------------------- |
|
373 | 373 | # set up output traps: catch all output from files, being run, modules |
|
374 | 374 | # loaded, etc. Then give it to the user in a clean form at the end. |
|
375 | 375 | |
|
376 | 376 | msg_out = 'Output messages. ' |
|
377 | 377 | msg_err = 'Error messages. ' |
|
378 | 378 | msg_sep = '\n' |
|
379 | 379 | msg = Struct(config = OutputTrap('Configuration Loader',msg_out, |
|
380 | 380 | msg_err,msg_sep,debug, |
|
381 | 381 | quiet_out=1), |
|
382 | 382 | user_exec = OutputTrap('User File Execution',msg_out, |
|
383 | 383 | msg_err,msg_sep,debug), |
|
384 | 384 | logplay = OutputTrap('Log Loader',msg_out, |
|
385 | 385 | msg_err,msg_sep,debug), |
|
386 | 386 | summary = '' |
|
387 | 387 | ) |
|
388 | 388 | |
|
389 | 389 | #------------------------------------------------------------------------- |
|
390 | 390 | # Process user ipythonrc-type configuration files |
|
391 | 391 | |
|
392 | 392 | # turn on output trapping and log to msg.config |
|
393 | 393 | # remember that with debug on, trapping is actually disabled |
|
394 | 394 | msg.config.trap_all() |
|
395 | 395 | |
|
396 | 396 | # look for rcfile in current or default directory |
|
397 | 397 | try: |
|
398 | 398 | opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir) |
|
399 | 399 | except IOError: |
|
400 | 400 | if opts_all.debug: IP.InteractiveTB() |
|
401 | 401 | warn('Configuration file %s not found. Ignoring request.' |
|
402 | 402 | % (opts_all.rcfile) ) |
|
403 | 403 | |
|
404 | 404 | # 'profiles' are a shorthand notation for config filenames |
|
405 | 405 | if opts_all.profile: |
|
406 | 406 | try: |
|
407 | 407 | opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile |
|
408 | 408 | + rc_suffix, |
|
409 | 409 | opts_all.ipythondir) |
|
410 | 410 | except IOError: |
|
411 | 411 | if opts_all.debug: IP.InteractiveTB() |
|
412 | 412 | opts.profile = '' # remove profile from options if invalid |
|
413 | 413 | warn('Profile configuration file %s not found. Ignoring request.' |
|
414 | 414 | % (opts_all.profile) ) |
|
415 | 415 | |
|
416 | 416 | # load the config file |
|
417 | 417 | rcfiledata = None |
|
418 | 418 | if opts_all.quick: |
|
419 | 419 | print 'Launching IPython in quick mode. No config file read.' |
|
420 | 420 | elif opts_all.classic: |
|
421 | 421 | print 'Launching IPython in classic mode. No config file read.' |
|
422 | 422 | elif opts_all.rcfile: |
|
423 | 423 | try: |
|
424 | 424 | cfg_loader = ConfigLoader(conflict) |
|
425 | 425 | rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv, |
|
426 | 426 | 'include',opts_all.ipythondir, |
|
427 | 427 | purge = 1, |
|
428 | 428 | unique = conflict['preserve']) |
|
429 | 429 | except: |
|
430 | 430 | IP.InteractiveTB() |
|
431 | 431 | warn('Problems loading configuration file '+ |
|
432 | 432 | `opts_all.rcfile`+ |
|
433 | 433 | '\nStarting with default -bare bones- configuration.') |
|
434 | 434 | else: |
|
435 | 435 | warn('No valid configuration file found in either currrent directory\n'+ |
|
436 | 436 | 'or in the IPython config. directory: '+`opts_all.ipythondir`+ |
|
437 | 437 | '\nProceeding with internal defaults.') |
|
438 | 438 | |
|
439 | 439 | #------------------------------------------------------------------------ |
|
440 | 440 | # Set exception handlers in mode requested by user. |
|
441 | 441 | otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode |
|
442 | 442 | IP.magic_xmode(opts_all.xmode) |
|
443 | 443 | otrap.release_out() |
|
444 | 444 | |
|
445 | 445 | #------------------------------------------------------------------------ |
|
446 | 446 | # Execute user config |
|
447 | 447 | |
|
448 |
# |
|
|
449 |
# defaults < rcfile < command line. |
|
|
450 | # avoid a zillion attribute accesses. Right before returning, this will | |
|
451 | # be set as IP.rc. | |
|
452 | IP_rc = rc_def.copy() | |
|
448 | # Create a valid config structure with the right precedence order: | |
|
449 | # defaults < rcfile < command line. This needs to be in the instance, so | |
|
450 | # that method calls below that rely on it find it. | |
|
451 | IP.rc = rc_def.copy() | |
|
452 | ||
|
453 | # Work with a local alias inside this routine to avoid unnecessary | |
|
454 | # attribute lookups. | |
|
455 | IP_rc = IP.rc | |
|
456 | ||
|
453 | 457 | IP_rc.update(opts_def) |
|
454 | 458 | if rcfiledata: |
|
455 | 459 | # now we can update |
|
456 | 460 | IP_rc.update(rcfiledata) |
|
457 | 461 | IP_rc.update(opts) |
|
458 | 462 | IP_rc.update(rc_override) |
|
459 | 463 | |
|
460 | 464 | # Store the original cmd line for reference: |
|
461 | 465 | IP_rc.opts = opts |
|
462 | 466 | IP_rc.args = args |
|
463 | 467 | |
|
464 | 468 | # create a *runtime* Struct like rc for holding parameters which may be |
|
465 | 469 | # created and/or modified by runtime user extensions. |
|
466 | 470 | IP.runtime_rc = Struct() |
|
467 | 471 | |
|
468 | 472 | # from this point on, all config should be handled through IP_rc, |
|
469 | 473 | # opts* shouldn't be used anymore. |
|
470 | 474 | |
|
471 | 475 | # add personal .ipython dir to sys.path so that users can put things in |
|
472 | 476 | # there for customization |
|
473 | 477 | sys.path.append(IP_rc.ipythondir) |
|
474 | 478 | sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran |
|
475 | 479 | |
|
476 | 480 | # update IP_rc with some special things that need manual |
|
477 | 481 | # tweaks. Basically options which affect other options. I guess this |
|
478 | 482 | # should just be written so that options are fully orthogonal and we |
|
479 | 483 | # wouldn't worry about this stuff! |
|
480 | 484 | |
|
481 | 485 | if IP_rc.classic: |
|
482 | 486 | IP_rc.quick = 1 |
|
483 | 487 | IP_rc.cache_size = 0 |
|
484 | 488 | IP_rc.pprint = 0 |
|
485 | 489 | IP_rc.prompt_in1 = '>>> ' |
|
486 | 490 | IP_rc.prompt_in2 = '... ' |
|
487 | 491 | IP_rc.prompt_out = '' |
|
488 | 492 | IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0' |
|
489 | 493 | IP_rc.colors = 'NoColor' |
|
490 | 494 | IP_rc.xmode = 'Plain' |
|
491 | 495 | |
|
492 | 496 | # configure readline |
|
493 | 497 | # Define the history file for saving commands in between sessions |
|
494 | 498 | if IP_rc.profile: |
|
495 | 499 | histfname = 'history-%s' % IP_rc.profile |
|
496 | 500 | else: |
|
497 | 501 | histfname = 'history' |
|
498 | 502 | IP.histfile = os.path.join(opts_all.ipythondir,histfname) |
|
499 | 503 | |
|
500 | 504 | # update exception handlers with rc file status |
|
501 | 505 | otrap.trap_out() # I don't want these messages ever. |
|
502 | 506 | IP.magic_xmode(IP_rc.xmode) |
|
503 | 507 | otrap.release_out() |
|
504 | 508 | |
|
505 | 509 | # activate logging if requested and not reloading a log |
|
506 | 510 | if IP_rc.logplay: |
|
507 | 511 | IP.magic_logstart(IP_rc.logplay + ' append') |
|
508 | 512 | elif IP_rc.logfile: |
|
509 | 513 | IP.magic_logstart(IP_rc.logfile) |
|
510 | 514 | elif IP_rc.log: |
|
511 | 515 | IP.magic_logstart() |
|
512 | 516 | |
|
513 | 517 | # find user editor so that it we don't have to look it up constantly |
|
514 | 518 | if IP_rc.editor.strip()=='0': |
|
515 | 519 | try: |
|
516 | 520 | ed = os.environ['EDITOR'] |
|
517 | 521 | except KeyError: |
|
518 | 522 | if os.name == 'posix': |
|
519 | 523 | ed = 'vi' # the only one guaranteed to be there! |
|
520 | 524 | else: |
|
521 | 525 | ed = 'notepad' # same in Windows! |
|
522 | 526 | IP_rc.editor = ed |
|
523 | 527 | |
|
524 | 528 | # Keep track of whether this is an embedded instance or not (useful for |
|
525 | 529 | # post-mortems). |
|
526 | 530 | IP_rc.embedded = IP.embedded |
|
527 | 531 | |
|
528 | 532 | # Recursive reload |
|
529 | 533 | try: |
|
530 | 534 | from IPython import deep_reload |
|
531 | 535 | if IP_rc.deep_reload: |
|
532 | 536 | __builtin__.reload = deep_reload.reload |
|
533 | 537 | else: |
|
534 | 538 | __builtin__.dreload = deep_reload.reload |
|
535 | 539 | del deep_reload |
|
536 | 540 | except ImportError: |
|
537 | 541 | pass |
|
538 | 542 | |
|
539 | 543 | # Save the current state of our namespace so that the interactive shell |
|
540 | 544 | # can later know which variables have been created by us from config files |
|
541 | 545 | # and loading. This way, loading a file (in any way) is treated just like |
|
542 | 546 | # defining things on the command line, and %who works as expected. |
|
543 | 547 | |
|
544 | 548 | # DON'T do anything that affects the namespace beyond this point! |
|
545 | 549 | IP.internal_ns.update(__main__.__dict__) |
|
546 | 550 | |
|
547 | 551 | #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who |
|
548 | 552 | |
|
549 | 553 | # Now run through the different sections of the users's config |
|
550 | 554 | if IP_rc.debug: |
|
551 | 555 | print 'Trying to execute the following configuration structure:' |
|
552 | 556 | print '(Things listed first are deeper in the inclusion tree and get' |
|
553 | 557 | print 'loaded first).\n' |
|
554 | 558 | pprint(IP_rc.__dict__) |
|
555 | 559 | |
|
556 | 560 | for mod in IP_rc.import_mod: |
|
557 | 561 | try: |
|
558 | 562 | exec 'import '+mod in IP.user_ns |
|
559 | 563 | except : |
|
560 | 564 | IP.InteractiveTB() |
|
561 | 565 | import_fail_info(mod) |
|
562 | 566 | |
|
563 | 567 | for mod_fn in IP_rc.import_some: |
|
564 | 568 | if mod_fn == []: break |
|
565 | 569 | mod,fn = mod_fn[0],','.join(mod_fn[1:]) |
|
566 | 570 | try: |
|
567 | 571 | exec 'from '+mod+' import '+fn in IP.user_ns |
|
568 | 572 | except : |
|
569 | 573 | IP.InteractiveTB() |
|
570 | 574 | import_fail_info(mod,fn) |
|
571 | 575 | |
|
572 | 576 | for mod in IP_rc.import_all: |
|
573 | 577 | try: |
|
574 | 578 | exec 'from '+mod+' import *' in IP.user_ns |
|
575 | 579 | except : |
|
576 | 580 | IP.InteractiveTB() |
|
577 | 581 | import_fail_info(mod) |
|
578 | 582 | |
|
579 | 583 | for code in IP_rc.execute: |
|
580 | 584 | try: |
|
581 | 585 | exec code in IP.user_ns |
|
582 | 586 | except: |
|
583 | 587 | IP.InteractiveTB() |
|
584 | 588 | warn('Failure executing code: ' + `code`) |
|
585 | 589 | |
|
586 | 590 | # Execute the files the user wants in ipythonrc |
|
587 | 591 | for file in IP_rc.execfile: |
|
588 | 592 | try: |
|
589 | 593 | file = filefind(file,sys.path+[IPython_dir]) |
|
590 | 594 | except IOError: |
|
591 | 595 | warn(itpl('File $file not found. Skipping it.')) |
|
592 | 596 | else: |
|
593 | 597 | IP.safe_execfile(os.path.expanduser(file),IP.user_ns) |
|
594 | 598 | |
|
595 | 599 | # release stdout and stderr and save config log into a global summary |
|
596 | 600 | msg.config.release_all() |
|
597 | 601 | if IP_rc.messages: |
|
598 | 602 | msg.summary += msg.config.summary_all() |
|
599 | 603 | |
|
600 | 604 | #------------------------------------------------------------------------ |
|
601 | 605 | # Setup interactive session |
|
602 | 606 | |
|
603 | 607 | # Now we should be fully configured. We can then execute files or load |
|
604 | 608 | # things only needed for interactive use. Then we'll open the shell. |
|
605 | 609 | |
|
606 | 610 | # Take a snapshot of the user namespace before opening the shell. That way |
|
607 | 611 | # we'll be able to identify which things were interactively defined and |
|
608 | 612 | # which were defined through config files. |
|
609 | 613 | IP.user_config_ns = IP.user_ns.copy() |
|
610 | 614 | |
|
611 | 615 | # Force reading a file as if it were a session log. Slower but safer. |
|
612 | 616 | if load_logplay: |
|
613 | 617 | print 'Replaying log...' |
|
614 | 618 | try: |
|
615 | 619 | if IP_rc.debug: |
|
616 | 620 | logplay_quiet = 0 |
|
617 | 621 | else: |
|
618 | 622 | logplay_quiet = 1 |
|
619 | 623 | |
|
620 | 624 | msg.logplay.trap_all() |
|
621 | 625 | IP.safe_execfile(load_logplay,IP.user_ns, |
|
622 | 626 | islog = 1, quiet = logplay_quiet) |
|
623 | 627 | msg.logplay.release_all() |
|
624 | 628 | if IP_rc.messages: |
|
625 | 629 | msg.summary += msg.logplay.summary_all() |
|
626 | 630 | except: |
|
627 | 631 | warn('Problems replaying logfile %s.' % load_logplay) |
|
628 | 632 | IP.InteractiveTB() |
|
629 | 633 | |
|
630 | 634 | # Load remaining files in command line |
|
631 | 635 | msg.user_exec.trap_all() |
|
632 | 636 | |
|
633 | 637 | # Do NOT execute files named in the command line as scripts to be loaded |
|
634 | 638 | # by embedded instances. Doing so has the potential for an infinite |
|
635 | 639 | # recursion if there are exceptions thrown in the process. |
|
636 | 640 | |
|
637 | 641 | # XXX FIXME: the execution of user files should be moved out to after |
|
638 | 642 | # ipython is fully initialized, just as if they were run via %run at the |
|
639 | 643 | # ipython prompt. This would also give them the benefit of ipython's |
|
640 | 644 | # nice tracebacks. |
|
641 | 645 | |
|
642 | 646 | if not embedded and IP_rc.args: |
|
643 | 647 | name_save = IP.user_ns['__name__'] |
|
644 | 648 | IP.user_ns['__name__'] = '__main__' |
|
645 | 649 | try: |
|
646 | 650 | # Set our own excepthook in case the user code tries to call it |
|
647 | 651 | # directly. This prevents triggering the IPython crash handler. |
|
648 | 652 | old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook |
|
649 | 653 | for run in args: |
|
650 | 654 | IP.safe_execfile(run,IP.user_ns) |
|
651 | 655 | finally: |
|
652 | 656 | # Reset our crash handler in place |
|
653 | 657 | sys.excepthook = old_excepthook |
|
654 | 658 | |
|
655 | 659 | IP.user_ns['__name__'] = name_save |
|
656 | 660 | |
|
657 | 661 | msg.user_exec.release_all() |
|
658 | 662 | if IP_rc.messages: |
|
659 | 663 | msg.summary += msg.user_exec.summary_all() |
|
660 | 664 | |
|
661 | 665 | # since we can't specify a null string on the cmd line, 0 is the equivalent: |
|
662 | 666 | if IP_rc.nosep: |
|
663 | 667 | IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0' |
|
664 | 668 | if IP_rc.separate_in == '0': IP_rc.separate_in = '' |
|
665 | 669 | if IP_rc.separate_out == '0': IP_rc.separate_out = '' |
|
666 | 670 | if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = '' |
|
667 | 671 | IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n') |
|
668 | 672 | IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n') |
|
669 | 673 | IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n') |
|
670 | 674 | |
|
671 | 675 | # Determine how many lines at the bottom of the screen are needed for |
|
672 | 676 | # showing prompts, so we can know wheter long strings are to be printed or |
|
673 | 677 | # paged: |
|
674 | 678 | num_lines_bot = IP_rc.separate_in.count('\n')+1 |
|
675 | 679 | IP_rc.screen_length = IP_rc.screen_length - num_lines_bot |
|
676 | 680 | # Initialize cache, set in/out prompts and printing system |
|
677 | 681 | IP.outputcache = CachedOutput(IP_rc.cache_size, |
|
678 | 682 | IP_rc.pprint, |
|
679 | 683 | input_sep = IP_rc.separate_in, |
|
680 | 684 | output_sep = IP_rc.separate_out, |
|
681 | 685 | output_sep2 = IP_rc.separate_out2, |
|
682 | 686 | ps1 = IP_rc.prompt_in1, |
|
683 | 687 | ps2 = IP_rc.prompt_in2, |
|
684 | 688 | ps_out = IP_rc.prompt_out, |
|
685 | 689 | user_ns = IP.user_ns, |
|
686 | 690 | input_hist = IP.input_hist, |
|
687 | 691 | pad_left = IP_rc.prompts_pad_left) |
|
688 | 692 | |
|
689 | 693 | # user may have over-ridden the default print hook: |
|
690 | 694 | try: |
|
691 | 695 | IP.outputcache.__class__.display = IP.hooks.display |
|
692 | 696 | except AttributeError: |
|
693 | 697 | pass |
|
694 | 698 | |
|
695 | 699 | # Set calling of pdb on exceptions |
|
696 | 700 | IP.InteractiveTB.call_pdb = IP_rc.pdb |
|
697 | 701 | |
|
698 | 702 | # I don't like assigning globally to sys, because it means when embedding |
|
699 | 703 | # instances, each embedded instance overrides the previous choice. But |
|
700 | 704 | # sys.displayhook seems to be called internally by exec, so I don't see a |
|
701 | 705 | # way around it. |
|
702 | 706 | sys.displayhook = IP.outputcache |
|
703 | 707 | |
|
704 | 708 | # we need to know globally if we're caching i/o or not |
|
705 | 709 | IP.do_full_cache = IP.outputcache.do_full_cache |
|
706 | 710 | |
|
707 | 711 | # configure startup banner |
|
708 | 712 | if IP_rc.c: # regular python doesn't print the banner with -c |
|
709 | 713 | IP_rc.banner = 0 |
|
710 | 714 | if IP_rc.banner: |
|
711 | 715 | BANN_P = IP.BANNER_PARTS |
|
712 | 716 | else: |
|
713 | 717 | BANN_P = [] |
|
714 | 718 | |
|
715 | 719 | if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile) |
|
716 | 720 | |
|
717 | 721 | # add message log (possibly empty) |
|
718 | 722 | if msg.summary: BANN_P.append(msg.summary) |
|
719 | 723 | # Final banner is a string |
|
720 | 724 | IP.BANNER = '\n'.join(BANN_P) |
|
721 | 725 | |
|
722 | # Assign the IP_rc object as an attribute of IP | |
|
723 | IP.rc = IP_rc | |
|
724 | ||
|
725 | 726 | # Finalize the IPython instance. This assumes the rc structure is fully |
|
726 | 727 | # in place. |
|
727 | 728 | IP.post_config_initialization() |
|
728 | 729 | |
|
729 | 730 | return IP |
|
730 | 731 | #************************ end of file <ipmaker.py> ************************** |
General Comments 0
You need to be logged in to leave comments.
Login now