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

normalize databricks paths as part of resolving them #157

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,18 @@ def resolve(self: P, strict: bool = False) -> P:
if strict and not absolute.exists():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't self.absolute() fail because self.cwd() is not available for _DatabricksPath?

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will but that's not the problem being addressed by this PR

msg = f"Path does not exist: {self}"
raise FileNotFoundError(msg)
return absolute
return absolute.normalize()

def normalize(self: P) -> P:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick initial impression while I have a closer look at this: we should not be introducing new public methods like this, they're not part of the Path() API being emulated here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. this method is a good private

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree but we're already doing this: see is_notebook() ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for index, part in enumerate(self._path_parts):
# we can't normalize '/../stuff' so let's ignore such scenarios
if index == 0 or part != "..":
continue
segments = list(self._path_parts)
segments.pop(index)
segments.pop(index - 1)
return self.with_segments(self.anchor, *segments).normalize()
return self
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for index, part in enumerate(self._path_parts):
# we can't normalize '/../stuff' so let's ignore such scenarios
if index == 0 or part != "..":
continue
segments = list(self._path_parts)
segments.pop(index)
segments.pop(index - 1)
return self.with_segments(self.anchor, *segments).normalize()
return self
stack = []
for part in self._path_parts:
if part == "..":
if stack:
stack.pop()
elif part == "." or not part:
continue
else:
stack.append(part)
return self.with_segments(self.anchor, *stack)

can we perhaps get a non-recursive option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


def absolute(self: P) -> P:
if self.is_absolute():
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ def test_replace_file(ws, make_random, cls):
tmp_dir.rmdir(recursive=True)


@pytest.mark.parametrize("cls", DATABRICKS_PATHLIKE)
def test_resolve_is_consistent(ws, cls):
path = cls(ws, "/a/b/c") / Path("../../d")
asnare marked this conversation as resolved.
Show resolved Hide resolved
resolved = path.resolve()
assert resolved == cls(ws, "/a/d")
asnare marked this conversation as resolved.
Show resolved Hide resolved


def test_workspace_as_fuse(ws):
wsp = WorkspacePath(ws, "/Users/foo/bar/baz")
assert Path("/Workspace/Users/foo/bar/baz") == wsp.as_fuse()
Expand Down