Skip to content

Commit bf83762

Browse files
temp
1 parent ac1f030 commit bf83762

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

singlestoredb/management/files.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,12 @@ def is_dir(self, path: PathLike) -> bool:
847847
bool
848848
849849
"""
850-
raise ValueError(
851-
'Operation not supported: directories are currently not allowed in Files API',
852-
)
850+
try:
851+
return self.info(path).type == 'directory'
852+
except ManagementError as exc:
853+
if exc.errno == 404:
854+
return False
855+
raise
853856

854857
# TODO: remove from FileLocation?
855858
def is_file(self, path: PathLike) -> bool:
@@ -873,6 +876,30 @@ def is_file(self, path: PathLike) -> bool:
873876
return False
874877
raise
875878

879+
def _listdir(self, path: PathLike, *, recursive: bool = False) -> List[str]:
880+
"""
881+
Return the names of files in a directory.
882+
883+
Parameters
884+
----------
885+
path : Path or str
886+
Path to the folder in personal/shared space
887+
recursive : bool, optional
888+
Should folders be listed recursively?
889+
890+
"""
891+
res = self._manager._get(
892+
f'files/fs/{self._location}/{path}',
893+
).json()
894+
if recursive:
895+
out = []
896+
for item in res['content'] or []:
897+
out.append(item['path'])
898+
if item['type'] == 'directory':
899+
out.extend(self._listdir(item['path'], recursive=recursive))
900+
return out
901+
return [x['path'] for x in res['content'] or []]
902+
876903
# TODO: remove from FileLocation?
877904
def listdir(
878905
self,
@@ -893,9 +920,17 @@ def listdir(
893920
List[str]
894921
895922
"""
896-
raise ValueError(
897-
'Operation not supported: directories are currently not allowed in Files API',
898-
)
923+
path = re.sub(r'^(\./|/)+', r'', str(path))
924+
path = re.sub(r'/+$', r'', path) + '/'
925+
926+
if self.is_dir(path):
927+
out = self._listdir(path, recursive=recursive)
928+
if path != '/':
929+
stage_path_n = len(path.split('/')) - 1
930+
out = ['/'.join(x.split('/')[stage_path_n:]) for x in out]
931+
return out
932+
933+
raise NotADirectoryError(f'path is not a directory: {path}')
899934

900935
def download_file(
901936
self,

0 commit comments

Comments
 (0)