Skip to content

Commit cdaf518

Browse files
committed
reformat get function
1 parent cdf9764 commit cdaf518

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed

jupyter_drives/manager.py

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -223,56 +223,47 @@ async def get_contents(self, drive_name, path):
223223

224224
try :
225225
data = []
226-
isDir = False
227-
emptyDir = True # assume we are dealing with an empty directory
228-
229-
chunk_size = 100
230-
if self._max_files_listed < chunk_size:
231-
chunk_size = self._max_files_listed
232-
no_batches = int(self._max_files_listed/chunk_size)
233-
234-
# using Arrow lists as they are recommended for large results
235-
# stream will be an async iterable of RecordBatch
236-
current_batch = 0
237-
stream = obs.list(self._content_managers[drive_name]["store"], path, chunk_size=chunk_size, return_arrow=True)
238-
async for batch in stream:
239-
current_batch += 1
240-
# reached last batch that can be shown (partially)
241-
if current_batch == no_batches + 1:
242-
remaining_files = self._max_files_listed - no_batches*chunk_size
243-
244-
# if content exists we are dealing with a directory
245-
if isDir is False and batch:
246-
isDir = True
247-
emptyDir = False
226+
is_dir = await self._file_system._isdir(drive_name + '/' + path)
227+
228+
if is_dir == True:
229+
chunk_size = 100
230+
if self._max_files_listed < chunk_size:
231+
chunk_size = self._max_files_listed
232+
no_batches = int(self._max_files_listed/chunk_size)
233+
234+
# using Arrow lists as they are recommended for large results
235+
# stream will be an async iterable of RecordBatch
236+
current_batch = 0
237+
stream = obs.list(self._content_managers[drive_name]["store"], path, chunk_size=chunk_size, return_arrow=True)
238+
async for batch in stream:
239+
current_batch += 1
240+
# reached last batch that can be shown (partially)
241+
if current_batch == no_batches + 1:
242+
remaining_files = self._max_files_listed - no_batches*chunk_size
243+
244+
contents_list = pyarrow.record_batch(batch).to_pylist()
245+
for object in contents_list:
246+
# when listing the last batch (partially), make sure we don't exceed limit
247+
if current_batch == no_batches + 1:
248+
if remaining_files <= 0:
249+
break
250+
remaining_files -= 1
251+
data.append({
252+
"path": object["path"],
253+
"last_modified": object["last_modified"].isoformat(),
254+
"size": object["size"],
255+
})
248256

249-
contents_list = pyarrow.record_batch(batch).to_pylist()
250-
for object in contents_list:
251-
# when listing the last batch (partially), make sure we don't exceed limit
257+
# check if we reached the limit of files that can be listed
252258
if current_batch == no_batches + 1:
253-
if remaining_files <= 0:
254-
break
255-
remaining_files -= 1
256-
data.append({
257-
"path": object["path"],
258-
"last_modified": object["last_modified"].isoformat(),
259-
"size": object["size"],
260-
})
259+
break
261260

262-
# check if we reached the limit of files that can be listed
263-
if current_batch == no_batches + 1:
264-
break
265-
266-
# check if we are dealing with an empty drive
267-
if isDir is False and path != '':
261+
else:
268262
content = b""
269263
# retrieve contents of object
270264
obj = await obs.get_async(self._content_managers[drive_name]["store"], path)
271265
stream = obj.stream(min_chunk_size=5 * 1024 * 1024) # 5MB sized chunks
272266
async for buf in stream:
273-
# if content exists we are dealing with a file
274-
if emptyDir is True and buf:
275-
emptyDir = False
276267
content += buf
277268

278269
# retrieve metadata of object
@@ -293,12 +284,6 @@ async def get_contents(self, drive_name, path):
293284
"size": metadata["size"]
294285
}
295286

296-
# dealing with the case of an empty directory, making sure it is not an empty file
297-
if emptyDir is True:
298-
check = await self._file_system._isdir(drive_name + '/' + path)
299-
if check == True:
300-
data = []
301-
302287
response = {
303288
"data": data
304289
}

0 commit comments

Comments
 (0)