def load(settings_file, update_placeholders=True): """ Loads and returns the content of a given YAML file as a dictionary :param settings_file: String represents a path to a YAML file :param update_placeholders: Whether to update Placeholder list or not in case of Placeholder tag is in the given YAML file :return: Dictionary containing the content the given YAML file """ LOG.debug("Loading setting file: %s" % settings_file) if not os.path.exists(settings_file): raise exceptions.IRFileNotFoundException(settings_file) try: with open(settings_file) as f_obj: loaded_yml = yaml.load(f_obj) # Handling case of empty file if loaded_yml is None: raise exceptions.IREmptySettingsFile(settings_file) if update_placeholders: for placeholder in Placeholder.placeholders_list[::-1]: if placeholder.file_path is None: placeholder.file_path = settings_file else: break return loaded_yml except yaml.constructor.ConstructorError as e: raise exceptions.IRYAMLConstructorError(e, settings_file)
def resolve(self, value): pathes = self.get_check_locations(value) # check also for files with yml extension if self.SEARCH_YAML: pathes.extend([ path + '.yml' for path in pathes if not path.endswith('.yml') ]) target_file = next( (file_path for file_path in pathes if self.__class__.validate(file_path)), None) if target_file is None: raise exceptions.IRFileNotFoundException(pathes) return os.path.abspath(target_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 """ file_path = os.path.expanduser(file_path) 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
def copy_file(self, file_path, additional_lookup_dirs=None): """Copies specified file to the workspace folder.""" dirs = [os.path.curdir] if additional_lookup_dirs is not None: dirs.extend(additional_lookup_dirs) target_path = None for additional_dir in dirs: abs_path = os.path.join(additional_dir, file_path) if os.path.isfile(abs_path): target_path = os.path.join( self.path, os.path.basename(os.path.normpath(abs_path))) if not (os.path.exists(target_path) and os.path.samefile(abs_path, target_path)): LOG.debug("Copying file: src='%s', dest='%s'", abs_path, self.path) shutil.copy2(abs_path, self.path) break if target_path is None: raise exceptions.IRFileNotFoundException(file_path) return target_path
def load_yaml(filename, *search_paths): """Find YAML file. search default path first. :param filename: path to file :param search_paths: the list of paths to search for a file. :returns: dict. loaded YAML file. """ path = None searched_files = [] files_to_search = map( lambda search_path: os.path.join(search_path, filename), search_paths) for filename in files_to_search: searched_files.append(os.path.abspath(filename)) if os.path.exists(filename): path = os.path.abspath(filename) break if path is not None: LOG.debug("Loading YAML file: %s" % path) return yamls.load(path) else: raise exceptions.IRFileNotFoundException(file_path=searched_files)
def validate(self, value): """Validate if value is allowed""" allowed_values = self.get_allowed_values() if value not in allowed_values: raise exceptions.IRFileNotFoundException(self.files_path)