Skip to content

Commit

Permalink
added tests, doc and other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishab87 committed Jan 24, 2025
1 parent 1547abb commit 7a96565
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/source/developers/contents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Models may contain the following entries:
| | ``None`` | if any. (:ref:`See |
| | | Below<modelcontent>`) |
+--------------------+------------+-------------------------------+
| **item_count** | int or | The number of items in a |
| | ``None`` | directory, or ``None`` for |
| | | files and notebooks. |
+--------------------+------------+-------------------------------+
| **format** | unicode or | The format of ``content``, |
| | ``None`` | if any. (:ref:`See |
| | | Below<modelcontent>`) |
Expand Down Expand Up @@ -110,6 +114,7 @@ model. There are three model types: **notebook**, **file**, and **directory**.
- The ``content`` field contains a list of :ref:`content-free<contentfree>`
models representing the entities in the directory.
- The ``hash`` field is always ``None``.
- The ``item_count`` field contains the number of items in the directory

.. note::

Expand Down
29 changes: 21 additions & 8 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def _base_model(self, path):
model["writable"] = self.is_writable(path)
model["hash"] = None
model["hash_algorithm"] = None
model["item_count"] = None

return model

Expand All @@ -293,12 +294,18 @@ def _dir_model(self, path, content=True):
model = self._base_model(path)
model["type"] = "directory"
model["size"] = None
model["item_count"] = None
os_dir = self._get_os_path(path)
dir_contents = os.listdir(os_dir)
model["item_count"] = len(
[
name
for name in dir_contents
if self.should_list(name)
and (self.allow_hidden or not is_file_hidden(os.path.join(os_dir, name)))
]
)
if content:
model["content"] = contents = []
os_dir = self._get_os_path(path)
dir_contents = os.listdir(os_dir)
model["item_count"] = len(dir_contents)
for name in os.listdir(os_dir):
try:
os_path = os.path.join(os_dir, name)
Expand Down Expand Up @@ -768,12 +775,18 @@ async def _dir_model(self, path, content=True):
model = self._base_model(path)
model["type"] = "directory"
model["size"] = None
model["item_count"] = None
os_dir = self._get_os_path(path)
dir_contents = await run_sync(os.listdir, os_dir)
model["item_count"] = len(
[
name
for name in dir_contents
if self.should_list(name)
and (self.allow_hidden or not is_file_hidden(os.path.join(os_dir, name)))
]
)
if content:
model["content"] = contents = []
os_dir = self._get_os_path(path)
dir_contents = await run_sync(os.listdir, os_dir)
model["item_count"] = len(dir_contents)
for name in dir_contents:
try:
os_path = os.path.join(os_dir, name)
Expand Down
5 changes: 5 additions & 0 deletions tests/services/contents/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ async def test_get(jp_contents_manager):
assert isinstance(model2, dict)
assert "name" in model2
assert "path" in model2
assert "item_count" in model2
assert "content" in model2
assert model2["name"] == "Untitled.ipynb"
assert model2["path"] == "{}/{}".format(sub_dir.strip("/"), name)
Expand Down Expand Up @@ -631,6 +632,7 @@ async def test_get(jp_contents_manager):
for key, value in expected_model.items():
assert file_model[key] == value
assert "created" in file_model
assert "item_count" in file_model
assert "last_modified" in file_model
assert file_model["hash"]

Expand All @@ -639,8 +641,10 @@ async def test_get(jp_contents_manager):
_make_dir(cm, "foo/bar")
dirmodel = await ensure_async(cm.get("foo"))
assert dirmodel["type"] == "directory"
assert "item_count" in dirmodel
assert isinstance(dirmodel["content"], list)
assert len(dirmodel["content"]) == 3
assert dirmodel["item_count"] == 3
assert dirmodel["path"] == "foo"
assert dirmodel["name"] == "foo"

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

Expand Down

0 comments on commit 7a96565

Please sign in to comment.