ipapp.py
655 lines
| 26.5 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2202 | #!/usr/bin/env python | ||
# encoding: utf-8 | ||||
""" | ||||
Brian Granger
|
r2301 | The :class:`~IPython.core.application.Application` object for the command | ||
line :command:`ipython` program. | ||||
Brian Granger
|
r2202 | |||
Fernando Perez
|
r2427 | Authors | ||
------- | ||||
Brian Granger
|
r2202 | |||
* Brian Granger | ||||
* Fernando Perez | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r2427 | # Copyright (C) 2008-2010 The IPython Development Team | ||
Brian Granger
|
r2202 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r2427 | from __future__ import absolute_import | ||
Brian Granger
|
r2202 | |||
Brian Granger
|
r2252 | import logging | ||
Brian Granger
|
r2203 | import os | ||
import sys | ||||
Fernando Perez
|
r2403 | from IPython.core import crashhandler | ||
Fernando Perez
|
r2412 | from IPython.core.application import Application | ||
Brian Granger
|
r2202 | from IPython.core.iplib import InteractiveShell | ||
Brian Granger
|
r2245 | from IPython.config.loader import ( | ||
Config, | ||||
Fernando Perez
|
r2428 | PyFileConfigLoader, | ||
# NoConfigDefault, | ||||
Brian Granger
|
r2245 | ) | ||
Brian Granger
|
r2264 | from IPython.lib import inputhook | ||
Brian Granger
|
r2252 | from IPython.utils.genutils import filefind, get_ipython_dir | ||
Fernando Perez
|
r2427 | from . import usage | ||
Brian Granger
|
r2203 | |||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r2427 | # Globals, utilities and helpers | ||
Brian Granger
|
r2203 | #----------------------------------------------------------------------------- | ||
Fernando Perez
|
r2427 | default_config_file_name = u'ipython_config.py' | ||
Brian Granger
|
r2203 | |||
cl_args = ( | ||||
Brian Granger
|
r2322 | (('--autocall',), dict( | ||
Fernando Perez
|
r2428 | type=int, dest='InteractiveShell.autocall', | ||
Fernando Perez
|
r2427 | help= | ||
"""Make IPython automatically call any callable object even if you | ||||
didn't type explicit parentheses. For example, 'str 43' becomes | ||||
'str(43)' automatically. The value can be '0' to disable the feature, | ||||
'1' for 'smart' autocall, where it is not applied if there are no more | ||||
arguments on the line, and '2' for 'full' autocall, where all callable | ||||
objects are automatically called (even if no arguments are present). | ||||
The default is '1'.""", | ||||
Brian Granger
|
r2245 | metavar='InteractiveShell.autocall') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--autoindent',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.autoindent', | ||
Brian Granger
|
r2203 | help='Turn on autoindenting.') | ||
), | ||||
Brian Granger
|
r2322 | (('--no-autoindent',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.autoindent', | ||
Brian Granger
|
r2203 | help='Turn off autoindenting.') | ||
), | ||||
Brian Granger
|
r2322 | (('--automagic',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.automagic', | ||
help='Turn on the auto calling of magic commands.' | ||||
'Type %%magic at the IPython prompt for more information.') | ||||
), | ||||
Brian Granger
|
r2322 | (('--no-automagic',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.automagic', | ||
Brian Granger
|
r2203 | help='Turn off the auto calling of magic commands.') | ||
), | ||||
Brian Granger
|
r2322 | (('--autoedit-syntax',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.autoedit_syntax', | ||
Brian Granger
|
r2203 | help='Turn on auto editing of files with syntax errors.') | ||
), | ||||
Brian Granger
|
r2322 | (('--no-autoedit-syntax',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.autoedit_syntax', | ||
Brian Granger
|
r2203 | help='Turn off auto editing of files with syntax errors.') | ||
), | ||||
Brian Granger
|
r2322 | (('--banner',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='Global.display_banner', | ||
Brian Granger
|
r2203 | help='Display a banner upon starting IPython.') | ||
), | ||||
Brian Granger
|
r2322 | (('--no-banner',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='Global.display_banner', | ||
Brian Granger
|
r2203 | help="Don't display a banner upon starting IPython.") | ||
), | ||||
Brian Granger
|
r2322 | (('--cache-size',), dict( | ||
Fernando Perez
|
r2428 | type=int, dest='InteractiveShell.cache_size', | ||
Fernando Perez
|
r2427 | help= | ||
"""Set the size of the output cache. The default is 1000, you can | ||||
change it permanently in your config file. Setting it to 0 completely | ||||
disables the caching system, and the minimum value accepted is 20 (if | ||||
you provide a value less than 20, it is reset to 0 and a warning is | ||||
issued). This limit is defined because otherwise you'll spend more | ||||
time re-flushing a too small cache than working. | ||||
""", | ||||
Brian Granger
|
r2245 | metavar='InteractiveShell.cache_size') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--classic',), dict( | ||
Fernando Perez
|
r2428 | action='store_true', dest='Global.classic', | ||
Brian Granger
|
r2203 | help="Gives IPython a similar feel to the classic Python prompt.") | ||
), | ||||
Brian Granger
|
r2322 | (('--colors',), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.colors', | ||
Brian Granger
|
r2245 | help="Set the color scheme (NoColor, Linux, and LightBG).", | ||
metavar='InteractiveShell.colors') | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--color-info',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.color_info', | ||
help= | ||||
"""IPython can display information about objects via a set of func- | ||||
tions, and optionally can use colors for this, syntax highlighting | ||||
source code and various other elements. However, because this | ||||
information is passed through a pager (like 'less') and many pagers get | ||||
confused with color codes, this option is off by default. You can test | ||||
it and turn it on permanently in your ipython_config.py file if it | ||||
works for you. Test it and turn it on permanently if it works with | ||||
your system. The magic function %%color_info allows you to toggle this | ||||
inter- actively for testing.""" | ||||
) | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--no-color-info',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.color_info', | ||
Brian Granger
|
r2203 | help="Disable using colors for info related things.") | ||
), | ||||
Brian Granger
|
r2322 | (('--confirm-exit',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.confirm_exit', | ||
help= | ||||
"""Set to confirm when you try to exit IPython with an EOF (Control-D | ||||
in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or | ||||
'%%Exit', you can force a direct exit without any confirmation. | ||||
""" | ||||
) | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--no-confirm-exit',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.confirm_exit', | ||
help="Don't prompt the user when exiting.") | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--deep-reload',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.deep_reload', | ||
help= | ||||
"""Enable deep (recursive) reloading by default. IPython can use the | ||||
deep_reload module which reloads changes in modules recursively (it | ||||
replaces the reload() function, so you don't need to change anything to | ||||
use it). deep_reload() forces a full reload of modules whose code may | ||||
have changed, which the default reload() function does not. When | ||||
deep_reload is off, IPython will use the normal reload(), but | ||||
deep_reload will still be available as dreload(). This fea- ture is off | ||||
by default [which means that you have both normal reload() and | ||||
dreload()].""") | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--no-deep-reload',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.deep_reload', | ||
Brian Granger
|
r2203 | help="Disable deep (recursive) reloading by default.") | ||
), | ||||
Brian Granger
|
r2322 | (('--editor',), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.editor', | ||
Brian Granger
|
r2245 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad).", | ||
metavar='InteractiveShell.editor') | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--log','-l'), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.logstart', | ||
help="Start logging to the default log file (./ipython_log.py).") | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--logfile','-lf'), dict( | ||
Fernando Perez
|
r2428 | type=unicode, dest='InteractiveShell.logfile', | ||
Fernando Perez
|
r2427 | help="Start logging to logfile with this name.", | ||
Brian Granger
|
r2245 | metavar='InteractiveShell.logfile') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--log-append','-la'), dict( | ||
Fernando Perez
|
r2427 | type=unicode, dest='InteractiveShell.logappend', | ||
help="Start logging to the given file in append mode.", | ||||
Brian Granger
|
r2265 | metavar='InteractiveShell.logfile') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--pdb',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.pdb', | ||
Brian Granger
|
r2203 | help="Enable auto calling the pdb debugger after every exception.") | ||
), | ||||
Brian Granger
|
r2328 | (('--no-pdb',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.pdb', | ||
Brian Granger
|
r2203 | help="Disable auto calling the pdb debugger after every exception.") | ||
), | ||||
Brian Granger
|
r2322 | (('--pprint',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.pprint', | ||
Brian Granger
|
r2203 | help="Enable auto pretty printing of results.") | ||
), | ||||
Brian Granger
|
r2322 | (('--no-pprint',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.pprint', | ||
Brian Granger
|
r2203 | help="Disable auto auto pretty printing of results.") | ||
), | ||||
Brian Granger
|
r2322 | (('--prompt-in1','-pi1'), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.prompt_in1', | ||
Fernando Perez
|
r2427 | help= | ||
"""Set the main input prompt ('In [\#]: '). Note that if you are using | ||||
numbered prompts, the number is represented with a '\#' in the string. | ||||
Don't forget to quote strings with spaces embedded in them. Most | ||||
bash-like escapes can be used to customize IPython's prompts, as well | ||||
as a few additional ones which are IPython-spe- cific. All valid | ||||
prompt escapes are described in detail in the Customization section of | ||||
the IPython manual.""", | ||||
Brian Granger
|
r2245 | metavar='InteractiveShell.prompt_in1') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--prompt-in2','-pi2'), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.prompt_in2', | ||
Fernando Perez
|
r2427 | help= | ||
"""Set the secondary input prompt (' .\D.: '). Similar to the previous | ||||
option, but used for the continuation prompts. The special sequence | ||||
'\D' is similar to '\#', but with all digits replaced by dots (so you | ||||
can have your continuation prompt aligned with your input prompt). | ||||
Default: ' .\D.: ' (note three spaces at the start for alignment with | ||||
'In [\#]')""", | ||||
Brian Granger
|
r2245 | metavar='InteractiveShell.prompt_in2') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--prompt-out','-po'), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.prompt_out', | ||
Brian Granger
|
r2245 | help="Set the output prompt ('Out[\#]:')", | ||
metavar='InteractiveShell.prompt_out') | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--quick',), dict( | ||
Fernando Perez
|
r2428 | action='store_true', dest='Global.quick', | ||
Brian Granger
|
r2203 | help="Enable quick startup with no config files.") | ||
), | ||||
Brian Granger
|
r2322 | (('--readline',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.readline_use', | ||
Brian Granger
|
r2203 | help="Enable readline for command line usage.") | ||
), | ||||
Brian Granger
|
r2322 | (('--no-readline',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.readline_use', | ||
Brian Granger
|
r2203 | help="Disable readline for command line usage.") | ||
), | ||||
Brian Granger
|
r2322 | (('--screen-length','-sl'), dict( | ||
Fernando Perez
|
r2427 | type=int, dest='InteractiveShell.screen_length', | ||
help= | ||||
"""Number of lines of your screen, used to control printing of very | ||||
long strings. Strings longer than this number of lines will be sent | ||||
through a pager instead of directly printed. The default value for | ||||
this is 0, which means IPython will auto-detect your screen size every | ||||
time it needs to print certain potentially long strings (this doesn't | ||||
change the behavior of the 'print' keyword, it's only triggered | ||||
internally). If for some reason this isn't working well (it needs | ||||
curses support), specify it yourself. Otherwise don't change the | ||||
default.""", | ||||
Brian Granger
|
r2245 | metavar='InteractiveShell.screen_length') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--separate-in','-si'), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.separate_in', | ||
Fernando Perez
|
r2427 | help="Separator before input prompts. Default '\\n'.", | ||
Brian Granger
|
r2245 | metavar='InteractiveShell.separate_in') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--separate-out','-so'), dict( | ||
Fernando Perez
|
r2427 | type=str, dest='InteractiveShell.separate_out', | ||
Brian Granger
|
r2245 | help="Separator before output prompts. Default 0 (nothing).", | ||
metavar='InteractiveShell.separate_out') | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--separate-out2','-so2'), dict( | ||
Fernando Perez
|
r2427 | type=str, dest='InteractiveShell.separate_out2', | ||
Brian Granger
|
r2245 | help="Separator after output prompts. Default 0 (nonight).", | ||
metavar='InteractiveShell.separate_out2') | ||||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('-no-sep',), dict( | ||
Fernando Perez
|
r2428 | action='store_true', dest='Global.nosep', | ||
Brian Granger
|
r2203 | help="Eliminate all spacing between prompts.") | ||
), | ||||
Brian Granger
|
r2322 | (('--term-title',), dict( | ||
Fernando Perez
|
r2427 | action='store_true', dest='InteractiveShell.term_title', | ||
Brian Granger
|
r2204 | help="Enable auto setting the terminal title.") | ||
), | ||||
Brian Granger
|
r2322 | (('--no-term-title',), dict( | ||
Fernando Perez
|
r2427 | action='store_false', dest='InteractiveShell.term_title', | ||
Brian Granger
|
r2204 | help="Disable auto setting the terminal title.") | ||
), | ||||
Brian Granger
|
r2322 | (('--xmode',), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='InteractiveShell.xmode', | ||
Fernando Perez
|
r2427 | help= | ||
"""Exception reporting mode ('Plain','Context','Verbose'). Plain: | ||||
similar to python's normal traceback printing. Context: prints 5 lines | ||||
of context source code around each line in the traceback. Verbose: | ||||
similar to Context, but additionally prints the variables currently | ||||
visible where the exception happened (shortening their strings if too | ||||
long). This can potentially be very slow, if you happen to have a huge | ||||
data structure whose string representation is complex to compute. | ||||
Your computer may appear to freeze for a while with cpu usage at 100%%. | ||||
If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting | ||||
it more than once). | ||||
""", | ||||
Brian Granger
|
r2245 | metavar='InteractiveShell.xmode') | ||
Brian Granger
|
r2203 | ), | ||
Brian Granger
|
r2322 | (('--ext',), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='Global.extra_extension', | ||
Brian Granger
|
r2252 | help="The dotted module name of an IPython extension to load.", | ||
metavar='Global.extra_extension') | ||||
), | ||||
Brian Granger
|
r2253 | (('-c',), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='Global.code_to_run', | ||
Brian Granger
|
r2253 | help="Execute the given command string.", | ||
metavar='Global.code_to_run') | ||||
), | ||||
(('-i',), dict( | ||||
Fernando Perez
|
r2427 | action='store_true', dest='Global.force_interact', | ||
help= | ||||
"If running code from the command line, become interactive afterwards." | ||||
) | ||||
Brian Granger
|
r2253 | ), | ||
Fernando Perez
|
r2388 | |||
# Options to start with GUI control enabled from the beginning | ||||
(('--gui',), dict( | ||||
Fernando Perez
|
r2428 | type=str, dest='Global.gui', | ||
Fernando Perez
|
r2388 | help="Enable GUI event loop integration ('qt', 'wx', 'gtk').", | ||
metavar='gui-mode') | ||||
), | ||||
Fernando Perez
|
r2392 | (('--pylab','-pylab'), dict( | ||
Fernando Perez
|
r2428 | type=str, dest='Global.pylab', | ||
Fernando Perez
|
r2388 | nargs='?', const='auto', metavar='gui-mode', | ||
help="Pre-load matplotlib and numpy for interactive use. "+ | ||||
"If no value is given, the gui backend is matplotlib's, else use "+ | ||||
"one of: ['tk', 'qt', 'wx', 'gtk'].") | ||||
), | ||||
# Legacy GUI options. Leave them in for backwards compatibility, but the | ||||
# 'thread' names are really a misnomer now. | ||||
Fernando Perez
|
r2363 | (('--wthread','-wthread'), dict( | ||
Fernando Perez
|
r2428 | action='store_true', dest='Global.wthread', | ||
Fernando Perez
|
r2388 | help="Enable wxPython event loop integration "+ | ||
"(DEPRECATED, use --gui wx)") | ||||
Brian Granger
|
r2204 | ), | ||
Fernando Perez
|
r2363 | (('--q4thread','--qthread','-q4thread','-qthread'), dict( | ||
Fernando Perez
|
r2428 | action='store_true', dest='Global.q4thread', | ||
Fernando Perez
|
r2388 | help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+ | ||
"(DEPRECATED, use --gui qt)") | ||||
Brian Granger
|
r2264 | ), | ||
Fernando Perez
|
r2363 | (('--gthread','-gthread'), dict( | ||
Fernando Perez
|
r2428 | action='store_true', dest='Global.gthread', | ||
Fernando Perez
|
r2388 | help="Enable GTK event loop integration. "+ | ||
"(DEPRECATED, use --gui gtk)") | ||||
Brian Granger
|
r2264 | ), | ||
Brian Granger
|
r2203 | ) | ||
Fernando Perez
|
r2427 | #----------------------------------------------------------------------------- | ||
# Main classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r2245 | |||
Brian Granger
|
r2202 | class IPythonApp(Application): | ||
Brian Granger
|
r2328 | name = u'ipython' | ||
Fernando Perez
|
r2427 | #: argparse formats better the 'usage' than the 'description' field | ||
description = None | ||||
#: usage message printed by argparse. If None, auto-generate | ||||
usage = usage.cl_usage | ||||
Brian Granger
|
r2297 | config_file_name = default_config_file_name | ||
Brian Granger
|
r2202 | |||
Fernando Perez
|
r2403 | cl_arguments = Application.cl_arguments + cl_args | ||
# Private and configuration attributes | ||||
_CrashHandler = crashhandler.IPythonCrashHandler | ||||
Fernando Perez
|
r2439 | def __init__(self, argv=None, | ||
constructor_config=None, override_config=None, | ||||
**shell_params): | ||||
Fernando Perez
|
r2392 | """Create a new IPythonApp. | ||
Fernando Perez
|
r2439 | See the parent class for details on how configuration is handled. | ||
Fernando Perez
|
r2392 | Parameters | ||
---------- | ||||
argv : optional, list | ||||
If given, used as the command-line argv environment to read arguments | ||||
from. | ||||
Fernando Perez
|
r2439 | constructor_config : optional, Config | ||
If given, additional config that is merged last, after internal | ||||
defaults, command-line and file-based configs. | ||||
override_config : optional, Config | ||||
If given, config that overrides all others unconditionally (except | ||||
for internal defaults, which ensure that all parameters exist). | ||||
Fernando Perez
|
r2392 | shell_params : optional, dict | ||
All other keywords are passed to the :class:`iplib.InteractiveShell` | ||||
constructor. | ||||
""" | ||||
Fernando Perez
|
r2439 | super(IPythonApp, self).__init__(argv, constructor_config, | ||
override_config) | ||||
Fernando Perez
|
r2392 | self.shell_params = shell_params | ||
Brian Granger
|
r2252 | def create_default_config(self): | ||
super(IPythonApp, self).create_default_config() | ||||
Fernando Perez
|
r2357 | # Eliminate multiple lookups | ||
Global = self.default_config.Global | ||||
Fernando Perez
|
r2388 | |||
Fernando Perez
|
r2357 | # Set all default values | ||
Global.display_banner = True | ||||
Brian Granger
|
r2253 | |||
# If the -c flag is given or a file is given to run at the cmd line | ||||
# like "ipython foo.py", normally we exit without starting the main | ||||
# loop. The force_interact config variable allows a user to override | ||||
# this and interact. It is also set by the -i cmd line flag, just | ||||
# like Python. | ||||
Fernando Perez
|
r2357 | Global.force_interact = False | ||
Brian Granger
|
r2264 | |||
Brian Granger
|
r2253 | # By default always interact by starting the IPython mainloop. | ||
Fernando Perez
|
r2357 | Global.interact = True | ||
Brian Granger
|
r2264 | |||
# No GUI integration by default | ||||
Fernando Perez
|
r2388 | Global.gui = False | ||
Fernando Perez
|
r2363 | # Pylab off by default | ||
Global.pylab = False | ||||
Fernando Perez
|
r2388 | # Deprecated versions of gui support that used threading, we support | ||
# them just for bacwards compatibility as an alternate spelling for | ||||
# '--gui X' | ||||
Global.qthread = False | ||||
Global.q4thread = False | ||||
Global.wthread = False | ||||
Global.gthread = False | ||||
Brian Granger
|
r2203 | def load_file_config(self): | ||
Brian Granger
|
r2245 | if hasattr(self.command_line_config.Global, 'quick'): | ||
if self.command_line_config.Global.quick: | ||||
self.file_config = Config() | ||||
Brian Granger
|
r2203 | return | ||
super(IPythonApp, self).load_file_config() | ||||
def post_load_file_config(self): | ||||
Brian Granger
|
r2252 | if hasattr(self.command_line_config.Global, 'extra_extension'): | ||
if not hasattr(self.file_config.Global, 'extensions'): | ||||
self.file_config.Global.extensions = [] | ||||
self.file_config.Global.extensions.append( | ||||
self.command_line_config.Global.extra_extension) | ||||
del self.command_line_config.Global.extra_extension | ||||
Brian Granger
|
r2203 | |||
def pre_construct(self): | ||||
config = self.master_config | ||||
Brian Granger
|
r2245 | if hasattr(config.Global, 'classic'): | ||
if config.Global.classic: | ||||
config.InteractiveShell.cache_size = 0 | ||||
config.InteractiveShell.pprint = 0 | ||||
config.InteractiveShell.prompt_in1 = '>>> ' | ||||
config.InteractiveShell.prompt_in2 = '... ' | ||||
config.InteractiveShell.prompt_out = '' | ||||
config.InteractiveShell.separate_in = \ | ||||
config.InteractiveShell.separate_out = \ | ||||
config.InteractiveShell.separate_out2 = '' | ||||
config.InteractiveShell.colors = 'NoColor' | ||||
config.InteractiveShell.xmode = 'Plain' | ||||
Brian Granger
|
r2203 | |||
Brian Granger
|
r2245 | if hasattr(config.Global, 'nosep'): | ||
if config.Global.nosep: | ||||
config.InteractiveShell.separate_in = \ | ||||
config.InteractiveShell.separate_out = \ | ||||
Brian Granger
|
r2253 | config.InteractiveShell.separate_out2 = '' | ||
# if there is code of files to run from the cmd line, don't interact | ||||
# unless the -i flag (Global.force_interact) is true. | ||||
code_to_run = config.Global.get('code_to_run','') | ||||
file_to_run = False | ||||
Fernando Perez
|
r2439 | if self.extra_args and self.extra_args[0]: | ||
Brian Granger
|
r2253 | file_to_run = True | ||
if file_to_run or code_to_run: | ||||
if not config.Global.force_interact: | ||||
config.Global.interact = False | ||||
Brian Granger
|
r2203 | |||
Brian Granger
|
r2202 | def construct(self): | ||
Brian Granger
|
r2203 | # I am a little hesitant to put these into InteractiveShell itself. | ||
# But that might be the place for them | ||||
sys.path.insert(0, '') | ||||
Brian Granger
|
r2252 | |||
Brian Granger
|
r2203 | # Create an InteractiveShell instance | ||
Fernando Perez
|
r2392 | self.shell = InteractiveShell(None, self.master_config, | ||
**self.shell_params ) | ||||
Brian Granger
|
r2252 | |||
def post_construct(self): | ||||
"""Do actions after construct, but before starting the app.""" | ||||
Brian Granger
|
r2264 | config = self.master_config | ||
Brian Granger
|
r2252 | # shell.display_banner should always be False for the terminal | ||
# based app, because we call shell.show_banner() by hand below | ||||
# so the banner shows *before* all extension loading stuff. | ||||
self.shell.display_banner = False | ||||
Brian Granger
|
r2264 | if config.Global.display_banner and \ | ||
config.Global.interact: | ||||
Brian Granger
|
r2252 | self.shell.show_banner() | ||
# Make sure there is a space below the banner. | ||||
if self.log_level <= logging.INFO: print | ||||
Brian Granger
|
r2270 | # Now a variety of things that happen after the banner is printed. | ||
Fernando Perez
|
r2363 | self._enable_gui_pylab() | ||
Brian Granger
|
r2252 | self._load_extensions() | ||
self._run_exec_lines() | ||||
self._run_exec_files() | ||||
Brian Granger
|
r2253 | self._run_cmd_line_code() | ||
Fernando Perez
|
r2363 | self._configure_xmode() | ||
def _enable_gui_pylab(self): | ||||
"""Enable GUI event loop integration, taking pylab into account.""" | ||||
Global = self.master_config.Global | ||||
# Select which gui to use | ||||
Fernando Perez
|
r2388 | if Global.gui: | ||
gui = Global.gui | ||||
# The following are deprecated, but there's likely to be a lot of use | ||||
# of this form out there, so we might as well support it for now. But | ||||
# the --gui option above takes precedence. | ||||
elif Global.wthread: | ||||
Fernando Perez
|
r2363 | gui = inputhook.GUI_WX | ||
Fernando Perez
|
r2388 | elif Global.qthread: | ||
Fernando Perez
|
r2363 | gui = inputhook.GUI_QT | ||
elif Global.gthread: | ||||
gui = inputhook.GUI_GTK | ||||
else: | ||||
gui = None | ||||
Fernando Perez
|
r2388 | # Using --pylab will also require gui activation, though which toolkit | ||
# to use may be chosen automatically based on mpl configuration. | ||||
Fernando Perez
|
r2363 | if Global.pylab: | ||
activate = self.shell.enable_pylab | ||||
Fernando Perez
|
r2388 | if Global.pylab == 'auto': | ||
gui = None | ||||
else: | ||||
gui = Global.pylab | ||||
Fernando Perez
|
r2363 | else: | ||
# Enable only GUI integration, no pylab | ||||
activate = inputhook.enable_gui | ||||
if gui or Global.pylab: | ||||
try: | ||||
Fernando Perez
|
r2388 | self.log.info("Enabling GUI event loop integration, " | ||
"toolkit=%s, pylab=%s" % (gui, Global.pylab) ) | ||||
Fernando Perez
|
r2363 | activate(gui) | ||
except: | ||||
self.log.warn("Error in enabling GUI event loop integration:") | ||||
self.shell.showtraceback() | ||||
Brian Granger
|
r2252 | |||
def _load_extensions(self): | ||||
"""Load all IPython extensions in Global.extensions. | ||||
This uses the :meth:`InteractiveShell.load_extensions` to load all | ||||
the extensions listed in ``self.master_config.Global.extensions``. | ||||
""" | ||||
try: | ||||
if hasattr(self.master_config.Global, 'extensions'): | ||||
self.log.debug("Loading IPython extensions...") | ||||
extensions = self.master_config.Global.extensions | ||||
for ext in extensions: | ||||
try: | ||||
self.log.info("Loading IPython extension: %s" % ext) | ||||
self.shell.load_extension(ext) | ||||
except: | ||||
self.log.warn("Error in loading extension: %s" % ext) | ||||
self.shell.showtraceback() | ||||
except: | ||||
self.log.warn("Unknown error in loading extensions:") | ||||
self.shell.showtraceback() | ||||
def _run_exec_lines(self): | ||||
"""Run lines of code in Global.exec_lines in the user's namespace.""" | ||||
try: | ||||
if hasattr(self.master_config.Global, 'exec_lines'): | ||||
self.log.debug("Running code from Global.exec_lines...") | ||||
exec_lines = self.master_config.Global.exec_lines | ||||
for line in exec_lines: | ||||
try: | ||||
self.log.info("Running code in user namespace: %s" % line) | ||||
self.shell.runlines(line) | ||||
except: | ||||
self.log.warn("Error in executing line in user namespace: %s" % line) | ||||
self.shell.showtraceback() | ||||
except: | ||||
self.log.warn("Unknown error in handling Global.exec_lines:") | ||||
self.shell.showtraceback() | ||||
Brian Granger
|
r2253 | def _exec_file(self, fname): | ||
Brian Granger
|
r2328 | full_filename = filefind(fname, [u'.', self.ipython_dir]) | ||
Brian Granger
|
r2253 | if os.path.isfile(full_filename): | ||
Brian Granger
|
r2328 | if full_filename.endswith(u'.py'): | ||
Brian Granger
|
r2253 | self.log.info("Running file in user namespace: %s" % full_filename) | ||
self.shell.safe_execfile(full_filename, self.shell.user_ns) | ||||
elif full_filename.endswith('.ipy'): | ||||
self.log.info("Running file in user namespace: %s" % full_filename) | ||||
self.shell.safe_execfile_ipy(full_filename) | ||||
else: | ||||
self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename) | ||||
Brian Granger
|
r2252 | def _run_exec_files(self): | ||
try: | ||||
if hasattr(self.master_config.Global, 'exec_files'): | ||||
self.log.debug("Running files in Global.exec_files...") | ||||
exec_files = self.master_config.Global.exec_files | ||||
for fname in exec_files: | ||||
Brian Granger
|
r2253 | self._exec_file(fname) | ||
Brian Granger
|
r2252 | except: | ||
self.log.warn("Unknown error in handling Global.exec_files:") | ||||
self.shell.showtraceback() | ||||
Brian Granger
|
r2253 | def _run_cmd_line_code(self): | ||
if hasattr(self.master_config.Global, 'code_to_run'): | ||||
line = self.master_config.Global.code_to_run | ||||
try: | ||||
self.log.info("Running code given at command line (-c): %s" % line) | ||||
self.shell.runlines(line) | ||||
except: | ||||
self.log.warn("Error in executing line in user namespace: %s" % line) | ||||
self.shell.showtraceback() | ||||
return | ||||
# Like Python itself, ignore the second if the first of these is present | ||||
try: | ||||
fname = self.extra_args[0] | ||||
except: | ||||
pass | ||||
else: | ||||
try: | ||||
self._exec_file(fname) | ||||
except: | ||||
self.log.warn("Error in executing file in user namespace: %s" % fname) | ||||
self.shell.showtraceback() | ||||
Fernando Perez
|
r2363 | def _configure_xmode(self): | ||
# XXX - shouldn't this be read from the config? I'm still a little | ||||
# lost with all the details of handling the new config guys... | ||||
self.shell.InteractiveTB.set_mode(mode=self.shell.xmode) | ||||
Brian Granger
|
r2202 | def start_app(self): | ||
Brian Granger
|
r2253 | if self.master_config.Global.interact: | ||
self.log.debug("Starting IPython's mainloop...") | ||||
self.shell.mainloop() | ||||
Fernando Perez
|
r2391 | else: | ||
self.log.debug("IPython not interactive, start_app is no-op...") | ||||
Brian Granger
|
r2202 | |||
Fernando Perez
|
r2363 | |||
Brian Granger
|
r2322 | def load_default_config(ipython_dir=None): | ||
"""Load the default config file from the default ipython_dir. | ||||
Brian Granger
|
r2245 | |||
This is useful for embedded shells. | ||||
""" | ||||
Brian Granger
|
r2322 | if ipython_dir is None: | ||
ipython_dir = get_ipython_dir() | ||||
cl = PyFileConfigLoader(default_config_file_name, ipython_dir) | ||||
Brian Granger
|
r2245 | config = cl.load_config() | ||
return config | ||||
Brian Granger
|
r2269 | def launch_new_instance(): | ||
Brian Granger
|
r2296 | """Create and run a full blown IPython instance""" | ||
Brian Granger
|
r2202 | app = IPythonApp() | ||
Brian Granger
|
r2269 | app.start() | ||