How to use the pathvalidate.sanitize_filepath function in pathvalidate

To help you get started, we’ve selected a few pathvalidate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thombashi / pathvalidate / test / test_filepath.py View on Github external
def test_normal_auto_platform_linux(self, value, expected):
        if isinstance(expected, str):
            sanitized = sanitize_filepath(value, platform="auto")
            assert is_valid_filepath(sanitized, platform="auto")
        else:
            with pytest.raises(expected):
                sanitize_filepath(value, platform="auto")
github thombashi / pathvalidate / test / test_filepath.py View on Github external
def test_normal_check_reserved(self, value, check_reserved, expected):
        assert (
            sanitize_filepath(value, platform="windows", check_reserved=check_reserved) == expected
        )
github thombashi / pathvalidate / test / test_filepath.py View on Github external
def test_normal_auto_platform_linux(self, value, expected):
        if isinstance(expected, str):
            sanitized = sanitize_filepath(value, platform="auto")
            assert is_valid_filepath(sanitized, platform="auto")
        else:
            with pytest.raises(expected):
                sanitize_filepath(value, platform="auto")
github thombashi / pathvalidate / test / test_filepath.py View on Github external
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)
github thombashi / pathvalidate / test / test_filepath.py View on Github external
def test_normal_path_separator(self, platform, value, expected):
        sanitized = sanitize_filepath(value, platform=platform)
        assert sanitized == expected
        assert is_valid_filepath(sanitized, platform=platform)
github thombashi / pathvalidate / test / test_filepath.py View on Github external
def test_normal_reserved_name(self, value, test_platform, expected):
        filename = sanitize_filepath(value, platform=test_platform)
        assert filename == expected
        assert is_valid_filepath(filename, platform=test_platform)
github thombashi / pathvalidate / test / test_filepath.py View on Github external
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)
github RhetTbull / osxphotos / examples / export_by_album.py View on Github external
photosdb = osxphotos.PhotosDB(library_path)
    else:
        photosdb = osxphotos.PhotosDB()

    photos = photosdb.photos()

    for p in photos:
        if not p.ismissing:
            albums = p.albums
            if not albums:
                albums = [default_album]
            for album in albums:
                click.echo(f"exporting {p.original_filename} in album {album}")

                # make sure no invalid characters in destination path (could be in album name)
                album_name = sanitize_filepath(album, platform="auto")

                # create destination folder, if necessary, based on album name
                dest_dir = os.path.join(export_path, album_name)

                # verify path is a valid path
                if not is_valid_filepath(dest_dir, platform="auto"):
                    sys.exit(f"Invalid filepath {dest_dir}")

                # create destination dir if needed
                if not os.path.isdir(dest_dir):
                    os.makedirs(dest_dir)

                filename = p.original_filename
                # export the photo but only if --edited, photo has adjustments, and 
                # path_edited is not None (can be None if edited photo is missing)
                if edited and p.hasadjustments and p.path_edited:
github shadowmoose / RedditDownloader / redditdownloader / processing / wrappers / rel_file.py View on Github external
def __init__(self, base, file_path=None, full_file_path=None):
		super().__init__(base, file_path, full_file_path)
		self._path = self._path.replace('%', '%%')
		self._path = self.remove_dotslash(self._path)
		# noinspection PyUnresolvedReferences
		self._path = pathvalidate.sanitize_filepath(self._path, '_').strip(". /\\").strip()
		if not len(self._path):
			self._path = '_'
github RhetTbull / osxphotos / osxphotos / __main__.py View on Github external
elif directory:
        # got a directory template, render it and check results are valid
        dirnames, unmatched = photo.render_template(directory)
        if not dirnames:
            raise click.BadOptionUsage(
                "directory",
                f"Invalid template '{directory}': results={dirnames} unmatched={unmatched}",
            )
        elif unmatched:
            raise click.BadOptionUsage(
                "directory",
                f"Invalid template '{directory}': results={dirnames} unmatched={unmatched}",
            )
        dest_paths = []
        for dirname in dirnames:
            dirname = sanitize_filepath(dirname, platform="auto")
            dest_path = os.path.join(dest, dirname)
            if not is_valid_filepath(dest_path, platform="auto"):
                raise ValueError(f"Invalid file path: '{dest_path}'")
            if not dry_run and not os.path.isdir(dest_path):
                os.makedirs(dest_path)
            dest_paths.append(dest_path)
    else:
        dest_paths = [dest]
    return dest_paths