Skip to content

Commit 7a96565

Browse files
committed
added tests, doc and other minor changes
1 parent 1547abb commit 7a96565

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

docs/source/developers/contents.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Models may contain the following entries:
5959
| | ``None`` | if any. (:ref:`See |
6060
| | | Below<modelcontent>`) |
6161
+--------------------+------------+-------------------------------+
62+
| **item_count** | int or | The number of items in a |
63+
| | ``None`` | directory, or ``None`` for |
64+
| | | files and notebooks. |
65+
+--------------------+------------+-------------------------------+
6266
| **format** | unicode or | The format of ``content``, |
6367
| | ``None`` | if any. (:ref:`See |
6468
| | | Below<modelcontent>`) |
@@ -110,6 +114,7 @@ model. There are three model types: **notebook**, **file**, and **directory**.
110114
- The ``content`` field contains a list of :ref:`content-free<contentfree>`
111115
models representing the entities in the directory.
112116
- The ``hash`` field is always ``None``.
117+
- The ``item_count`` field contains the number of items in the directory
113118

114119
.. note::
115120

jupyter_server/services/contents/filemanager.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def _base_model(self, path):
272272
model["writable"] = self.is_writable(path)
273273
model["hash"] = None
274274
model["hash_algorithm"] = None
275+
model["item_count"] = None
275276

276277
return model
277278

@@ -293,12 +294,18 @@ def _dir_model(self, path, content=True):
293294
model = self._base_model(path)
294295
model["type"] = "directory"
295296
model["size"] = None
296-
model["item_count"] = None
297+
os_dir = self._get_os_path(path)
298+
dir_contents = os.listdir(os_dir)
299+
model["item_count"] = len(
300+
[
301+
name
302+
for name in dir_contents
303+
if self.should_list(name)
304+
and (self.allow_hidden or not is_file_hidden(os.path.join(os_dir, name)))
305+
]
306+
)
297307
if content:
298308
model["content"] = contents = []
299-
os_dir = self._get_os_path(path)
300-
dir_contents = os.listdir(os_dir)
301-
model["item_count"] = len(dir_contents)
302309
for name in os.listdir(os_dir):
303310
try:
304311
os_path = os.path.join(os_dir, name)
@@ -768,12 +775,18 @@ async def _dir_model(self, path, content=True):
768775
model = self._base_model(path)
769776
model["type"] = "directory"
770777
model["size"] = None
771-
model["item_count"] = None
778+
os_dir = self._get_os_path(path)
779+
dir_contents = await run_sync(os.listdir, os_dir)
780+
model["item_count"] = len(
781+
[
782+
name
783+
for name in dir_contents
784+
if self.should_list(name)
785+
and (self.allow_hidden or not is_file_hidden(os.path.join(os_dir, name)))
786+
]
787+
)
772788
if content:
773789
model["content"] = contents = []
774-
os_dir = self._get_os_path(path)
775-
dir_contents = await run_sync(os.listdir, os_dir)
776-
model["item_count"] = len(dir_contents)
777790
for name in dir_contents:
778791
try:
779792
os_path = os.path.join(os_dir, name)

tests/services/contents/test_manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ async def test_get(jp_contents_manager):
590590
assert isinstance(model2, dict)
591591
assert "name" in model2
592592
assert "path" in model2
593+
assert "item_count" in model2
593594
assert "content" in model2
594595
assert model2["name"] == "Untitled.ipynb"
595596
assert model2["path"] == "{}/{}".format(sub_dir.strip("/"), name)
@@ -631,6 +632,7 @@ async def test_get(jp_contents_manager):
631632
for key, value in expected_model.items():
632633
assert file_model[key] == value
633634
assert "created" in file_model
635+
assert "item_count" in file_model
634636
assert "last_modified" in file_model
635637
assert file_model["hash"]
636638

@@ -639,8 +641,10 @@ async def test_get(jp_contents_manager):
639641
_make_dir(cm, "foo/bar")
640642
dirmodel = await ensure_async(cm.get("foo"))
641643
assert dirmodel["type"] == "directory"
644+
assert "item_count" in dirmodel
642645
assert isinstance(dirmodel["content"], list)
643646
assert len(dirmodel["content"]) == 3
647+
assert dirmodel["item_count"] == 3
644648
assert dirmodel["path"] == "foo"
645649
assert dirmodel["name"] == "foo"
646650

@@ -649,6 +653,7 @@ async def test_get(jp_contents_manager):
649653
model2_no_content = await ensure_async(cm.get(sub_dir + name, content=False))
650654
file_model_no_content = await ensure_async(cm.get("foo/untitled.txt", content=False))
651655
sub_sub_dir_no_content = await ensure_async(cm.get("foo/bar", content=False))
656+
assert "item_count" in model2_no_content
652657
assert sub_sub_dir_no_content["path"] == "foo/bar"
653658
assert sub_sub_dir_no_content["name"] == "bar"
654659

0 commit comments

Comments
 (0)