diff --git a/singlestoredb/management/files.py b/singlestoredb/management/files.py index f804903d..2e4f787d 100644 --- a/singlestoredb/management/files.py +++ b/singlestoredb/management/files.py @@ -847,9 +847,12 @@ def is_dir(self, path: PathLike) -> bool: bool """ - raise ValueError( - 'Operation not supported: directories are currently not allowed in Files API', - ) + try: + return self.info(path).type == 'directory' + except ManagementError as exc: + if exc.errno == 404: + return False + raise # TODO: remove from FileLocation? def is_file(self, path: PathLike) -> bool: @@ -873,6 +876,30 @@ def is_file(self, path: PathLike) -> bool: return False raise + def _listdir(self, path: PathLike, *, recursive: bool = False) -> List[str]: + """ + Return the names of files in a directory. + + Parameters + ---------- + path : Path or str + Path to the folder in personal/shared space + recursive : bool, optional + Should folders be listed recursively? + + """ + res = self._manager._get( + f'files/fs/{self._location}/{path}', + ).json() + if recursive: + out = [] + for item in res['content'] or []: + out.append(item['path']) + if item['type'] == 'directory': + out.extend(self._listdir(item['path'], recursive=recursive)) + return out + return [x['path'] for x in res['content'] or []] + # TODO: remove from FileLocation? def listdir( self, @@ -893,9 +920,17 @@ def listdir( List[str] """ - raise ValueError( - 'Operation not supported: directories are currently not allowed in Files API', - ) + path = re.sub(r'^(\./|/)+', r'', str(path)) + path = re.sub(r'/+$', r'', path) + '/' + + if self.is_dir(path): + out = self._listdir(path, recursive=recursive) + if path != '/': + stage_path_n = len(path.split('/')) - 1 + out = ['/'.join(x.split('/')[stage_path_n:]) for x in out] + return out + + raise NotADirectoryError(f'path is not a directory: {path}') def download_file( self,