def update_settings(settings, file_path): """merge settings in 'file_path' with 'settings' :param settings: settings to be merge with (configure.Configuration) :param file_path: path to file with settings to be merged :return: merged settings """ LOG.debug("Loading setting file: %s" % file_path) if not os.path.exists(file_path): raise exceptions.IRFileNotFoundException(file_path) try: loaded_file = configure.Configuration.from_file(file_path).configure() placeholders_list = cli.yamls.Placeholder.placeholders_list for placeholder in placeholders_list[::-1]: if placeholder.file_path is None: placeholder.file_path = file_path else: break except yaml.constructor.ConstructorError as e: raise exceptions.IRYAMLConstructorError(e, file_path) settings = settings.merge(loaded_file) return settings
def load_config_file(): """Load config file order(ENV, CWD, USER HOME, SYSTEM). :return ConfigParser: config object """ # create a parser with default path to InfraRed's main dir cwd_path = os.path.join(os.getcwd(), utils.IR_CONF_FILE) _config = ConfigParser.ConfigParser() env_path = os.getenv(utils.ENV_VAR_NAME, None) if env_path is not None: env_path = os.path.expanduser(env_path) if os.path.isdir(env_path): env_path = os.path.join(env_path, utils.IR_CONF_FILE) for path in (env_path, cwd_path, utils.USER_PATH, utils.SYSTEM_PATH): if path is not None and os.path.exists(path): _config.read(path) break else: LOG.warning("Configuration file not found, using InfraRed project dir") project_dir = os.path.dirname(os.path.dirname(__file__)) _config.add_section('defaults') for option, value in DEFAULT_CONF_DIRS.iteritems(): _config.set('defaults', option, os.path.join(project_dir, value)) # Validates settings dir exists settings_dir = _config.get('defaults', 'settings') if not os.path.exists(settings_dir): raise exceptions.IRFileNotFoundException( settings_dir, "Settings directory doesn't exist: ") return _config
def validate(self): """ Validates the configuration. """ # Validates at least one settings dir exists dirs = self.get_settings_dirs() if not any([os.path.exists(path) for path in dirs]): raise exceptions.IRFileNotFoundException( dirs, "Settings directories do not exist: ")
def load_yaml(filename, search_first): """Find YAML file. search default path first. :param filename: path to file :param search_first: default path to search first :returns: dict. loaded YAML file. """ filename = os.path.join(search_first, filename) if os.path.exists( os.path.join(search_first, filename)) else filename if os.path.exists(os.path.abspath(filename)): LOG.debug("Loading YAML file: %s" % os.path.abspath(filename)) path = os.path.abspath(filename) else: raise exceptions.IRFileNotFoundException( file_path=os.path.abspath(filename)) with open(path) as yaml_file: return yaml.load(yaml_file)
def normalize_file(file_path): """Return a normalized absolutized version of a file :param file_path: path to file to be normalized :return: normalized path of a file :raise: IRFileNotFoundException if the file doesn't exist """ if not os.path.isabs(file_path): abspath = os.path.abspath(file_path) LOG.debug( 'Setting the absolute path of "%s" to: "%s"' % (file_path, abspath) ) file_path = abspath if not os.path.exists(file_path): raise exceptions.IRFileNotFoundException(file_path) return file_path