def dict2dir(dir, dic, mode="w"): dir = Path(dir) if not dir.exists(): dir.mkdir() for filename, content in dic.items(): p = Path(dir, filename) if isinstance(content, dict): dict2dir(p, content) continue f = open(p, mode) f.write(content) f.close()
def folder_source_clean_path(folder_source): if folder_source.path is not None: path = Path(folder_source.path) if not path.exists(): raise ValidationError({ "path": "The folder does not exist." }) elif path.islink(): raise ValidationError({ "path": "Symbolic links not allowed." }) elif not path.isdir(): raise ValidationError({ "path": "Not a folder." })
def file_source_clean_path(file_source): if file_source.path is not None: path = Path(file_source.path) if not path.exists(): raise ValidationError({ "path": "The file does not exist." }) elif path.islink(): raise ValidationError({ "path": "Symbolic links not allowed." }) elif not path.isfile(): raise ValidationError({ "path": "Not a file." }) elif not is_valid_file(path): raise ValidationError({ "path": "Not a valid file." })
def location_exists(location): path = Path(location) return path.exists()