Skip to content

Commit

Permalink
Revert to original .suffix implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
asnare committed Jul 10, 2024
1 parent b94ad64 commit fde8a7f
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class WorkspacePath(Path): # pylint: disable=too-many-public-methods
)
_cached_object_info: ObjectInfo

_SUFFIXES = {Language.PYTHON: ".py", Language.SQL: ".sql", Language.SCALA: ".scala", Language.R: ".R"}
_SUFFIXES = {".py": Language.PYTHON, ".sql": Language.SQL, ".scala": Language.SCALA, ".R": Language.R}

# Path semantics are posix-like.
parser = posixpath
Expand Down Expand Up @@ -460,19 +460,6 @@ def parts(self):
return self.drive + self.root, *self._path_parts
return self._path_parts

@property
def suffix(self):
# Super implementations are mostly fine...
suffix = super().suffix
# ...but if there is no suffix and this path is for a notebook then infer the extension based on the notebook
# language.
if not suffix and self.is_notebook():
try:
suffix = self._SUFFIXES.get(self._object_info.language, "")
except DatabricksError:
pass
return suffix

def with_name(self, name):
parser = self.parser
if not name or parser.sep in name or name == ".":
Expand Down Expand Up @@ -664,6 +651,22 @@ def open(self, mode="r", buffering=-1, encoding=None, errors=None, newline=None)
return _TextUploadIO(self._ws, self.as_posix())
raise ValueError(f"invalid mode: {mode}")

@property
def suffix(self):
"""Return the file extension. If the file is a notebook, return the suffix based on the language."""
suffix = super().suffix
if suffix:
return suffix
if not self.is_notebook():
return ""
for sfx, lang in self._SUFFIXES.items():
try:
if self._object_info.language == lang:
return sfx
except DatabricksError:
return ""
return ""

@property
def _object_info(self) -> ObjectInfo:
# this method is cached because it is used in multiple is_* methods.
Expand Down

0 comments on commit fde8a7f

Please sign in to comment.