##// END OF EJS Templates
And again
And again

File last commit:

r27831:71f7729b
r27903:10a27e1d
Show More
ipapp.py
343 lines | 12.1 KiB | text/x-python | PythonLexer
Brian Granger
Massive, crazy refactoring of everything....
r2202 #!/usr/bin/env python
# encoding: utf-8
"""
Matthias Bussonnier
Allow to get cellid from ipykernel...
r27616 The :class:`~traitlets.config.application.Application` object for the command
Brian Granger
ipcontroller/ipengine use the new clusterdir.py module.
r2301 line :command:`ipython` program.
Brian Granger
Massive, crazy refactoring of everything....
r2202 """
Min RK
Don't blend kernel and frontend CLI args...
r20890 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
More work on the crash handler....
r2506
Brian Granger
Massive, crazy refactoring of everything....
r2202
Brian Granger
Work on startup related things....
r2252 import logging
Brian Granger
More work on getting rid of ipmaker.
r2203 import os
import sys
Min RK
adopt traitlets 4.2 API...
r22340 import warnings
Brian Granger
More work on getting rid of ipmaker.
r2203
Min RK
update dependency imports...
r21253 from traitlets.config.loader import Config
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 from traitlets.config.application import boolean_flag, catch_config_error
Brian Granger
More work on the crash handler....
r2506 from IPython.core import release
MinRK
Terminal IPython working with newapp
r3963 from IPython.core import usage
MinRK
move completer configurables to IPCompleter where they belong...
r5231 from IPython.core.completer import IPCompleter
Brian Granger
More work on the crash handler....
r2506 from IPython.core.crashhandler import CrashHandler
MinRK
Terminal IPython working with newapp
r3963 from IPython.core.formatters import PlainTextFormatter
MinRK
add HistoryManager to class list...
r6823 from IPython.core.history import HistoryManager
MinRK
rename core.newapplication -> core.application
r4023 from IPython.core.application import (
MinRK
Terminal IPython working with newapp
r3963 ProfileDir, BaseIPythonApplication, base_flags, base_aliases
Brian Granger
Massive refactoring of of the core....
r2245 )
Matthias Bussonnier
Allow to configure lazy loadable magics....
r27503 from IPython.core.magic import MagicsManager
Joshua Storck
Adding a -q option to the logstart magic to suppress output of log state. Also added a command line option
r23624 from IPython.core.magics import (
ScriptMagics, LoggingMagics
)
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 from IPython.core.shellapp import (
InteractiveShellApp, shell_flags, shell_aliases
)
Thomas Kluyver
Restore autorestore option for storemagic....
r12331 from IPython.extensions.storemagic import StoreMagics
Min RK
undeprecate terminal.interactiveshell...
r22549 from .interactiveshell import TerminalInteractiveShell
Min RK
update dependency imports...
r21253 from IPython.paths import get_ipython_dir
from traitlets import (
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 Bool, List, default, observe, Type
MinRK
Terminal IPython working with newapp
r3963 )
Brian Granger
More work on getting rid of ipmaker.
r2203
#-----------------------------------------------------------------------------
Fernando Perez
Unify command-line usage information in one place....
r2427 # Globals, utilities and helpers
Brian Granger
More work on getting rid of ipmaker.
r2203 #-----------------------------------------------------------------------------
Brian Granger
More work on adding examples to help strings.
r4216 _examples = """
Paul Ivanov
replace --pylab flag with --matplotlib in usage...
r12015 ipython --matplotlib # enable matplotlib integration
Bing Xia
Fix typo in ipapp
r12154 ipython --matplotlib=qt # enable matplotlib integration with qt4 backend
Paul Ivanov
replace --pylab flag with --matplotlib in usage...
r12015
Brian E. Granger
Fixing command line options and help strings to use new syntax....
r4219 ipython --log-level=DEBUG # set logging to DEBUG
Brian Granger
More work on adding examples to help strings.
r4216 ipython --profile=foo # start with profile foo
Brian E. Granger
Finishing up help string work.
r4218
MinRK
reorder profile & notebook in ipython help
r6169 ipython profile create foo # create profile foo w/ default config files
ipython help profile # show the help for the profile subcmd
MinRK
add locate subcommands for IPython
r6901
ipython locate # print the path to the IPython directory
ipython locate profile foo # print the path to the directory for profile `foo`
Brian Granger
More work on adding examples to help strings.
r4216 """
Brian Granger
Refactored the command line config system and other aspects of config....
r2501
Fernando Perez
Unify command-line usage information in one place....
r2427 #-----------------------------------------------------------------------------
Brian Granger
More work on the crash handler....
r2506 # Crash handler for this application
#-----------------------------------------------------------------------------
class IPAppCrashHandler(CrashHandler):
"""sys.excepthook for IPython itself, leaves a detailed report on disk."""
def __init__(self, app):
MinRK
update release/authors...
r9188 contact_name = release.author
MinRK
use ipython-dev as the email address for crash reports
r5316 contact_email = release.author_email
bug_tracker = 'https://github.com/ipython/ipython/issues'
Brian Granger
More work on the crash handler....
r2506 super(IPAppCrashHandler,self).__init__(
app, contact_name, contact_email, bug_tracker
)
def make_report(self,traceback):
"""Return a string containing a crash report."""
sec_sep = self.section_sep
# Start with parent report
report = [super(IPAppCrashHandler, self).make_report(traceback)]
# Add interactive-specific info we may have
rpt_add = report.append
try:
rpt_add(sec_sep+"History of session input:")
for line in self.app.shell.user_ns['_ih']:
rpt_add(line)
rpt_add('\n*** Last line of input (may not be in above history):\n')
rpt_add(self.app.shell._last_input_line+'\n')
except:
pass
return ''.join(report)
MinRK
Terminal IPython working with newapp
r3963 #-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------
flags = dict(base_flags)
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 flags.update(shell_flags)
MinRK
update flags&aliases for two-process apps...
r5610 frontend_flags = {}
addflag = lambda *args: frontend_flags.update(boolean_flag(*args))
MinRK
Terminal IPython working with newapp
r3963 addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax',
'Turn on auto editing of files with syntax errors.',
'Turn off auto editing of files with syntax errors.'
)
Matthias Bussonnier
Add flag for simple_prompt....
r22416 addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt',
"Force simple minimal prompt using `raw_input`",
"Use a rich interactive prompt with prompt_toolkit",
)
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 addflag('banner', 'TerminalIPythonApp.display_banner',
MinRK
Terminal IPython working with newapp
r3963 "Display a banner upon starting IPython.",
"Don't display a banner upon starting IPython."
)
addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit',
"""Set to confirm when you try to exit IPython with an EOF (Control-D
MinRK
minor helpstring cleanup per review.
r3967 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
you can force a direct exit without any confirmation.""",
MinRK
Terminal IPython working with newapp
r3963 "Don't prompt the user when exiting."
)
addflag('term-title', 'TerminalInteractiveShell.term_title',
"Enable auto setting the terminal title.",
"Disable auto setting the terminal title."
)
classic_config = Config()
classic_config.InteractiveShell.cache_size = 0
classic_config.PlainTextFormatter.pprint = False
Matthias Bussonnier
Remove a few usage of PromptManager in example...
r22447 classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts'
MinRK
Terminal IPython working with newapp
r3963 classic_config.InteractiveShell.separate_in = ''
classic_config.InteractiveShell.separate_out = ''
classic_config.InteractiveShell.separate_out2 = ''
classic_config.InteractiveShell.colors = 'NoColor'
classic_config.InteractiveShell.xmode = 'Plain'
MinRK
update flags&aliases for two-process apps...
r5610 frontend_flags['classic']=(
MinRK
Terminal IPython working with newapp
r3963 classic_config,
"Gives IPython a similar feel to the classic Python prompt."
)
# # log doesn't make so much sense this way anymore
# paa('--log','-l',
# action='store_true', dest='InteractiveShell.logstart',
# help="Start logging to the default log file (./ipython_log.py).")
#
# # quick is harder to implement
MinRK
update flags&aliases for two-process apps...
r5610 frontend_flags['quick']=(
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 {'TerminalIPythonApp' : {'quick' : True}},
MinRK
Terminal IPython working with newapp
r3963 "Enable quick startup with no config files."
)
MinRK
update flags&aliases for two-process apps...
r5610 frontend_flags['i'] = (
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 {'TerminalIPythonApp' : {'force_interact' : True}},
Joel Nothman
Note use of -- for -i to behave like cpython...
r21556 """If running code from the command line, become interactive afterwards.
It is often useful to follow this with `--` to treat remaining flags as
script arguments.
"""
MinRK
Terminal IPython working with newapp
r3963 )
MinRK
update flags&aliases for two-process apps...
r5610 flags.update(frontend_flags)
MinRK
Terminal IPython working with newapp
r3963
aliases = dict(base_aliases)
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 aliases.update(shell_aliases)
MinRK
Terminal IPython working with newapp
r3963
Brian Granger
More work on the crash handler....
r2506 #-----------------------------------------------------------------------------
Fernando Perez
Unify command-line usage information in one place....
r2427 # Main classes and functions
#-----------------------------------------------------------------------------
Brian Granger
Massive refactoring of of the core....
r2245
MinRK
add locate subcommands for IPython
r6901
class LocateIPythonApp(BaseIPythonApplication):
description = """print the path to the IPython dir"""
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 subcommands = dict(
MinRK
add locate subcommands for IPython
r6901 profile=('IPython.core.profileapp.ProfileLocate',
"print the path to an IPython profile directory",
),
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 )
MinRK
add locate subcommands for IPython
r6901 def start(self):
if self.subapp is not None:
return self.subapp.start()
else:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print(self.ipython_dir)
MinRK
add locate subcommands for IPython
r6901
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):
Brian Granger
Beginning to transition all paths, files, dirs over to unicode....
r2328 name = u'ipython'
MinRK
Terminal IPython working with newapp
r3963 description = usage.cl_usage
Brian Granger
More work on the crash handler....
r2506 crash_handler_class = IPAppCrashHandler
Brian Granger
More work on adding examples to help strings.
r4216 examples = _examples
Brian Granger
Command line examples added for non-parallel apps.
r4215
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 flags = flags
aliases = aliases
MinRK
move InteractiveShellApp before TerminalIPythonApp in TerminalApp class list...
r4462 classes = List()
Matthias Bussonnier
Make the TerminalInteractiveShell class configurable....
r23505
interactive_shell_class = Type(
klass=object, # use default_value otherwise which only allow subclasses.
default_value=TerminalInteractiveShell,
help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends"
).tag(config=True)
Min RK
adopt traitlets 4.2 API...
r22340 @default('classes')
MinRK
move InteractiveShellApp before TerminalIPythonApp in TerminalApp class list...
r4462 def _classes_default(self):
"""This has to be in a method, for TerminalIPythonApp to be available."""
return [
InteractiveShellApp, # ShellApp comes before TerminalApp, because
self.__class__, # it will also affect subclasses (e.g. QtConsole)
TerminalInteractiveShell,
MinRK
add HistoryManager to class list...
r6823 HistoryManager,
Matthias Bussonnier
Allow to configure lazy loadable magics....
r27503 MagicsManager,
MinRK
move InteractiveShellApp before TerminalIPythonApp in TerminalApp class list...
r4462 ProfileDir,
PlainTextFormatter,
MinRK
move completer configurables to IPCompleter where they belong...
r5231 IPCompleter,
MinRK
add ScriptMagics to class list for generated config
r7417 ScriptMagics,
Joshua Storck
Adding a -q option to the logstart magic to suppress output of log state. Also added a command line option
r23624 LoggingMagics,
Thomas Kluyver
Restore autorestore option for storemagic....
r12331 StoreMagics,
MinRK
move InteractiveShellApp before TerminalIPythonApp in TerminalApp class list...
r4462 ]
Bernardo B. Marques
remove all trailling spaces
r4872
Matthias Bussonnier
Actually warn that `ipython <subcommand>` is deprecated....
r21836 subcommands = dict(
profile = ("IPython.core.profileapp.ProfileApp",
"Create and manage IPython profiles."
),
kernel = ("ipykernel.kernelapp.IPKernelApp",
"Start a kernel without an attached frontend."
),
locate=('IPython.terminal.ipapp.LocateIPythonApp',
LocateIPythonApp.description
),
history=('IPython.core.historyapp.HistoryApp',
"Manage the IPython history database."
),
)
Matthias Bussonnier
Remove more deprecated utilities....
r27221
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
default config files are automatically generated...
r4025 # *do* autocreate requested profile, but don't create the config file.
MinRK
Terminal IPython working with newapp
r3963 auto_create=Bool(True)
# configurables
Min RK
adopt traitlets 4.2 API...
r22340 quick = Bool(False,
MinRK
Terminal IPython working with newapp
r3963 help="""Start IPython quickly by skipping the loading of config files."""
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
@observe('quick')
def _quick_changed(self, change):
if change['new']:
MinRK
Terminal IPython working with newapp
r3963 self.load_config_file = lambda *a, **kw: None
Min RK
adopt traitlets 4.2 API...
r22340 display_banner = Bool(True,
MinRK
Terminal IPython working with newapp
r3963 help="Whether to display a banner upon starting IPython."
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
MinRK
Terminal IPython working with newapp
r3963
# if there is code of files to run from the cmd line, don't interact
# unless the --i flag (App.force_interact) is true.
Min RK
adopt traitlets 4.2 API...
r22340 force_interact = Bool(False,
MinRK
Terminal IPython working with newapp
r3963 help="""If a command or file is given via the command-line,
Wieland Hoffmann
Complete the docstring for the force_interactive flag
r13929 e.g. 'ipython foo.py', start an interactive shell after executing the
file or command."""
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
@observe('force_interact')
def _force_interact_changed(self, change):
if change['new']:
MinRK
Terminal IPython working with newapp
r3963 self.interact = True
Bernardo B. Marques
remove all trailling spaces
r4872
Min RK
adopt traitlets 4.2 API...
r22340 @observe('file_to_run', 'code_to_run', 'module_to_run')
def _file_to_run_changed(self, change):
new = change['new']
Bradley M. Froehle
Add "something_to_run" flag which is set to True if there is a module, file, or code to run.
r6081 if new:
self.something_to_run = True
MinRK
Terminal IPython working with newapp
r3963 if new and not self.force_interact:
self.interact = False
# internal, not-configurable
Bradley M. Froehle
Add "something_to_run" flag which is set to True if there is a module, file, or code to run.
r6081 something_to_run=Bool(False)
MinRK
Terminal IPython working with newapp
r3963
MinRK
catch_config -> catch_config_error
r5214 @catch_config_error
MinRK
Terminal IPython working with newapp
r3963 def initialize(self, argv=None):
"""Do actions after construct, but before starting the app."""
MinRK
Split generic part of terminal frontend/terminal/ipapp into core/shellapp...
r3968 super(TerminalIPythonApp, self).initialize(argv)
MinRK
add qtconsole as subapp of terminal ipapp...
r3982 if self.subapp is not None:
# don't bother initializing further, starting subapp
return
MinRK
Terminal IPython working with newapp
r3963 # print self.extra_args
Bradley M. Froehle
Add "something_to_run" flag which is set to True if there is a module, file, or code to run.
r6081 if self.extra_args and not self.something_to_run:
MinRK
Terminal IPython working with newapp
r3963 self.file_to_run = self.extra_args[0]
Bradley M. Froehle
Move sys.path.insert(0, '') from subclasses to InteractiveShellApp
r6695 self.init_path()
MinRK
Terminal IPython working with newapp
r3963 # create the shell
self.init_shell()
# and draw the banner
self.init_banner()
# Now a variety of things that happen after the banner is printed.
self.init_gui_pylab()
self.init_extensions()
self.init_code()
def init_shell(self):
"""initialize the InteractiveShell instance"""
Brian Granger
First draft of refactored Component->Configurable.
r2731 # Create an InteractiveShell instance.
Bernardo B. Marques
remove all trailling spaces
r4872 # shell.display_banner should always be False for the terminal
Brian Granger
Work on startup related things....
r2252 # based app, because we call shell.show_banner() by hand below
# so the banner shows *before* all extension loading stuff.
Matthias Bussonnier
Make the TerminalInteractiveShell class configurable....
r23505 self.shell = self.interactive_shell_class.instance(parent=self,
Min RK
Remove unused `display_banner=False` arg...
r22168 profile_dir=self.profile_dir,
Thomas Kluyver
Fix passing user_ns to IPython.start_ipython()
r12159 ipython_dir=self.ipython_dir, user_ns=self.user_ns)
MinRK
include parent Application in InteractiveShell.configurables...
r5315 self.shell.configurables.append(self)
Brian Granger
Work on startup related things....
r2252
MinRK
Terminal IPython working with newapp
r3963 def init_banner(self):
"""optionally display the banner"""
if self.display_banner and self.interact:
self.shell.show_banner()
Brian Granger
Work on startup related things....
r2252 # Make sure there is a space below the banner.
Thomas Kluyver
Convert print statements to print function calls...
r13348 if self.log_level <= logging.INFO: print()
Brian Granger
Work on startup related things....
r2252
Bradley M. Froehle
Move gui and pylab options to InteractiveShellApp.
r7096 def _pylab_changed(self, name, old, new):
"""Replace --pylab='inline' with --pylab='auto'"""
if new == 'inline':
Min RK
adopt traitlets 4.2 API...
r22340 warnings.warn("'inline' not available as pylab backend, "
Thomas Kluyver
Fix IPython.utils.warn API so messages are automatically displayed followed by a newline.
r8223 "using 'auto' instead.")
Bradley M. Froehle
Move gui and pylab options to InteractiveShellApp.
r7096 self.pylab = 'auto'
Brian Granger
Work on startup related things....
r2252
MinRK
Terminal IPython working with newapp
r3963 def start(self):
MinRK
add qtconsole as subapp of terminal ipapp...
r3982 if self.subapp is not None:
return self.subapp.start()
MinRK
Terminal IPython working with newapp
r3963 # perform any prexec steps:
if self.interact:
Brian Granger
All code startup related things are working....
r2253 self.log.debug("Starting IPython's mainloop...")
self.shell.mainloop()
Fernando Perez
Manage and propagate argv correctly....
r2391 else:
MinRK
Terminal IPython working with newapp
r3963 self.log.debug("IPython not interactive...")
Maciej Goszczycki
Restore terminal title when not running non-interactively
r27831 self.shell.restore_term_title()
Thomas Kluyver
Exit code 1 if non-interactive execution fails...
r23933 if not self.shell.last_execution_succeeded:
sys.exit(1)
Brian Granger
Massive, crazy refactoring of everything....
r2202
Brian Granger
Lots of work on command line options and env vars....
r2322 def load_default_config(ipython_dir=None):
"""Load the default config file from the default ipython_dir.
Brian Granger
Massive refactoring of of the core....
r2245
This is useful for embedded shells.
"""
Brian Granger
Lots of work on command line options and env vars....
r2322 if ipython_dir is None:
ipython_dir = get_ipython_dir()
Matthias BUSSONNIER
allow to load config from json file...
r13771
MinRK
Terminal IPython working with newapp
r3963 profile_dir = os.path.join(ipython_dir, 'profile_default')
Min RK
avoid using private traitlets APIs in load_default_config...
r23784 app = TerminalIPythonApp()
app.config_file_paths.append(profile_dir)
app.load_config_file()
return app.config
Brian Granger
Massive refactoring of of the core....
r2245
MinRK
Application.launch_instance...
r11176 launch_new_instance = TerminalIPythonApp.launch_instance
Brian Granger
Refactored the command line config system and other aspects of config....
r2501
Brian Granger
Removed the top-level iptest.py and INSTALLED logic....
r2507
if __name__ == '__main__':
launch_new_instance()