##// END OF EJS Templates
Restore terminal title when not running non-interactively
Maciej Goszczycki -
Show More
@@ -1,342 +1,343 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 The :class:`~traitlets.config.application.Application` object for the command
5 5 line :command:`ipython` program.
6 6 """
7 7
8 8 # Copyright (c) IPython Development Team.
9 9 # Distributed under the terms of the Modified BSD License.
10 10
11 11
12 12 import logging
13 13 import os
14 14 import sys
15 15 import warnings
16 16
17 17 from traitlets.config.loader import Config
18 18 from traitlets.config.application import boolean_flag, catch_config_error
19 19 from IPython.core import release
20 20 from IPython.core import usage
21 21 from IPython.core.completer import IPCompleter
22 22 from IPython.core.crashhandler import CrashHandler
23 23 from IPython.core.formatters import PlainTextFormatter
24 24 from IPython.core.history import HistoryManager
25 25 from IPython.core.application import (
26 26 ProfileDir, BaseIPythonApplication, base_flags, base_aliases
27 27 )
28 28 from IPython.core.magic import MagicsManager
29 29 from IPython.core.magics import (
30 30 ScriptMagics, LoggingMagics
31 31 )
32 32 from IPython.core.shellapp import (
33 33 InteractiveShellApp, shell_flags, shell_aliases
34 34 )
35 35 from IPython.extensions.storemagic import StoreMagics
36 36 from .interactiveshell import TerminalInteractiveShell
37 37 from IPython.paths import get_ipython_dir
38 38 from traitlets import (
39 39 Bool, List, default, observe, Type
40 40 )
41 41
42 42 #-----------------------------------------------------------------------------
43 43 # Globals, utilities and helpers
44 44 #-----------------------------------------------------------------------------
45 45
46 46 _examples = """
47 47 ipython --matplotlib # enable matplotlib integration
48 48 ipython --matplotlib=qt # enable matplotlib integration with qt4 backend
49 49
50 50 ipython --log-level=DEBUG # set logging to DEBUG
51 51 ipython --profile=foo # start with profile foo
52 52
53 53 ipython profile create foo # create profile foo w/ default config files
54 54 ipython help profile # show the help for the profile subcmd
55 55
56 56 ipython locate # print the path to the IPython directory
57 57 ipython locate profile foo # print the path to the directory for profile `foo`
58 58 """
59 59
60 60 #-----------------------------------------------------------------------------
61 61 # Crash handler for this application
62 62 #-----------------------------------------------------------------------------
63 63
64 64 class IPAppCrashHandler(CrashHandler):
65 65 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
66 66
67 67 def __init__(self, app):
68 68 contact_name = release.author
69 69 contact_email = release.author_email
70 70 bug_tracker = 'https://github.com/ipython/ipython/issues'
71 71 super(IPAppCrashHandler,self).__init__(
72 72 app, contact_name, contact_email, bug_tracker
73 73 )
74 74
75 75 def make_report(self,traceback):
76 76 """Return a string containing a crash report."""
77 77
78 78 sec_sep = self.section_sep
79 79 # Start with parent report
80 80 report = [super(IPAppCrashHandler, self).make_report(traceback)]
81 81 # Add interactive-specific info we may have
82 82 rpt_add = report.append
83 83 try:
84 84 rpt_add(sec_sep+"History of session input:")
85 85 for line in self.app.shell.user_ns['_ih']:
86 86 rpt_add(line)
87 87 rpt_add('\n*** Last line of input (may not be in above history):\n')
88 88 rpt_add(self.app.shell._last_input_line+'\n')
89 89 except:
90 90 pass
91 91
92 92 return ''.join(report)
93 93
94 94 #-----------------------------------------------------------------------------
95 95 # Aliases and Flags
96 96 #-----------------------------------------------------------------------------
97 97 flags = dict(base_flags)
98 98 flags.update(shell_flags)
99 99 frontend_flags = {}
100 100 addflag = lambda *args: frontend_flags.update(boolean_flag(*args))
101 101 addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax',
102 102 'Turn on auto editing of files with syntax errors.',
103 103 'Turn off auto editing of files with syntax errors.'
104 104 )
105 105 addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt',
106 106 "Force simple minimal prompt using `raw_input`",
107 107 "Use a rich interactive prompt with prompt_toolkit",
108 108 )
109 109
110 110 addflag('banner', 'TerminalIPythonApp.display_banner',
111 111 "Display a banner upon starting IPython.",
112 112 "Don't display a banner upon starting IPython."
113 113 )
114 114 addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit',
115 115 """Set to confirm when you try to exit IPython with an EOF (Control-D
116 116 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
117 117 you can force a direct exit without any confirmation.""",
118 118 "Don't prompt the user when exiting."
119 119 )
120 120 addflag('term-title', 'TerminalInteractiveShell.term_title',
121 121 "Enable auto setting the terminal title.",
122 122 "Disable auto setting the terminal title."
123 123 )
124 124 classic_config = Config()
125 125 classic_config.InteractiveShell.cache_size = 0
126 126 classic_config.PlainTextFormatter.pprint = False
127 127 classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts'
128 128 classic_config.InteractiveShell.separate_in = ''
129 129 classic_config.InteractiveShell.separate_out = ''
130 130 classic_config.InteractiveShell.separate_out2 = ''
131 131 classic_config.InteractiveShell.colors = 'NoColor'
132 132 classic_config.InteractiveShell.xmode = 'Plain'
133 133
134 134 frontend_flags['classic']=(
135 135 classic_config,
136 136 "Gives IPython a similar feel to the classic Python prompt."
137 137 )
138 138 # # log doesn't make so much sense this way anymore
139 139 # paa('--log','-l',
140 140 # action='store_true', dest='InteractiveShell.logstart',
141 141 # help="Start logging to the default log file (./ipython_log.py).")
142 142 #
143 143 # # quick is harder to implement
144 144 frontend_flags['quick']=(
145 145 {'TerminalIPythonApp' : {'quick' : True}},
146 146 "Enable quick startup with no config files."
147 147 )
148 148
149 149 frontend_flags['i'] = (
150 150 {'TerminalIPythonApp' : {'force_interact' : True}},
151 151 """If running code from the command line, become interactive afterwards.
152 152 It is often useful to follow this with `--` to treat remaining flags as
153 153 script arguments.
154 154 """
155 155 )
156 156 flags.update(frontend_flags)
157 157
158 158 aliases = dict(base_aliases)
159 159 aliases.update(shell_aliases)
160 160
161 161 #-----------------------------------------------------------------------------
162 162 # Main classes and functions
163 163 #-----------------------------------------------------------------------------
164 164
165 165
166 166 class LocateIPythonApp(BaseIPythonApplication):
167 167 description = """print the path to the IPython dir"""
168 168 subcommands = dict(
169 169 profile=('IPython.core.profileapp.ProfileLocate',
170 170 "print the path to an IPython profile directory",
171 171 ),
172 172 )
173 173 def start(self):
174 174 if self.subapp is not None:
175 175 return self.subapp.start()
176 176 else:
177 177 print(self.ipython_dir)
178 178
179 179
180 180 class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):
181 181 name = u'ipython'
182 182 description = usage.cl_usage
183 183 crash_handler_class = IPAppCrashHandler
184 184 examples = _examples
185 185
186 186 flags = flags
187 187 aliases = aliases
188 188 classes = List()
189 189
190 190 interactive_shell_class = Type(
191 191 klass=object, # use default_value otherwise which only allow subclasses.
192 192 default_value=TerminalInteractiveShell,
193 193 help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends"
194 194 ).tag(config=True)
195 195
196 196 @default('classes')
197 197 def _classes_default(self):
198 198 """This has to be in a method, for TerminalIPythonApp to be available."""
199 199 return [
200 200 InteractiveShellApp, # ShellApp comes before TerminalApp, because
201 201 self.__class__, # it will also affect subclasses (e.g. QtConsole)
202 202 TerminalInteractiveShell,
203 203 HistoryManager,
204 204 MagicsManager,
205 205 ProfileDir,
206 206 PlainTextFormatter,
207 207 IPCompleter,
208 208 ScriptMagics,
209 209 LoggingMagics,
210 210 StoreMagics,
211 211 ]
212 212
213 213 subcommands = dict(
214 214 profile = ("IPython.core.profileapp.ProfileApp",
215 215 "Create and manage IPython profiles."
216 216 ),
217 217 kernel = ("ipykernel.kernelapp.IPKernelApp",
218 218 "Start a kernel without an attached frontend."
219 219 ),
220 220 locate=('IPython.terminal.ipapp.LocateIPythonApp',
221 221 LocateIPythonApp.description
222 222 ),
223 223 history=('IPython.core.historyapp.HistoryApp',
224 224 "Manage the IPython history database."
225 225 ),
226 226 )
227 227
228 228
229 229 # *do* autocreate requested profile, but don't create the config file.
230 230 auto_create=Bool(True)
231 231 # configurables
232 232 quick = Bool(False,
233 233 help="""Start IPython quickly by skipping the loading of config files."""
234 234 ).tag(config=True)
235 235 @observe('quick')
236 236 def _quick_changed(self, change):
237 237 if change['new']:
238 238 self.load_config_file = lambda *a, **kw: None
239 239
240 240 display_banner = Bool(True,
241 241 help="Whether to display a banner upon starting IPython."
242 242 ).tag(config=True)
243 243
244 244 # if there is code of files to run from the cmd line, don't interact
245 245 # unless the --i flag (App.force_interact) is true.
246 246 force_interact = Bool(False,
247 247 help="""If a command or file is given via the command-line,
248 248 e.g. 'ipython foo.py', start an interactive shell after executing the
249 249 file or command."""
250 250 ).tag(config=True)
251 251 @observe('force_interact')
252 252 def _force_interact_changed(self, change):
253 253 if change['new']:
254 254 self.interact = True
255 255
256 256 @observe('file_to_run', 'code_to_run', 'module_to_run')
257 257 def _file_to_run_changed(self, change):
258 258 new = change['new']
259 259 if new:
260 260 self.something_to_run = True
261 261 if new and not self.force_interact:
262 262 self.interact = False
263 263
264 264 # internal, not-configurable
265 265 something_to_run=Bool(False)
266 266
267 267 @catch_config_error
268 268 def initialize(self, argv=None):
269 269 """Do actions after construct, but before starting the app."""
270 270 super(TerminalIPythonApp, self).initialize(argv)
271 271 if self.subapp is not None:
272 272 # don't bother initializing further, starting subapp
273 273 return
274 274 # print self.extra_args
275 275 if self.extra_args and not self.something_to_run:
276 276 self.file_to_run = self.extra_args[0]
277 277 self.init_path()
278 278 # create the shell
279 279 self.init_shell()
280 280 # and draw the banner
281 281 self.init_banner()
282 282 # Now a variety of things that happen after the banner is printed.
283 283 self.init_gui_pylab()
284 284 self.init_extensions()
285 285 self.init_code()
286 286
287 287 def init_shell(self):
288 288 """initialize the InteractiveShell instance"""
289 289 # Create an InteractiveShell instance.
290 290 # shell.display_banner should always be False for the terminal
291 291 # based app, because we call shell.show_banner() by hand below
292 292 # so the banner shows *before* all extension loading stuff.
293 293 self.shell = self.interactive_shell_class.instance(parent=self,
294 294 profile_dir=self.profile_dir,
295 295 ipython_dir=self.ipython_dir, user_ns=self.user_ns)
296 296 self.shell.configurables.append(self)
297 297
298 298 def init_banner(self):
299 299 """optionally display the banner"""
300 300 if self.display_banner and self.interact:
301 301 self.shell.show_banner()
302 302 # Make sure there is a space below the banner.
303 303 if self.log_level <= logging.INFO: print()
304 304
305 305 def _pylab_changed(self, name, old, new):
306 306 """Replace --pylab='inline' with --pylab='auto'"""
307 307 if new == 'inline':
308 308 warnings.warn("'inline' not available as pylab backend, "
309 309 "using 'auto' instead.")
310 310 self.pylab = 'auto'
311 311
312 312 def start(self):
313 313 if self.subapp is not None:
314 314 return self.subapp.start()
315 315 # perform any prexec steps:
316 316 if self.interact:
317 317 self.log.debug("Starting IPython's mainloop...")
318 318 self.shell.mainloop()
319 319 else:
320 320 self.log.debug("IPython not interactive...")
321 self.shell.restore_term_title()
321 322 if not self.shell.last_execution_succeeded:
322 323 sys.exit(1)
323 324
324 325 def load_default_config(ipython_dir=None):
325 326 """Load the default config file from the default ipython_dir.
326 327
327 328 This is useful for embedded shells.
328 329 """
329 330 if ipython_dir is None:
330 331 ipython_dir = get_ipython_dir()
331 332
332 333 profile_dir = os.path.join(ipython_dir, 'profile_default')
333 334 app = TerminalIPythonApp()
334 335 app.config_file_paths.append(profile_dir)
335 336 app.load_config_file()
336 337 return app.config
337 338
338 339 launch_new_instance = TerminalIPythonApp.launch_instance
339 340
340 341
341 342 if __name__ == '__main__':
342 343 launch_new_instance()
General Comments 0
You need to be logged in to leave comments. Login now