async def test_normalize_local_path_uri__tilde_in_relative_path_2(pwd: Path,) -> None: url = URL("file:./~/path/to/file.txt") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == pwd / "~/path/to/file.txt" assert str(url) == (pwd / "~/path/to/file.txt").as_uri().replace("%7E", "~")
async def test_normalize_local_path_uri__3_slashes__double(pwd: Path) -> None: url = URL(f"file:///{pwd}/path/to/file.txt") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == pwd / "path/to/file.txt" assert str(url) == (pwd / "path/to/file.txt").as_uri()
async def _expand(paths: Sequence[str], root: Root, glob: bool, allow_file: bool = False) -> List[URL]: uris = [] for path in paths: uri = parse_blob_or_file_resource(path, root) if root.verbosity > 0: painter = get_painter(root.color, quote=True) curi = painter.paint(str(uri), FileStatusType.FILE) click.echo(f"Expand {curi}") uri_path = str(_extract_path(uri)) if glob and globmodule.has_magic(uri_path): if uri.scheme == "blob": bucket_name, key = root.client.blob_storage._extract_bucket_and_key( uri) if globmodule.has_magic(bucket_name): raise ValueError( "You can not glob on bucket names. Please provide name " "explicitly.") async for blob in root.client.blob_storage.glob_blobs( bucket_name=bucket_name, pattern=key): uris.append(blob.uri) elif allow_file and uri.scheme == "file": for p in globmodule.iglob(uri_path, recursive=True): uris.append(uri.with_path(p)) else: uris.append(uri) else: uris.append(uri) return uris
async def _is_dir(root: Root, uri: URL) -> bool: if uri.scheme == "blob": return await root.client.blob_storage._is_dir(uri) elif uri.scheme == "file": path = _extract_path(uri) return path.is_dir() return False
async def test_normalize_local_path_uri__tilde_in_absolute_path( fake_homedir: Path, pwd: Path ) -> None: url = URL("file:/~/path/to/file.txt") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == pwd / "/~/path/to/file.txt" assert str(url) == (pwd / "/~/path/to/file.txt").as_uri().replace("%7E", "~")
async def test_normalize_local_path_uri__tilde_slash__double( fake_homedir: Path ) -> None: url = URL("file:~/path/to/file.txt") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == fake_homedir / "path/to/file.txt" assert str(url) == (fake_homedir / "path/to/file.txt").as_uri()
async def _is_dir(root: Root, uri: URL) -> bool: if uri.scheme == "storage": try: stat = await root.client.storage.stat(uri) return stat.is_dir() except ResourceNotFound: pass elif uri.scheme == "file": path = _extract_path(uri) return path.is_dir() return False
async def _expand( paths: Sequence[str], root: Root, glob: bool, allow_file: bool = False ) -> List[URL]: uris = [] for path in paths: uri = parse_file_resource(path, root) if root.verbosity > 0: painter = get_painter(root.color, quote=True) curi = painter.paint(str(uri), FileStatusType.FILE) click.echo(f"Expand {curi}") uri_path = str(_extract_path(uri)) if glob and globmodule.has_magic(uri_path): if uri.scheme == "storage": async for file in root.client.storage.glob(uri): uris.append(file) elif allow_file and path.startswith("file:"): for p in globmodule.iglob(uri_path, recursive=True): uris.append(uri.with_path(p)) else: uris.append(uri) else: uris.append(uri) return uris
async def test_normalize_local_path_uri__no_slash__double(pwd: Path) -> None: url = URL("file:path/to/file.txt") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == pwd / "path/to/file.txt"
async def test_normalize_local_path_uri__3_slashes_relative(pwd: Path) -> None: url = URL("file:///path/to/file.txt") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == Path(pwd.drive + "/path/to/file.txt")
async def test_normalize_local_path_uri_no_path(pwd: Path) -> None: url = URL("file:") url = normalize_local_path_uri(url) assert url.scheme == "file" assert url.host is None assert _extract_path(url) == pwd
def format_url(url: URL) -> str: if url.scheme == "file": path = _extract_path(url) return str(path) else: return str(url)
def test_uri_from_cli_absolute_path_special_chars() -> None: uri = uri_from_cli("/path/to/file#%23:?@~", "testuser", "test-cluster") assert _extract_path(uri) == Path("/path/to/file#%23:?@~").absolute()