def read_config(config_section, config_file="ants.cfg"): """Read indicated configuraton section and return a dict. Uses config.optionxform to preserver upper/lower case letters in config file. """ default_config = os.path.join(_ROOT, "etc", config_file) config_path = "/etc/ants/" system_config = os.path.join(config_path, config_file) if not os.path.isfile(default_config): raise OSError("Default config file not found at %s" % default_config) config = configparser.ConfigParser(strict=False) config.optionxform = str try: config.read([default_config, system_config]) except configparser.MissingSectionHeaderError as missing_section_error: configparser.ParsingError = missing_section_error print("Error while reading configuration from %s." % system_config) print("Ignoring system configuraiton") config.read([default_config]) config_dict = dict(config.items(config_section)) if use_macos_prefs: macos_dict = macos_prefs.read_prefs(config_section) config_dict = macos_prefs.merge_dicts(config_dict, macos_dict) # Add base search path to config # This is done to allow for easy append of custom paths while keeping the # default search path as well if config_section == "main": config_dict["ansible_callback_plugins_base_path"] = os.path.join( os.path.join(_ROOT, "plugins", "callback") ) return config_dict
raise OSError('Default config file not found at %s' % default_config) config = ConfigParser.ConfigParser() config.optionxform = str try: config.read([default_config, system_config]) except ConfigParser.MissingSectionHeaderError, ConfigParser.ParsingError: print 'Error while reading configuration from %s.' % system_config print 'Ignoring system configuraiton' config.read([default_config]) config_dict = dict(config.items(config_section)) if use_macos_prefs: macos_dict = macos_prefs.read_prefs(config_section) config_dict = macos_prefs.merge_dicts(config_dict, macos_dict) return config_dict def get_values(cfg, section_name): """Take and return a dict of values and prompt the user for a reply.""" msg = 'Configuration for section: %s' % section_name print "%s" % re.sub(r'[a-zA-Z :]', "#", msg) print "%s" % msg print "%s" % re.sub(r'[a-zA-Z :]', "#", msg) for key, value in cfg.iteritems(): cfg[key] = raw_input("%s [Example: %s]:" % (key, value)) or value return cfg