def test_exception_reserved_name(self, value, platform, expected): with pytest.raises(expected) as e: validate_filename(value, platform=platform) assert e.value.reserved_name assert e.value.reusable_name is False assert not is_valid_filename(value, platform=platform)
def file_delete(filename: str): # Should allow to delete any file from DFS try: validate_filename(filename) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) r = requests.get( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": filename, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return storage_ip, storage_port = str(r.json()["hosts"][0]["host"]), str(r.json()["hosts"][0]["port"]) typer.echo(f"Deleting the file {filename} from the server {storage_ip}") req = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "file_delete", "name": filename, "cwd": CWD}, ) if req.status_code == 200: typer.echo(f"{filename} is deleted successfully!") else: typer.echo(f"Error {req.status_code} {req.text}") data_dump()
def test_normal_pathlike(self, value, replace_text, expected): sanitized_name = sanitize_filename(value, replace_text) assert sanitized_name == expected assert is_pathlike_obj(sanitized_name) validate_filename(sanitized_name) assert is_valid_filename(sanitized_name)
def checkIfValidFileName(self, name): from pathvalidate import ValidationError, validate_filename try: validate_filename(name) return (True) except ValidationError as e: return (False)
def filename_valid(self, text): error = '' # noinspection PyBroadException try: if text in forbidden_names: return ValidationError(description=f'Forbidden name {text}') validate_filename(text) except ValidationError as ve: if ve.description is not None: error = 'Could not change filename.\n\nReason: {}'.format( ve.description) elif len(ve.args): error = 'Could not change filename.\n\nReason: {}'.format( ve.args[0]) else: error = 'Could not change filename.\n\nReason: {}'.format( ve.reason) except Exception as ex: error = f'Unknown exception: {ex}' finally: if error: return False else: return True
def dump_structure(struct, path, filename, sort_keys=False): """ This function takes a python structure, dumps it to a json string and saves the result in a file or the specified directory. This function has an attribute 'path_dict'. Filename/Path validation is done using pathvalidate. This seems a better approach compared to the slugify function. Slugify will slug names even if corresponding filename is valid. :param struct: Python structure that need to be written to file. :param path: Path of the resulting file. If path does not exist it will be created. :param filename: Filename of the required file. :param sort_keys: If set then sort on Keys. Default False. :return: """ try: validate_filepath(path, platform='auto') except ValidationError as e: logging.critical(f"Invalid path {path}: {e}") return if not os.path.isdir(path): os.mkdir(path) struct_str = json.dumps(struct, ensure_ascii=False, indent=2, sort_keys=sort_keys) try: validate_filename(filename, platform='auto') except ValidationError: filename = sanitize_filename(filename) with open(os.path.join(path, filename), 'w', encoding='utf-8') as fh: fh.write(struct_str) return
def test_minmax_len(self, value, min_len, max_len, expected): if expected is None: validate_filename(value, min_len=min_len, max_len=max_len) assert is_valid_filename(value, min_len=min_len, max_len=max_len) else: with pytest.raises(expected): validate_filename(value, min_len=min_len, max_len=max_len)
def _checkNameSanity( self, name: Optional[str], parentFolder: dict, allow_rename: bool = False, ) -> str: if not name: raise RestException('Name cannot be empty.', code=400) try: pathvalidate.validate_filename(name, platform='Linux') except pathvalidate.ValidationError: raise RestException('Invalid file name: ' + name, code=400) q = {'parentId': parentFolder['_id'], 'name': name} if not allow_rename and Folder().findOne(q, fields=["_id"]): raise RestException('Name already exists: ' + name, code=409) n = 0 while Folder().findOne(q, fields=["_id"]): n += 1 q["name"] = f"{name} ({n})" if n > 100: break return q["name"]
async def save(self, ctx, *, name): player = self.lavalink.player_manager.get(ctx.guild.id) if not player.queue and not player.current: return await ctx.send('Очередь пустая') playlists = listdir(path.join('resources', 'playlists')) playlist_name = f'{ctx.author.id}_{name.lower()}' try: validate_filename(playlist_name) except ValidationError: return await ctx.send('Запрещенные символы в названии плейлиста') if playlist_name in playlists: return await ctx.send( f'Плейлист с таким названием уже существует\nДля удаления плейлиста используйте {ctx.prefix}delete <название>' ) if len(name) > 100: return await ctx.send('Слишком длинное название для плейлиста') local_queue = player.queue.copy() if player.queue else [] if player.current: local_queue.insert(0, player.current) with open(path.join('resources', 'playlists', playlist_name), 'wb+') as queue_file: pdump(local_queue, queue_file) ln = len(local_queue) return await ctx.send( f'Плейлист {name} [{ln} {sform(ln, "трек")}] сохранен')
def file_read(filename: str): # Should allow to read any file from DFS (download a file from the DFS to the Client side). try: validate_filename(filename) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) r = requests.get( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": filename, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return storage_ip, storage_port = str(r.json()["hosts"][0]["host"]), str(r.json()["hosts"][0]["port"]) req = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "file_read", "name": filename, "cwd": CWD}, ) if req.status_code != 200: typer.echo(f"Error {req.status_code} {req.text}") return typer.echo(f"Downloading the file {filename} from the server") with open(os.path.join(BASE_DIR, filename), "wb+") as fp: fp.write(req.content) typer.echo(f"File '{filename}' is downloaded") data_dump()
def file_info(filename: str): # Should provide information about the file (any useful information - size, node id, etc.) try: validate_filename(filename) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) r = requests.get( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": filename, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) if r.status_code == 404: typer.echo("File with this name doesn't exist") return info = r.json()["file_info"] table = BeautifulTable() print("keys are", info.keys()) table.columns.header = list(map(str, info.keys())) table.rows.append(map(str, info.values())) typer.echo(str(table)) data_dump()
def start_capture(): startButton['state'] = 'disabled' stopButton['state'] = 'normal' #ask for file name test_file_name = simpledialog.askstring( title="File Name", prompt="Enter the test file name (without .csv extension): ", initialvalue='test') if test_file_name is None: startButton['state'] = 'normal' stopButton['state'] = 'disabled' return try: validate_filename( test_file_name, platform=platform.system()) #validate the filename for current OS except ValidationError as e: tk.messagebox.showerror("Invalid Filename", "The entered filename is invalid:\n" + str(e)) startButton['state'] = 'normal' stopButton['state'] = 'disabled' return test_file_name += '.csv' global stop stop = 0 t1 = Thread(target=packet_capture, args=(test_file_name, )) t1.start() label['text'] = "Capturing packets..."
def file_copy(filename: str): # Should allow to create a copy of file. try: validate_filename(filename) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) r = requests.get( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": filename, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return storage_ip, storage_port = str(r.json()["hosts"][0]["host"]), str(r.json()["hosts"][0]["port"]) typer.echo("File replication is in process") r = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "file_copy", "name": filename, "cwd": CWD}, ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return typer.echo(f"File '{filename}' is successfully copied'") data_dump()
def file_create(filename: str): # Should allow to create a new empty file. try: validate_filename(filename) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) r = requests.post( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": filename, "size": 1, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return storage_ip, storage_port = str(r.json()["hosts"][0]["host"]), str(r.json()["hosts"][0]["port"]) typer.echo(f"Creating file '{filename}' on server '{storage_ip}'") req = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "file_create", "name": filename, "cwd": CWD}, ) if req.status_code == 201: typer.echo(f"File '{filename}' is successfilly created") else: typer.echo(f"Error {req.status_code} \n File with this name already exists") data_dump()
def validate_arguments(): """ One time utility used to group argument validation. """ cmd_args.template = Path(cmd_args.template) if cmd_args.source: cmd_args.source = Path(cmd_args.source) if cmd_args.output_dir: cmd_args.output_dir = Path(cmd_args.output_dir) cmd_args.output_dir.mkdir(parents=True, exist_ok=True) if cmd_args.target: for t in cmd_args.target: if not t.is_file() and not t.is_dir(): # Check if targets are valid files or directories raise NotADirectoryError("Target not found: \'%s\'" % t) if cmd_args.exclude: for x in cmd_args.exclude: if not x.is_file() and not x.is_dir(): # Check if exclusions are valid files or directories raise NotADirectoryError("Target not found: \'%s\'" % x) try: validate_filename(cmd_args.prefix + 'test.css') # Check if the prefix is valid for a filename except ValidationError as e: raise ValidationError("Prefix is not a valid filename: %s" % cmd_args.prefix, e)
def __validate_db_path(database_path): if dataproperty.is_empty_string(database_path): raise ValueError("null path") if database_path == MEMORY_DB_NAME: return pathvalidate.validate_filename(os.path.basename(database_path))
def test_min_len(self, value, min_len, expected): if expected is None: validate_filename(value, min_len=min_len) assert is_valid_filename(value, min_len=min_len) else: with pytest.raises(ValidationError) as e: validate_filename(value, min_len=min_len) assert e.value.reason == expected
def test_locale_ja(self, locale): from faker import Factory fake = Factory.create(locale=locale, seed=1) for _ in range(100): filename = fake.file_name() validate_filename(filename) assert is_valid_filename(filename)
def test_exception_escape_err_msg(self, value, platform, expected): with pytest.raises(expected) as e: print(platform, repr(value)) validate_filename(value, platform=platform) assert str(e.value) == ( r"invalid char found: invalids=('\r'), value='asdf\rsdf', " "reason=INVALID_CHARACTER, target-platform=Windows" ) # noqa
def test_max_len(self, value, platform, max_len, expected): if expected is None: validate_filename(value, platform=platform, max_len=max_len) assert is_valid_filename(value, platform=platform, max_len=max_len) return with pytest.raises(ValidationError) as e: validate_filename(value, platform=platform, max_len=max_len) assert e.value.reason == expected
def validate_upload_file_name(file_name): return_value = 'valid' try: validate_filename(file_name) except ValidationError as error: return_value = error.description print(error.description) print(file_name) return return_value
def test_exception_reserved_name(self, value, platform, expected): if expected is None: validate_filename(value, platform=platform) else: with pytest.raises(expected) as e: validate_filename(value, platform=platform) assert e.value.reason == ErrorReason.RESERVED_NAME assert e.value.reserved_name assert e.value.reusable_name is False assert not is_valid_filename(value, platform=platform)
def __validate_db_path(database_path): if typepy.is_null_string(database_path): raise ValueError("null path") if database_path == MEMORY_DB_NAME: return try: pathvalidate.validate_filename(os.path.basename(database_path)) except AttributeError: raise TypeError("database path must be a string: actual={}".format( type(database_path)))
def parse_edited_text( text: EditedText, inodes_paths: InodesPaths, platform: Optional[str] = None, ) -> List[Clause]: """ Parse the text in which the user has modified some filenames to specify a renaming. Args: text: the text with the desired renamings. inodes_paths: dict associating each inode to its path. platform: OS on which the renamings will be performed. Without a value, `path_validate` will detect the platform automatically. Raises: UnknownInodeError: the edited text contains an inode absent from the source text. TabulationError: a new name contains a tabulation. Although such a character is valid on most platforms, in the edited text it probably results from a typo. pathvalidate.ValidationError: a new name includes invalid character(s) for a filename (depends on the target platform). Returns: A list of clauses (path, new_name). """ result = [] for line in text.split("\n"): head, *tail = line.split("\t", maxsplit=1) if not tail: continue if not head.isdigit(): continue inode = Inode(int(head)) path = inodes_paths.get(inode) if path is None: raise UnknownInodeError(f"Unknown inode {inode}.") new_name = tail[0] if path.name == new_name: continue if new_name == "": raise EmptyNameError("A new name cannot be empty.") if "\t" in new_name: raise TabulationError( f"The new name '{new_name}' contains a tabulation.") try: pathvalidate.validate_filename(new_name, platform=platform or "auto") except pathvalidate.ValidationError: raise ValidationError(f"The new name '{new_name}' is invalid.") result.append(Clause(path, Name(new_name))) return result
def __validate_db_path(database_path): if typepy.is_null_string(database_path): raise ValueError("null path") if database_path == MEMORY_DB_NAME: return try: pathvalidate.validate_filename(os.path.basename(database_path)) except AttributeError: raise TypeError( "database path must be a string: actual={}".format( type(database_path)))
async def delete(self, ctx, *, name): playlists = listdir(path.join('resources', 'playlists')) playlist_name = f'{ctx.author.id}_{name.lower()}' try: validate_filename(playlist_name) except ValidationError: return await ctx.send('Запрещенные символы в названии плейлиста') if playlist_name not in playlists: return await ctx.send( f'Нет плейлиста с таким названием\nДля просмотра своих плейлистов используйте {ctx.prefix}playlists' ) remove(path.join('resources', 'playlists', playlist_name)) return await ctx.send(f'Плейлист {name} удален!')
def __generateValidFilename(self, filename: str): """ The filenames YoutubeDL gives to the downloaded files is based on the video's title. If the video's title contains special characters then the file won't be created successfuly. """ try: validate_filename(f'{filename}') return filename except Exception as e: return sanitize_filename(f'{filename}')
def __init__(self, config_name: str, config_items: Sequence[ConfigItem]) -> None: try: import pathvalidate pathvalidate.validate_filename(config_name) except ImportError: pass self.__logger = logger self.__config_filepath = os.path.normpath( os.path.expanduser( os.path.join("~", ".{:s}".format(config_name.lstrip("."))))) self.__config_items = config_items
def handler(request): ''' We're in a closure, since we want to configure the directory when we set up the path. ''' # Extract the filename from the request filename = request.match_info['filename'] # Raise an exception if we get anything nasty pathvalidate.validate_filename(filename) # Check that the file exists full_pathname = os.path.join(basepath, filename) if not os.path.exists(full_pathname): raise aiohttp.web.HTTPNotFound() # And serve pack the file return aiohttp.web.FileResponse(full_pathname)
def validate_document(self, document): try: validate_filename(document.name) except ValidationError: raise serializers.ValidationError("Invalid filename.") document_data = document.file.read() mime_type = magic.from_buffer(document_data, mime=True) if not is_mime_type_supported(mime_type): raise serializers.ValidationError( "This file type is not supported.") return document.name, document_data
def validate_filename(ctx, param, value): p = PurePosixPath(value) file_name = str(p.name) file_path = str(p.parent) try: pathvalidate.validate_filepath(file_path, platform='Linux') pathvalidate.validate_filename(file_name, platform='Linux') except pathvalidate.ValidationError as e: click.secho("[!] Invalid filename provided: {}".format(value), bg='red') ctx.abort() return value
def merge(self): if len(self.files) < 2: self.show_message("Выберите не менее 2 файлов") return if not self.outdir: self.show_message("Выберите выходную папку") return result_name_prefix_text = self.result_name_prefix.text() try: validate_filename(result_name_prefix_text) except ValidationError as e: self.show_message("Выберите корректное имя файла") return joiner = SGFJoiner() joiner.join_files(self.files) result = joiner.serialise() def get_result_filename(size): if len(result.items()) == 1: return "{}.sgf".format(result_name_prefix_text) return "{}_{}x{}.sgf".format(result_name_prefix_text, size, size) # check if sgf with selected name already exists for s, c in result.items(): result_name = get_result_filename(s) if os.path.exists(os.path.join(self.outdir, result_name)): text = "Файл {} уже существует. Хотите его перезаписать?".format( result_name) dlg = QMessageBox(QMessageBox.Warning, "Файл уже существует", text, QMessageBox.Yes | QMessageBox.No) dlg.button(QMessageBox.Yes).setText("Да") dlg.button( QMessageBox.No).setText("Нет, выбрать другое имя файла") ret = dlg.exec() if ret != QMessageBox.Yes: self.show_message("Выберите уникальное имя файла") return for s, c in result.items(): result_name = get_result_filename(s) with open(os.path.join(self.outdir, result_name), "wb") as fh: fh.write(c) self.show_message("Готово. Записано {} SGF.".format(len(result)))
def test_normal_multibyte(self, value, replace_text, expected): sanitized_name = sanitize_filename(value, replace_text) assert sanitized_name == expected validate_filename(sanitized_name) assert is_valid_filename(sanitized_name)
def test_normal_multibyte(self, value, platform): validate_filename(value, platform) assert is_valid_filename(value, platform=platform)
def test_exception_win_invalid_char(self, value, platform): with pytest.raises(InvalidCharError): validate_filename(value, platform=platform) assert not is_valid_filename(value, platform=platform)
def test_exception(self, value, expected): with pytest.raises(expected): validate_filename(value) assert not is_valid_filename(value)
def test_normal_str(self, platform, value, replace_text, expected): sanitized_name = sanitize_filename(value, platform=platform, replacement_text=replace_text) assert sanitized_name == expected assert isinstance(sanitized_name, six.text_type) validate_filename(sanitized_name, platform=platform) assert is_valid_filename(sanitized_name, platform=platform)