From 7dbabd19602a574ba44a5258b8fff01acf4aa335 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Fri, 22 Nov 2013 07:14:51 +0100 Subject: [PATCH] config: Initialize config with defaults Try and create args.config_file[-1] with the default config commented out. We assume that the directory the config file is in exists. --- mopidy/__main__.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/mopidy/__main__.py b/mopidy/__main__.py index 3b6e5511..82e4569b 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -40,9 +40,6 @@ def main(): signal.signal(signal.SIGUSR1, pykka.debug.log_thread_tracebacks) try: - create_file_structures() - check_old_locations() - root_cmd = commands.RootCommand() config_cmd = commands.ConfigCommand() deps_cmd = commands.DepsCommand() @@ -61,6 +58,9 @@ def main(): args = root_cmd.parse(mopidy_args) + create_file_structures_and_config(args, installed_extensions) + check_old_locations() + config, config_errors = config_lib.load( args.config_files, installed_extensions, args.config_overrides) @@ -123,9 +123,22 @@ def main(): raise -def create_file_structures(): +def create_file_structures_and_config(args, extensions): path.get_or_create_dir(b'$XDG_DATA_DIR/mopidy') - path.get_or_create_file(b'$XDG_CONFIG_DIR/mopidy/mopidy.conf') + path.get_or_create_dir(b'$XDG_CONFIG_DIR/mopidy') + + # Initialize whatever the last config file is with defaults + config_file = args.config_files[-1] + if os.path.exists(config_file): + return + + try: + default = config_lib.format_initial(extensions) + path.get_or_create_file(config_file, mkdir=False, content=default) + logger.info('Initialized %s with default config', config_file) + except IOError as e: + logger.warning('Unable to initialize %s with default config: %s', + config_file, e) def check_old_locations():