22from io import BytesIO
33from pathlib import Path , PurePath
44
5- from databricks .sdk import WorkspaceClient
6- from databricks .sdk .errors import NotFound
75from fsspec import AbstractFileSystem
86
97from pins .errors import PinsError
@@ -13,18 +11,18 @@ class DatabricksFs(AbstractFileSystem):
1311 protocol = "dbc"
1412
1513 def ls (self , path , detail = False , ** kwargs ):
16- return self ._databricks_ls (self , path , detail )
14+ return self ._databricks_ls (path , detail )
1715
1816 def exists (self , path : str , ** kwargs ):
19- return self ._databricks_exists (self , path )
17+ return self ._databricks_exists (path )
2018
2119 def open (self , path : str , mode : str = "rb" , * args , ** kwargs ):
2220 if mode != "rb" :
2321 raise NotImplementedError
24- return self ._databricks_open (self , path )
22+ return self ._databricks_open (path )
2523
2624 def get (self , rpath , lpath , recursive = False , ** kwargs ):
27- self ._databricks_get (self , rpath , lpath , recursive , ** kwargs )
25+ self ._databricks_get (rpath , lpath , recursive , ** kwargs )
2826
2927 def mkdir (self , path , create_parents = True , ** kwargs ):
3028 if not create_parents :
@@ -50,11 +48,18 @@ def rm(self, path, recursive=True, maxdepth=None) -> None:
5048 raise NotImplementedError
5149 if maxdepth is not None :
5250 raise NotImplementedError
53- if self ._databricks_exists (self , path ):
54- self ._databricks_rm_dir (self , path )
51+ if self ._databricks_exists (path ):
52+ self ._databricks_rm_dir (path )
5553
5654 @staticmethod
5755 def _databricks_put (lpath , rpath ):
56+ try :
57+ from databricks .sdk import WorkspaceClient
58+ except ModuleNotFoundError :
59+ raise PinsError (
60+ "Install the `databricks-sdk` package for Databricks board support."
61+ )
62+
5863 w = WorkspaceClient ()
5964 path = Path (lpath ).absolute ()
6065 orig_path = path
@@ -74,15 +79,21 @@ def _upload_files(path):
7479
7580 _upload_files (path )
7681
77- @staticmethod
7882 def _databricks_get (self , board , rpath , lpath , recursive = False , ** kwargs ):
83+ try :
84+ from databricks .sdk import WorkspaceClient
85+ except ModuleNotFoundError :
86+ raise PinsError (
87+ "Install the `databricks-sdk` package for Databricks board support."
88+ )
89+
7990 w = WorkspaceClient ()
8091 file_type = self ._databricks_is_type (rpath )
8192 if file_type == "file" :
8293 board .fs .get (rpath , lpath , ** kwargs )
8394 return
8495
85- def _get_files (self , path , recursive , ** kwargs ):
96+ def _get_files (path , recursive , ** kwargs ):
8697 raw_contents = w .files .list_directory_contents (path )
8798 contents = list (raw_contents )
8899 details = list (map (self ._databricks_content_details , contents ))
@@ -96,11 +107,17 @@ def _get_files(self, path, recursive, **kwargs):
96107 target_path = PurePath (lpath ).joinpath (rel_path )
97108 board .fs .get (item_path , str (target_path ))
98109
99- _get_files (self , rpath , recursive , ** kwargs )
110+ _get_files (rpath , recursive , ** kwargs )
100111
101- @staticmethod
102112 def _databricks_open (self , path ):
103- if not self ._databricks_exists (self , path ):
113+ try :
114+ from databricks .sdk import WorkspaceClient
115+ except ModuleNotFoundError :
116+ raise PinsError (
117+ "Install the `databricks-sdk` package for Databricks board support."
118+ )
119+
120+ if not self ._databricks_exists (path ):
104121 raise PinsError ("File or directory does not exist" )
105122 w = WorkspaceClient ()
106123 resp = w .files .download (path )
@@ -109,7 +126,6 @@ def _databricks_open(self, path):
109126 f .seek (0 )
110127 return f
111128
112- @staticmethod
113129 def _databricks_exists (self , path : str ):
114130 if self ._databricks_is_type (path ) == "nothing" :
115131 return False
@@ -118,6 +134,14 @@ def _databricks_exists(self, path: str):
118134
119135 @staticmethod
120136 def _databricks_is_type (path : str ):
137+ try :
138+ from databricks .sdk import WorkspaceClient
139+ from databricks .sdk .errors import NotFound
140+ except ModuleNotFoundError :
141+ raise PinsError (
142+ "Install the `databricks-sdk` package for Databricks board support."
143+ )
144+
121145 w = WorkspaceClient ()
122146 try :
123147 w .files .get_metadata (path )
@@ -131,9 +155,15 @@ def _databricks_is_type(path: str):
131155 else :
132156 return "file"
133157
134- @staticmethod
135158 def _databricks_ls (self , path , detail ):
136- if not self ._databricks_exists (self , path ):
159+ try :
160+ from databricks .sdk import WorkspaceClient
161+ except ModuleNotFoundError :
162+ raise PinsError (
163+ "Install the `databricks-sdk` package for Databricks board support."
164+ )
165+
166+ if not self ._databricks_exists (path ):
137167 raise PinsError ("File or directory does not exist" )
138168 w = WorkspaceClient ()
139169 if self ._databricks_is_type (path ) == "file" :
@@ -159,22 +189,35 @@ def _databricks_ls(self, path, detail):
159189 items .append (item_path )
160190 return items
161191
162- @staticmethod
163192 def _databricks_rm_dir (self , path ):
193+ try :
194+ from databricks .sdk import WorkspaceClient
195+ except ModuleNotFoundError :
196+ raise PinsError (
197+ "Install the `databricks-sdk` package for Databricks board support."
198+ )
199+
164200 w = WorkspaceClient ()
165201 raw_contents = w .files .list_directory_contents (path )
166202 contents = list (raw_contents )
167203 details = list (map (self ._databricks_content_details , contents ))
168204 for item in details :
169205 item_path = item .get ("path" )
170206 if item .get ("is_directory" ):
171- self ._databricks_rm_dir (self , item_path )
207+ self ._databricks_rm_dir (item_path )
172208 else :
173209 w .files .delete (item_path )
174210 w .files .delete_directory (path )
175211
176212 @staticmethod
177213 def _databricks_mkdir (path ):
214+ try :
215+ from databricks .sdk import WorkspaceClient
216+ except ModuleNotFoundError :
217+ raise PinsError (
218+ "Install the `databricks-sdk` package for Databricks board support."
219+ )
220+
178221 w = WorkspaceClient ()
179222 w .files .create_directory (path )
180223
0 commit comments