Skip to content

Commit 57c0676

Browse files
jhamet93Josh Hametpre-commit-ci[bot]
authoredMar 29, 2022
Remove Redundant Dir_Exists Invocation When Creating New Files with ContentsManager (#720)
Co-authored-by: Josh Hamet <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 78521ab commit 57c0676

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed
 

‎jupyter_server/services/contents/handlers.py

-4
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ async def post(self, path=""):
190190
if file_exists:
191191
raise web.HTTPError(400, "Cannot POST to files, use PUT instead.")
192192

193-
dir_exists = await ensure_async(cm.dir_exists(path))
194-
if not dir_exists:
195-
raise web.HTTPError(404, "No such directory: %s" % path)
196-
197193
model = self.get_json_body()
198194

199195
if model is not None:

‎jupyter_server/services/contents/manager.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ def copy(self, from_path, to_path=None):
584584
from_path must be a full path to a file.
585585
"""
586586
path = from_path.strip("/")
587+
587588
if to_path is not None:
588589
to_path = to_path.strip("/")
589590

@@ -599,12 +600,20 @@ def copy(self, from_path, to_path=None):
599600
if model["type"] == "directory":
600601
raise HTTPError(400, "Can't copy directories")
601602

602-
if to_path is None:
603+
is_destination_specified = to_path is not None
604+
if not is_destination_specified:
603605
to_path = from_dir
604606
if self.dir_exists(to_path):
605607
name = copy_pat.sub(".", from_name)
606608
to_name = self.increment_filename(name, to_path, insert="-Copy")
607609
to_path = "{0}/{1}".format(to_path, to_name)
610+
elif is_destination_specified:
611+
if "/" in to_path:
612+
to_dir, to_name = to_path.rsplit("/", 1)
613+
if not self.dir_exists(to_dir):
614+
raise HTTPError(404, "No such parent directory: %s to copy file in" % to_dir)
615+
else:
616+
raise HTTPError(404, "No such directory: %s" % to_path)
608617

609618
model = self.save(model, to_path)
610619
return model
@@ -944,6 +953,7 @@ async def copy(self, from_path, to_path=None):
944953
from_path must be a full path to a file.
945954
"""
946955
path = from_path.strip("/")
956+
947957
if to_path is not None:
948958
to_path = to_path.strip("/")
949959

@@ -958,12 +968,21 @@ async def copy(self, from_path, to_path=None):
958968
model.pop("name", None)
959969
if model["type"] == "directory":
960970
raise HTTPError(400, "Can't copy directories")
961-
if to_path is None:
971+
972+
is_destination_specified = to_path is not None
973+
if not is_destination_specified:
962974
to_path = from_dir
963975
if await ensure_async(self.dir_exists(to_path)):
964976
name = copy_pat.sub(".", from_name)
965977
to_name = await self.increment_filename(name, to_path, insert="-Copy")
966978
to_path = "{0}/{1}".format(to_path, to_name)
979+
elif is_destination_specified:
980+
if "/" in to_path:
981+
to_dir, to_name = to_path.rsplit("/", 1)
982+
if not await ensure_async(self.dir_exists(to_dir)):
983+
raise HTTPError(404, "No such parent directory: %s to copy file in" % to_dir)
984+
else:
985+
raise HTTPError(404, "No such directory: %s" % to_path)
967986

968987
model = await self.save(model, to_path)
969988
return model

‎tests/services/contents/test_manager.py

+9
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,20 @@ async def test_copy(jp_contents_manager):
658658
copy2 = await ensure_async(cm.copy(path, "å b/copy 2.ipynb"))
659659
assert copy2["name"] == "copy 2.ipynb"
660660
assert copy2["path"] == "å b/copy 2.ipynb"
661+
661662
# copy with specified path
662663
copy2 = await ensure_async(cm.copy(path, "/"))
663664
assert copy2["name"] == name
664665
assert copy2["path"] == name
665666

667+
# copy to destination whose parent dir does not exist
668+
with pytest.raises(HTTPError) as e:
669+
await ensure_async(cm.copy(path, "å x/copy 2.ipynb"))
670+
671+
copy3 = await ensure_async(cm.copy(path, "/copy 3.ipynb"))
672+
assert copy3["name"] == "copy 3.ipynb"
673+
assert copy3["path"] == "copy 3.ipynb"
674+
666675

667676
async def test_mark_trusted_cells(jp_contents_manager):
668677
cm = jp_contents_manager

0 commit comments

Comments
 (0)
Please sign in to comment.