def make_directory(directory_name: str, path: Optional[str] = None): # Should allow to create a new directory. if not path: path = CWD else: try: validate_filepath(path) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) return r = requests.post(url="http://" + IP + ":" + PORT + "/api/hosts/", headers={"Server-Hash": "suchsecret"}) storage_server = random.randint(0, len(r.json()["hosts"]) - 1) storage_ip, storage_port = str(r.json()["hosts"][storage_server]["host"]), str( r.json()["hosts"][storage_server]["port"] ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return r = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "dir_make", "cwd": path, "name": directory_name}, ) if r.status_code != 201: typer.echo(f"Error {r.status_code} {r.text}") return typer.echo(f"Created directory {path}{directory_name}/") data_dump()
def on_text_changed(self, text: str) -> None: """ display an icon showing whether the entered file name is acceptable """ self.text.setToolTip(text) if not text: self.status.clear() self.path = None return path: Path = Path(text).resolve() if not path.is_dir(): self.status.setPixmap(self.style().standardIcon( QStyle.SP_MessageBoxCritical).pixmap(self.text.height())) self.path = None return if not path.exists(): self.status.setPixmap(self.style().standardIcon( QStyle.SP_MessageBoxWarning).pixmap(self.text.height())) self.path = path return try: pathvalidate.validate_filepath(path, platform='auto') except pathvalidate.error.ValidationError as ex: print(ex.description) self.status.setPixmap(self.style().standardIcon( QStyle.SP_MessageBoxCritical).pixmap(self.text.height())) self.path = None else: self.status.clear() self.path = path
def open_directory(name: str): # Should allow to change directory if name != "..": try: validate_filepath(name) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) global CWD r = requests.get( url="http://" + IP + ":" + PORT + "/api/directory/", params={"name": name, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) if r.status_code == 200: real_cwd = pathlib.Path(CWD) cwd_by_name = { "..": real_cwd.parent, ".": real_cwd, } CWD = str(cwd_by_name.get(name, real_cwd / name)) if CWD[-1] != "/": CWD += "/" data_dump(CWD) typer.echo(f"Current working directory is {CWD}") else: typer.echo(f"Error {r.status_code} \nSuch directory doesn't exist")
def test_normal_pathlike(self, value, replace_text, expected): sanitized_name = sanitize_filepath(value, replace_text) assert sanitized_name == expected assert is_pathlike_obj(sanitized_name) validate_filepath(sanitized_name) assert is_valid_filepath(sanitized_name)
def test_normal_max_len(self, value, platform, max_len, expected): if expected is None: validate_filepath(value, platform=platform, max_len=max_len) assert is_valid_filepath(value, platform=platform, max_len=max_len) else: with pytest.raises(expected): validate_filepath(value, platform=platform, max_len=max_len)
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 file_write(path_to_the_file: str): # Should allow to put any file to DFS (upload a file from the Client side to the DFS) try: validate_filepath(path_to_the_file) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) size = os.path.getsize(SCRIPT_DIR / path_to_the_file) # in bytes filename = SCRIPT_DIR / path_to_the_file r = requests.post( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": pathlib.Path(filename).name, "size": size, "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"Uploading the file {filename} to the server {storage_ip}") url = "http://" + storage_ip + ":" + storage_port + "/api/dfs/" files = {"file": open(filename, "rb")} params = {"command": "file_write", "name": filename, "cwd": CWD} req = requests.post(url=url, files=files, params=params) if req.status_code != 200: typer.echo(f"Error {req.status_code} {req.text}") return typer.echo(f"{filename} is uploaded successfully!") data_dump()
def test_normal_rel_path(self, test_platform, value, expected): if expected is None: validate_filepath(value, platform=test_platform) assert is_valid_filepath(value, platform=test_platform) else: with pytest.raises(expected): validate_filepath(value, platform=test_platform)
def test_normal_auto_platform_linux(self, value, expected): if expected is None: validate_filepath(value, platform="auto") assert is_valid_filepath(value, platform="auto") else: with pytest.raises(expected): validate_filepath(value, platform="auto")
def test_minmax_len(self, value, min_len, max_len, expected): if expected is None: validate_filepath(value, min_len=min_len, max_len=max_len) assert is_valid_filepath(value, min_len=min_len, max_len=max_len) else: with pytest.raises(expected): validate_filepath(value, min_len=min_len, max_len=max_len)
def test_normal_multibyte(self, test_platform, value, replace_text, expected): sanitized_name = sanitize_filepath(value, replace_text, platform=test_platform) assert sanitized_name == expected validate_filepath(sanitized_name, platform=test_platform) assert is_valid_filepath(sanitized_name, platform=test_platform)
def test_normal_str(self, platform, value, replace_text, expected): sanitized_name = sanitize_filepath(value, platform=platform, replacement_text=replace_text) assert sanitized_name == expected assert isinstance(sanitized_name, str) validate_filepath(sanitized_name, platform=platform) assert is_valid_filepath(sanitized_name, platform=platform)
def test_exception_escape_err_msg(self, value, platform, expected): with pytest.raises(expected) as e: print(platform, repr(value)) validate_filepath(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_exception_reserved_name(self, value, platform, expected): with pytest.raises(expected) as e: print(platform, value) validate_filepath(value, platform=platform) assert e.value.reusable_name is False assert e.value.reserved_name assert not is_valid_filepath(value, platform=platform)
def test_normal_max_len(self, value, platform, max_len, expected): if expected is None: validate_filepath(value, platform=platform, max_len=max_len) assert is_valid_filepath(value, platform=platform, max_len=max_len) return with pytest.raises(expected): validate_filepath(value, platform=platform, max_len=max_len)
def test_locale_jp(self, locale): from faker import Factory fake = Factory.create(locale=locale, seed=1) for _ in range(100): filepath = fake.file_path() validate_filepath(filepath, platform="linux") assert is_valid_filepath(filepath, platform="linux")
def test_locale_jp(self, locale): from faker import Factory fake = Factory.create(locale=locale, seed=1) for _ in range(100): filepath = fake.file_path() validate_filepath(filepath) assert is_valid_filepath(filepath)
def validate(self): try: pv.validate_filepath(self.source, platform="auto") except pv.ValidationError as e: raise InvalidFilePathError(e) if os.path.isfile(self.source) or is_fifo(self.source): return raise OSError("file not found")
def validate(filepath: str) -> bool: """ Performs a check if the string provided is file or not. """ try: validate_filepath(filepath) except ValidationError: return False return True
def validate(self): try: pv.validate_filepath(self.source) except pv.NullNameError: raise InvalidFilePathError("file path is empty") except (ValueError, pv.InvalidCharError, pv.InvalidLengthError) as e: raise InvalidFilePathError(e) if os.path.isfile(self.source) or is_fifo(self.source): return raise IOError("file not found")
def _output_path_valid(self, output_path): try: pathvalidate.validate_filepath(output_path) output_path_valid = True except: output_path_valid = False msg.showerror(self.gui.window_title, 'Output path is not a valid path.', parent=self.gui) return output_path_valid
def validate_data(self): """Validate input.""" if (self.algo_type not in ALGO_LIST): self.algo_type = 'copy' try: validate_filepath(self.input_path) except ValidationError as ve: print('Import file path' + ve) raise ve try: validate_filepath(self.output_path) except ValidationError as ve: print('Output file path' + ve) raise ve
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 val_path(cls, path_string): """ Get and parse a path string Arguments --------- path_string: Path string representation of path name Returns ------- Validated path string """ try: pv.validate_filepath(path_string, platform='auto') str_path = pv.sanitize_filepath(path_string, platform='auto') return str_path except ValueError as err: logger.error('Invalid filepath provided') return False
def write(self, file: Path): """ Writes CssSorted to file. Asks for permission to overwrite --force flag is not set :param file: file to write in :return: filepath of created file """ try: validate_filepath(file, platform='auto') with file.open('x') as sorted_file: sorted_file.writelines(self.raw) except FileExistsError: confirm = None while confirm not in _AFFIRMATIVE_ and confirm not in _NEGATIVE_: if not cmd_args.force: confirm = input("Are you sure you want to overwrite \'%s\' with its sorted version? [Y/N]: " % file.name).upper() else: # Force overwrite files if --force flag is set confirm = True if confirm in _AFFIRMATIVE_: with file.open('w') as sorted_file: sorted_file.writelines(self.raw) elif confirm in _NEGATIVE_: action = input("Specify a [new filename] or [abort]: ") if action == 'abort' or action == '\n': return None else: file = self.write(Path(action)) else: print("The available choices are %s or %s (not case sensitive)" % (_AFFIRMATIVE_, _NEGATIVE_)) return file except ValidationError as e: print("Not a valid file path: \'%s\'" % file, e)
def file_move(filename: str, destination_path: str): # Should allow to move a file to the specified path. try: validate_filename(filename) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) try: validate_filepath(destination_path) 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"File transfer to '{destination_path}' is in process") r = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "file_move", "name": filename, "cwd": CWD, "path": destination_path}, ) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return typer.echo(f"File '{filename}' is successfully transfered'") data_dump()
def test_normal_str(self, platform, value, replace_text, expected): sanitized_name = sanitize_filepath(value, platform=platform, replacement_text=replace_text) assert sanitized_name == expected assert isinstance(sanitized_name, six.text_type) validate_filepath(sanitized_name, platform=platform) assert is_valid_filepath(sanitized_name, platform=platform)
def test_normal(self, value): validate_filepath(value) assert is_valid_filepath(value)
def test_exception(self, value, expected): with pytest.raises(expected): validate_filepath(value) assert not is_valid_filepath(value)
def test_normal(self, value): validate_filepath(value, platform="windows") assert is_valid_filepath(value, platform="windows")
def test_normal_reserved_name_used_valid_place(self, value, platform): validate_filepath(value, platform=platform) assert is_valid_filepath(value, platform=platform)
def test_normal_space_or_period_at_tail(self, platform, value): validate_filepath(value, platform=platform) assert is_valid_filepath(value, platform=platform)
def test_normal_win(self, value): platform = "windows" validate_filepath(value, platform=platform) assert is_valid_filepath(value, platform=platform)
def test_normal_multibyte(self, value, platform): validate_filepath(value, platform) assert is_valid_filepath(value, platform=platform)
def test_normal_multibyte(self, value, replace_text, expected): sanitized_name = sanitize_filepath(value, replace_text) assert sanitized_name == expected validate_filepath(sanitized_name) assert is_valid_filepath(sanitized_name)
def test_exception(self, value, expected): with pytest.raises(ValidationError) as e: validate_filepath(value) assert e.value.reason == ErrorReason.MALFORMED_ABS_PATH assert not is_valid_filepath(value)
help='download screenshots, game clips, or both') args = parser.parse_args() # Other core variables. history_fname = 'history.json' history = {'note' : 'These IDs have been downloaded previously', \ 'screens': [], 'clips': []} downloads = {'screens': [], 'clips': []} # Load X API key, Xbox Profile User ID (xuid), and media directory. with open(args.config) as f_in: config = json.load(f_in) xapi_key = config['xapi_key'] xuid = str(make_xapi_call(xapi_key, '/v2/accountxuid')[0]['xuid']) try: validate_filepath(config['media_dir'], platform="auto") media_dir = config['media_dir'] except ValidationError as e: tqdm.write("ERROR: media_dir path is invalid\n{}".format(e)) sys.exit() # Load download history. if osp.exists(history_fname): with open(history_fname) as f_in: history = json.load(f_in) # Scan for new screenshots. if args.media_type in ['both', 'screenshots']: tqdm.write('Scanning for new screenshots...') cont_token = '' while True:
def test_exception_invalid_win_char(self, value, platform): with pytest.raises(InvalidCharError): validate_filepath(value, platform=platform) assert not is_valid_filepath(value, platform=platform)
def delete_directory(directory_name: str): path = None not_allowed_names = ["..", "."] if directory_name in not_allowed_names: typer.echo("I can't delete this directory") return # Should allow to delete directory. If the directory contains files the system should ask for confirmation from the user before deletion. if not path: path = CWD else: try: validate_filepath(path) except ValidationError as e: typer.echo(f"{e}\n", file=sys.stderr) return r = requests.get( url="http://" + IP + ":" + PORT + "/api/directory/", params={"name": directory_name, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) # typer.echo(r.text) respon_json = json.loads(r.text).get("files", []) if len(respon_json) != 0: files = [] dirs = [] for item in respon_json: name = item.get("name") size = item.get("size") if size is None: dirs.append(name) else: files.append(name) typer.echo( "This directory is not empty \n\nFiles:\n\n{}\nDirectories:\n\n{}\n".format( "\n".join(files), "\n".join(dirs) ) ) delete = typer.confirm("Are you sure you want to delete it?") if not delete: typer.echo("Not deleting") raise typer.Abort() typer.echo("Deleting it!") r = requests.get( url="http://" + IP + ":" + PORT + "/api/file/", params={"name": directory_name, "cwd": CWD}, headers={"Server-Hash": "suchsecret"}, ) server = random.choice(r.json()["hosts"]) storage_ip, storage_port = server["host"], str(server["port"]) if r.status_code != 200: typer.echo(f"Error {r.status_code} {r.text}") return r = requests.get( url="http://" + storage_ip + ":" + storage_port + "/api/dfs/", params={"command": "dir_delete", "cwd": f"{path}{directory_name}"}, ) if r.status_code == 200: typer.echo("Directory is succesfully deleted") elif r.status_code == 400: typer.echo("Directory with such name doesn't exist") data_dump()
def test_normal_reserved_name(self, value, platform, expected): validate_filepath(value, platform=platform) assert is_valid_filepath(value, platform=platform)