def save_config(self, config_dict, config_path, backup=True): """Save the config file, backing up as necessary.""" backup_path = weecfg.save(config_dict, config_path, backup) if backup_path: self.logger.log("Saved backup to %s" % backup_path) self.logger.log("Saved configuration to %s" % config_path)
def run(self, args, options): if options.version: print(weewx.__version__) sys.exit(0) if options.list_drivers: weecfg.print_drivers() sys.exit(0) # # If we got to this point, the verb must be --install, --upgrade, or --reconfigure. # Check for errors in the options. # # We can do one and only one of install, upgrade, and reconfigure: if (options.install and options.upgrade) \ or (options.install and options.reconfigure) \ or (options.upgrade and options.reconfigure): sys.exit("Must specify one and only one of --install, --upgrade, or --reconfigure.") # Check for missing --dist-config if (options.install or options.upgrade) and not options.dist_config: sys.exit("The commands --install and --upgrade require option --dist-config.") # Check for missing config file if options.upgrade and not (options.config_path or args): sys.exit("The command --upgrade requires an existing configuration file.") if options.install and not options.output: sys.exit("The --install command requires option --output.") # The install option does not take an old config file if options.install and (options.config_path or args): sys.exit("A configuration file cannot be used with the --install command.") # # Now run the commands. # # First, fiddle with option --altitude to convert it into a list: if options.altitude: options.altitude = options.altitude.split(",") if options.install or options.upgrade: # These options require a distribution config file. # Open it up and parse it: try: dist_config_dict = configobj.ConfigObj(options.dist_config, file_error=True, encoding='utf-8') except IOError as e: sys.exit("Unable to open distribution configuration file: %s" % e) except SyntaxError as e: sys.exit("Syntax error in distribution configuration file '%s': %s" % (options.dist_config, e)) # The install command uses the distribution config file as its input. # Other commands use an existing config file. if options.install: config_dict = dist_config_dict else: try: config_path, config_dict = weecfg.read_config(options.config_path, args) except SyntaxError as e: sys.exit("Syntax error in configuration file: %s" % e) except IOError as e: sys.exit("Unable to open configuration file: %s" % e) self.logger.log("Using configuration file %s" % config_path) if options.upgrade: # Update the config dictionary, then merge it with the distribution # dictionary weecfg.update_and_merge(config_dict, dist_config_dict) elif options.install or options.reconfigure: # Extract stn_info from the config_dict and command-line options: stn_info = self.get_stn_info(config_dict, options) # Use it to modify the configuration file. weecfg.modify_config(config_dict, stn_info, self.logger, options.debug) else: sys.exit("Internal logic error in config.py") # For the path to the final file, use whatever was specified by --output, # or the original path if that wasn't specified output_path = options.output or config_path # Save weewx.conf, backing up any old file. backup_path = weecfg.save(config_dict, output_path, not options.no_backup) if backup_path: self.logger.log("Saved backup to %s" % backup_path)