Beispiel #1
0
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()
Beispiel #2
0
    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
Beispiel #3
0
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")
Beispiel #4
0
    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)
Beispiel #6
0
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_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)
Beispiel #8
0
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()
Beispiel #9
0
 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)
Beispiel #10
0
 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")
Beispiel #11
0
 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)
Beispiel #12
0
 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)
Beispiel #13
0
 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)
Beispiel #14
0
 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)
Beispiel #15
0
    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
Beispiel #16
0
    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)
Beispiel #17
0
    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)
Beispiel #18
0
    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)
Beispiel #19
0
    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")
Beispiel #20
0
    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)
Beispiel #21
0
    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")
Beispiel #22
0
    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
Beispiel #23
0
    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")
Beispiel #24
0
    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
Beispiel #25
0
 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
Beispiel #26
0
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
Beispiel #27
0
    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
Beispiel #28
0
    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)
Beispiel #29
0
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()
Beispiel #30
0
 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)
Beispiel #31
0
 def test_normal(self, value):
     validate_filepath(value)
     assert is_valid_filepath(value)
Beispiel #32
0
 def test_exception(self, value, expected):
     with pytest.raises(expected):
         validate_filepath(value)
     assert not is_valid_filepath(value)
Beispiel #33
0
 def test_normal(self, value):
     validate_filepath(value, platform="windows")
     assert is_valid_filepath(value, platform="windows")
Beispiel #34
0
 def test_normal_reserved_name_used_valid_place(self, value, platform):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
Beispiel #35
0
 def test_normal_space_or_period_at_tail(self, platform, value):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
Beispiel #36
0
 def test_normal_win(self, value):
     platform = "windows"
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
Beispiel #37
0
 def test_normal_multibyte(self, value, platform):
     validate_filepath(value, platform)
     assert is_valid_filepath(value, platform=platform)
Beispiel #38
0
 def test_exception(self, value, expected):
     with pytest.raises(expected):
         validate_filepath(value)
     assert not is_valid_filepath(value)
Beispiel #39
0
 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)
Beispiel #40
0
 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)
Beispiel #41
0
                        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:
Beispiel #42
0
 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)
Beispiel #43
0
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()
Beispiel #44
0
 def test_normal_reserved_name(self, value, platform, expected):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)