Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogoncalves03 committed Nov 26, 2024
1 parent ac1f030 commit bf83762
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions singlestoredb/management/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit bf83762

Please sign in to comment.