Skip to content

Commit

Permalink
The implementation of .expanduser() is the same for both implementati…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
asnare committed Jul 16, 2024
1 parent 29913b0 commit 1156dae
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ def rename(self: P, target: str | bytes | os.PathLike) -> P: ...
@abstractmethod
def replace(self: P, target: str | bytes | os.PathLike) -> P: ...

@abstractmethod
def expanduser(self: P) -> P: ...

@abstractmethod
def iterdir(self: P) -> Generator[P, None, None]: ...

Expand Down Expand Up @@ -491,6 +488,21 @@ def home(self): # pylint: disable=arguments-differ
"""Return the user's home directory. Adapted from pathlib.Path"""
return type(self)(self._ws, "~").expanduser()

def expanduser(self: P) -> P:
# Expand ~ (but NOT ~user) constructs.
if not (self._drv or self._root) and self._path_parts and self._path_parts[0][:1] == "~":
if self._path_parts[0] == "~":
user_name = self._ws.current_user.me().user_name
else:
other_user = self._path_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.with_segments(homedir, *self._path_parts[1:])
return self

def _return_false(self) -> bool:
return False

Expand Down Expand Up @@ -687,21 +699,6 @@ def is_file(self) -> bool:
"""Return True if the path points to a file in Databricks Workspace."""
return not self.is_dir()

def expanduser(self: P) -> P:
# Expand ~ (but NOT ~user) constructs.
if not (self._drv or self._root) and self._path_parts and self._path_parts[0][:1] == "~":
if self._path_parts[0] == "~":
user_name = self._ws.current_user.me().user_name
else:
other_user = self._path_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.with_segments(homedir, *self._path_parts[1:])
return self

def iterdir(self) -> Generator[DBFSPath, None, None]:
for child in self._ws.dbfs.list(self.as_posix()):
yield self._from_file_info(self._ws, child)
Expand Down Expand Up @@ -847,21 +844,6 @@ def is_file(self) -> bool:
except DatabricksError:
return False

def expanduser(self: P) -> P:
# Expand ~ (but NOT ~user) constructs.
if not (self._drv or self._root) and self._path_parts and self._path_parts[0][:1] == "~":
if self._path_parts[0] == "~":
user_name = self._ws.current_user.me().user_name
else:
other_user = self._path_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.with_segments(homedir, *self._path_parts[1:])
return self

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

0 comments on commit 1156dae

Please sign in to comment.