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