Skip to content

Commit

Permalink
Fixed WorkspacePath support for python 3.11 (#121)
Browse files Browse the repository at this point in the history
This PR fixes some issues with the `WorkspacePath` implementation so
that it also works with 3.11. Due to internal changes between 3.10 and
3.11 fixes were needed for:

 - [X] `.expanduser()`
 - [x] `.glob()`

This will resolve #119.

Out of scope for this PR:

- Any other bugs not currently being surfaced by the existing unit and
integration tests.
 - Support for Python 3.12
  • Loading branch information
asnare authored Jul 8, 2024
1 parent f66d5b8 commit b274882
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ def __init__(self, object_info):
def __fspath__(self):
return self._object_info.path

def is_dir(self):
def is_dir(self, follow_symlinks=False): # pylint: disable=unused-argument
# follow_symlinks is for compatibility with Python 3.11
return self._object_info.object_type == ObjectType.DIRECTORY

def is_file(self):
def is_file(self, follow_symlinks=False): # pylint: disable=unused-argument
# follow_symlinks is for compatibility with Python 3.11
# TODO: check if we want to show notebooks as files
return self._object_info.object_type == ObjectType.FILE

Expand Down Expand Up @@ -173,10 +175,6 @@ class _DatabricksAccessor:
def __init__(self, ws: WorkspaceClient):
self._ws = ws

def expanduser(self, path):
home = f"/Users/{self._ws.current_user.me().user_name}"
return path.replace("~", home)

def __repr__(self):
return f"<{self.__class__.__name__} for {self._ws}>"

Expand Down Expand Up @@ -420,6 +418,26 @@ def is_file(self):
except DatabricksError:
return False

def _scandir(self):
# Python 3.10: Accesses _accessor.scandir() directly.
# Python 3.11: Instead invokes this (which normally dispatches to os.scandir())
return self._accessor.scandir(self)

def expanduser(self):
# Expand ~ (but NOT ~user) constructs.
if not (self._drv or self._root) and self._parts and self._parts[0][:1] == "~":
if self._parts[0] == "~":
user_name = self._ws.current_user.me().user_name
else:
other_user = self._parts[0][1:]
msg = f"Cannot determine home directory for: {other_user}"
raise RuntimeError(msg)
if user_name is None:
raise RuntimeError("Could not determine home directory.")
homedir = f"/Users/{user_name}"
return self._from_parts([homedir, *self._parts[1:]])
return self

def is_notebook(self):
"""Return True if the path points to a notebook in Databricks Workspace."""
try:
Expand Down

0 comments on commit b274882

Please sign in to comment.