From f244d94b52332dbad08e0df6d52251fb026951d0 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 16 Sep 2013 23:20:18 +0200 Subject: [PATCH] main: Split main() function in two By separating the teardown procedures for errors that happens before and after actors are started, we get a lot less output when failing on config errors. This makes the config errors the last lines printed by the `mopidy` command, making them easier to spot. Related to #467 --- mopidy/__main__.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/mopidy/__main__.py b/mopidy/__main__.py index 2323768f..c426266b 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -36,15 +36,12 @@ def main(): if args.show_deps: commands.show_deps() - loop = gobject.MainLoop() - enabled_extensions = [] # Make sure it is defined before the finally block - logging_initialized = False - # TODO: figure out a way to make the boilerplate in this file reusable in # scanner and other places we need it. try: # Initial config without extensions to bootstrap logging. + logging_initialized = False logging_config, _ = config_lib.load( args.config_files, [], args.config_overrides) @@ -61,6 +58,7 @@ def main(): args.config_files, installed_extensions, args.config_overrides) # Filter out disabled extensions and remove any config errors for them. + enabled_extensions = [] for extension in installed_extensions: enabled = config[extension.ext_name]['enabled'] if ext.validate_extension(extension) and enabled: @@ -77,21 +75,34 @@ def main(): log.setup_log_levels(proxied_config) check_old_locations() ext.register_gstreamer_elements(enabled_extensions) - - # Anything that wants to exit after this point must use - # mopidy.utils.process.exit_process as actors have been started. - audio = setup_audio(proxied_config) - backends = setup_backends(proxied_config, enabled_extensions, audio) - core = setup_core(audio, backends) - setup_frontends(proxied_config, enabled_extensions, core) - loop.run() except KeyboardInterrupt: if logging_initialized: logger.info('Interrupted. Exiting...') + return except Exception as ex: if logging_initialized: logger.exception(ex) raise + + # Anything that wants to exit after this point must use + # mopidy.utils.process.exit_process as actors have been started. + start(proxied_config, enabled_extensions) + + +def start(config, enabled_extensions): + loop = gobject.MainLoop() + try: + audio = setup_audio(config) + backends = setup_backends(config, enabled_extensions, audio) + core = setup_core(audio, backends) + setup_frontends(config, enabled_extensions, core) + loop.run() + except KeyboardInterrupt: + logger.info('Interrupted. Exiting...') + return + except Exception as ex: + logger.exception(ex) + raise finally: loop.quit() stop_frontends(enabled_extensions)