Skip to content

Commit ca47af1

Browse files
committed
added filters on counting
1 parent 86df6a4 commit ca47af1

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

jupyter_server/services/contents/filemanager.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,16 @@ def _dir_model(self, path, content=True):
296296
model["size"] = None
297297
os_dir = self._get_os_path(path)
298298
dir_contents = os.listdir(os_dir)
299-
model["item_count"] = len(dir_contents)
299+
filtered_count = 0
300+
for name in dir_contents:
301+
try:
302+
os_path = os.path.join(os_dir, name)
303+
if self.should_list(name) and (self.allow_hidden or not is_file_hidden(os_path)):
304+
filtered_count += 1
305+
except OSError as e:
306+
self.log.warning("Error accessing %s: %s", os.path.join(os_dir, name), e)
307+
308+
model["item_count"] = filtered_count
300309
if content:
301310
model["content"] = contents = []
302311
for name in os.listdir(os_dir):
@@ -770,7 +779,16 @@ async def _dir_model(self, path, content=True):
770779
model["size"] = None
771780
os_dir = self._get_os_path(path)
772781
dir_contents = await run_sync(os.listdir, os_dir)
773-
model["item_count"] = len(dir_contents)
782+
filtered_count = 0
783+
for name in dir_contents:
784+
try:
785+
os_path = os.path.join(os_dir, name)
786+
if self.should_list(name) and (self.allow_hidden or not is_file_hidden(os_path)):
787+
filtered_count += 1
788+
except OSError as e:
789+
self.log.warning("Error accessing %s: %s", os.path.join(os_dir, name), e)
790+
791+
model["item_count"] = filtered_count
774792
if content:
775793
model["content"] = contents = []
776794
for name in dir_contents:

tests/services/contents/test_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ async def test_get(jp_contents_manager):
644644
assert "item_count" in dirmodel
645645
assert isinstance(dirmodel["content"], list)
646646
assert len(dirmodel["content"]) == 3
647-
assert dirmodel["item_count"] == 4
647+
assert dirmodel["item_count"] == 3
648648
assert dirmodel["path"] == "foo"
649649
assert dirmodel["name"] == "foo"
650650

0 commit comments

Comments
 (0)