Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .as_uri() and .absolute() implementations for WorkspacePath #127

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,14 @@ def __str__(self):
self._str = (self._root + self.parser.sep.join(self._path_parts)) or "."
return self._str

def __bytes__(self):
return str(self).encode("utf-8")

def __repr__(self):
return f"{self.__class__.__name__}({str(self)!r})"

def as_uri(self) -> str:
return self._ws.config.host + "#workspace" + urlquote_from_bytes(bytes(self))
return f"{self._ws.config.host}#workspace{urlquote_from_bytes(bytes(self))}"

def __eq__(self, other):
if not isinstance(other, type(self)):
Expand Down Expand Up @@ -552,6 +555,11 @@ def resolve(self, strict=False):
"""Return the absolute path of the file or directory in Databricks Workspace."""
return self

def absolute(self):
if self.is_absolute():
return self
return self.with_segments(self.cwd(), self)

def is_dir(self):
"""Return True if the path points to a directory in Databricks Workspace."""
try:
Expand Down Expand Up @@ -609,12 +617,16 @@ def _prepare_pattern(self, pattern) -> Sequence[str]:

def glob(self, pattern, *, case_sensitive=None):
pattern_parts = self._prepare_pattern(pattern)
selector = _Selector.parse(pattern_parts, case_sensitive=case_sensitive if case_sensitive is not None else True)
if case_sensitive is None:
case_sensitive = True
selector = _Selector.parse(pattern_parts, case_sensitive=case_sensitive)
yield from selector(self)

def rglob(self, pattern, *, case_sensitive=None):
pattern_parts = ("**", *self._prepare_pattern(pattern))
selector = _Selector.parse(pattern_parts, case_sensitive=case_sensitive if case_sensitive is not None else True)
if case_sensitive is None:
case_sensitive = True
selector = _Selector.parse(pattern_parts, case_sensitive=case_sensitive)
yield from selector(self)


Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ def test_with_suffix() -> None:
_ = WorkspacePath(ws, "/").with_suffix(".txt")


def test_as_uri() -> None:
"""Verify that the URI that corresponds to a path can be generated."""
ws = create_autospec(WorkspaceClient)
ws.config.host = "https://example.com/instance"

ws_path = "/tmp/file with spaces.md"
expected_url = "https://example.com/instance#workspace/tmp/file%20with%20spaces.md"

assert WorkspacePath(ws, ws_path).as_uri() == expected_url


@pytest.mark.parametrize(
("path", "parent"),
[
Expand Down Expand Up @@ -653,6 +664,17 @@ def test_home_directory():
assert str(result) == "/Users/test_user"


def test_absolute() -> None:
"""This is only supported for absolute paths.

Otherwise it depends on the current working directory which isn't supported."""
ws = create_autospec(WorkspaceClient)

assert WorkspacePath(ws, "/absolute/path").absolute() == WorkspacePath(ws, "/absolute/path")
with pytest.raises(NotImplementedError):
_ = WorkspacePath(ws, "relative/path").absolute()


def test_is_dir_when_object_type_is_directory():
ws = create_autospec(WorkspaceClient)
workspace_path = WorkspacePath(ws, "/test/path")
Expand Down