##// END OF EJS Templates
Show invalid config message on TraitErrors during initialization...
MinRK -
Show More
@@ -26,6 +26,8 b' import sys'
26 26 from copy import deepcopy
27 27 from collections import defaultdict
28 28
29 from IPython.external.decorator import decorator
30
29 31 from IPython.config.configurable import SingletonConfigurable
30 32 from IPython.config.loader import (
31 33 KVArgParseConfigLoader, PyFileConfigLoader, Config, ArgumentError, ConfigFileNotFound,
@@ -69,6 +71,26 b" subcommand 'cmd', do: `{app} cmd -h`."
69 71 # Application class
70 72 #-----------------------------------------------------------------------------
71 73
74 @decorator
75 def catch_config(method, app, *args, **kwargs):
76 """Method decorator for catching invalid config (Trait/ArgumentErrors) during init.
77
78 On a TraitError (generally caused by bad config), this will print the trait's
79 message, and exit the app.
80
81 For use on init methods, to prevent invoking excepthook on invalid input.
82 """
83 try:
84 return method(app, *args, **kwargs)
85 except (TraitError, ArgumentError) as e:
86 app.print_description()
87 app.print_help()
88 app.print_examples()
89 app.log.fatal("Bad config encountered during initialization:")
90 app.log.fatal(str(e))
91 app.log.debug("Config at the time: %s", app.config)
92 app.exit(1)
93
72 94
73 95 class ApplicationError(Exception):
74 96 pass
@@ -173,6 +195,7 b' class Application(SingletonConfigurable):'
173 195 self._log_handler.setFormatter(self._log_formatter)
174 196 self.log.addHandler(self._log_handler)
175 197
198 @catch_config
176 199 def initialize(self, argv=None):
177 200 """Do the basic steps to configure me.
178 201
@@ -317,6 +340,7 b' class Application(SingletonConfigurable):'
317 340 # events.
318 341 self.config = newconfig
319 342
343 @catch_config
320 344 def initialize_subcommand(self, subc, argv=None):
321 345 """Initialize a subcommand with argv."""
322 346 subapp,help = self.subcommands.get(subc)
@@ -376,6 +400,7 b' class Application(SingletonConfigurable):'
376 400 flags[key] = (newflag, help)
377 401 return flags, aliases
378 402
403 @catch_config
379 404 def parse_command_line(self, argv=None):
380 405 """Parse the command line arguments."""
381 406 argv = sys.argv[1:] if argv is None else argv
@@ -402,18 +427,12 b' class Application(SingletonConfigurable):'
402 427
403 428 loader = KVArgParseConfigLoader(argv=argv, aliases=aliases,
404 429 flags=flags)
405 try:
406 430 config = loader.load_config()
407 431 self.update_config(config)
408 except (TraitError, ArgumentError) as e:
409 self.print_description()
410 self.print_help()
411 self.print_examples()
412 self.log.fatal(str(e))
413 self.exit(1)
414 432 # store unparsed args in extra_args
415 433 self.extra_args = loader.extra_args
416 434
435 @catch_config
417 436 def load_config_file(self, filename, path=None):
418 437 """Load a .py based config file by filename and path."""
419 438 loader = PyFileConfigLoader(filename, path=path)
@@ -34,7 +34,7 b' import os'
34 34 import shutil
35 35 import sys
36 36
37 from IPython.config.application import Application
37 from IPython.config.application import Application, catch_config
38 38 from IPython.config.configurable import Configurable
39 39 from IPython.config.loader import Config, ConfigFileNotFound
40 40 from IPython.core import release, crashhandler
@@ -304,7 +304,7 b' class BaseIPythonApplication(Application):'
304 304 with open(fname, 'w') as f:
305 305 f.write(s)
306 306
307
307 @catch_config
308 308 def initialize(self, argv=None):
309 309 # don't hook up crash handler before parsing command-line
310 310 self.parse_command_line(argv)
@@ -43,6 +43,7 b' from .handlers import (LoginHandler,'
43 43 )
44 44 from .notebookmanager import NotebookManager
45 45
46 from IPython.config.application import catch_config
46 47 from IPython.core.application import BaseIPythonApplication
47 48 from IPython.core.profiledir import ProfileDir
48 49 from IPython.zmq.session import Session, default_secure
@@ -260,6 +261,7 b' class NotebookApp(BaseIPythonApplication):'
260 261 # and all of its ancenstors until propagate is set to False.
261 262 self.log.propagate = False
262 263
264 @catch_config
263 265 def initialize(self, argv=None):
264 266 super(NotebookApp, self).initialize(argv)
265 267 self.init_configurables()
@@ -29,7 +29,7 b' import uuid'
29 29 from IPython.external.qt import QtGui
30 30
31 31 # Local imports
32 from IPython.config.application import boolean_flag
32 from IPython.config.application import boolean_flag, catch_config
33 33 from IPython.core.application import BaseIPythonApplication
34 34 from IPython.core.profiledir import ProfileDir
35 35 from IPython.lib.kernel import tunnel_to_kernel, find_connection_file
@@ -516,6 +516,7 b' class IPythonQtConsoleApp(BaseIPythonApplication):'
516 516 else:
517 517 raise IOError("Stylesheet %r not found."%self.stylesheet)
518 518
519 @catch_config
519 520 def initialize(self, argv=None):
520 521 super(IPythonQtConsoleApp, self).initialize(argv)
521 522 self.init_connection_file()
@@ -32,7 +32,7 b' import sys'
32 32 from IPython.config.loader import (
33 33 Config, PyFileConfigLoader, ConfigFileNotFound
34 34 )
35 from IPython.config.application import boolean_flag
35 from IPython.config.application import boolean_flag, catch_config
36 36 from IPython.core import release
37 37 from IPython.core import usage
38 38 from IPython.core.completer import Completer
@@ -286,6 +286,7 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):'
286 286
287 287 return super(TerminalIPythonApp, self).parse_command_line(argv)
288 288
289 @catch_config
289 290 def initialize(self, argv=None):
290 291 """Do actions after construct, but before starting the app."""
291 292 super(TerminalIPythonApp, self).initialize(argv)
@@ -29,6 +29,7 b' import sys'
29 29
30 30 from subprocess import Popen, PIPE
31 31
32 from IPython.config.application import catch_config
32 33 from IPython.core import release
33 34 from IPython.core.crashhandler import CrashHandler
34 35 from IPython.core.application import (
@@ -144,6 +145,7 b' class BaseParallelApplication(BaseIPythonApplication):'
144 145 aliases = Dict(base_aliases)
145 146 flags = Dict(base_flags)
146 147
148 @catch_config
147 149 def initialize(self, argv=None):
148 150 """initialize the app"""
149 151 super(BaseParallelApplication, self).initialize(argv)
@@ -31,7 +31,7 b' from subprocess import check_call, CalledProcessError, PIPE'
31 31 import zmq
32 32 from zmq.eventloop import ioloop
33 33
34 from IPython.config.application import Application, boolean_flag
34 from IPython.config.application import Application, boolean_flag, catch_config
35 35 from IPython.config.loader import Config
36 36 from IPython.core.application import BaseIPythonApplication
37 37 from IPython.core.profiledir import ProfileDir
@@ -269,6 +269,7 b' class IPClusterEngines(BaseParallelApplication):'
269 269 flags = Dict(engine_flags)
270 270 _stopping = False
271 271
272 @catch_config
272 273 def initialize(self, argv=None):
273 274 super(IPClusterEngines, self).initialize(argv)
274 275 self.init_signal()
@@ -41,9 +41,10 b' from IPython.parallel.apps.baseapp import ('
41 41 BaseParallelApplication,
42 42 base_aliases,
43 43 base_flags,
44 catch_config,
44 45 )
45 46 from IPython.utils.importstring import import_item
46 from IPython.utils.traitlets import Instance, Unicode, Bool, List, Dict
47 from IPython.utils.traitlets import Instance, Unicode, Bool, List, Dict, TraitError
47 48
48 49 from IPython.zmq.session import (
49 50 Session, session_aliases, session_flags, default_secure
@@ -263,7 +264,9 b' class IPControllerApp(BaseParallelApplication):'
263 264 self.factory = HubFactory(config=c, log=self.log)
264 265 # self.start_logging()
265 266 self.factory.init_hub()
266 except:
267 except TraitError:
268 raise
269 except Exception:
267 270 self.log.error("Couldn't construct the Controller", exc_info=True)
268 271 self.exit(1)
269 272
@@ -385,6 +388,7 b' class IPControllerApp(BaseParallelApplication):'
385 388 self.log.addHandler(handler)
386 389 self._log_handler = handler
387 390
391 @catch_config
388 392 def initialize(self, argv=None):
389 393 super(IPControllerApp, self).initialize(argv)
390 394 self.forward_logging()
@@ -34,6 +34,7 b' from IPython.parallel.apps.baseapp import ('
34 34 BaseParallelApplication,
35 35 base_aliases,
36 36 base_flags,
37 catch_config,
37 38 )
38 39 from IPython.zmq.log import EnginePUBHandler
39 40 from IPython.zmq.session import (
@@ -318,6 +319,7 b' class IPEngineApp(BaseParallelApplication):'
318 319 else:
319 320 mpi = None
320 321
322 @catch_config
321 323 def initialize(self, argv=None):
322 324 super(IPEngineApp, self).initialize(argv)
323 325 self.init_mpi()
@@ -30,7 +30,8 b' from IPython.utils.traitlets import Bool, Dict, Unicode'
30 30
31 31 from IPython.parallel.apps.baseapp import (
32 32 BaseParallelApplication,
33 base_aliases
33 base_aliases,
34 catch_config,
34 35 )
35 36 from IPython.parallel.apps.logwatcher import LogWatcher
36 37
@@ -68,6 +69,7 b' class IPLoggerApp(BaseParallelApplication):'
68 69 classes = [LogWatcher, ProfileDir]
69 70 aliases = Dict(aliases)
70 71
72 @catch_config
71 73 def initialize(self, argv=None):
72 74 super(IPLoggerApp, self).initialize(argv)
73 75 self.init_watcher()
@@ -28,7 +28,7 b' import zmq'
28 28
29 29 # Local imports.
30 30 from IPython.config.configurable import Configurable
31 from IPython.config.application import boolean_flag
31 from IPython.config.application import boolean_flag, catch_config
32 32 from IPython.core.application import ProfileDir
33 33 from IPython.core.error import StdinNotImplementedError
34 34 from IPython.core.shellapp import (
@@ -730,6 +730,8 b' class IPKernelApp(KernelApp, InteractiveShellApp):'
730 730 selecting a particular matplotlib backend and loop integration.
731 731 """
732 732 )
733
734 @catch_config
733 735 def initialize(self, argv=None):
734 736 super(IPKernelApp, self).initialize(argv)
735 737 self.init_shell()
@@ -26,7 +26,7 b' import zmq'
26 26 # IPython imports.
27 27 from IPython.core.ultratb import FormattedTB
28 28 from IPython.core.application import (
29 BaseIPythonApplication, base_flags, base_aliases
29 BaseIPythonApplication, base_flags, base_aliases, catch_config
30 30 )
31 31 from IPython.utils import io
32 32 from IPython.utils.localinterfaces import LOCALHOST
@@ -275,6 +275,7 b' class KernelApp(BaseIPythonApplication):'
275 275 )
276 276 self.kernel.record_ports(self.ports)
277 277
278 @catch_config
278 279 def initialize(self, argv=None):
279 280 super(KernelApp, self).initialize(argv)
280 281 self.init_blackhole()
General Comments 0
You need to be logged in to leave comments. Login now