Skip to content

Commit 968c861

Browse files
committed
Better utilize named path splitting in pathlib
1 parent 59c1244 commit 968c861

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

nbgrader/coursedir.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,24 +316,25 @@ def format_path(
316316
When escape=True, the non-passed elements of the path are regex-escaped, so the
317317
resulting string can be used as a pattern to match path components.
318318
"""
319-
320319
kwargs = dict(
321320
nbgrader_step=nbgrader_step,
322321
student_id=student_id,
323322
assignment_id=assignment_id
324323
)
325-
326-
if escape:
327-
base = Path(re.escape(self.root))
328-
else:
329-
base = Path(self.root)
330-
331-
path = base / self.directory_structure.format(**kwargs)
332-
324+
base = Path(self.root)
325+
structure = Path(self.directory_structure.format(**kwargs))
333326
if escape:
334-
return path.anchor + re.escape(os.path.sep).join(path.parts[1:])
327+
if structure.is_absolute():
328+
anchor = structure.anchor
329+
parts = list(structure.parts)
330+
else:
331+
anchor = base.anchor
332+
parts = [re.escape(part) for part in base._tail] + list(structure.parts)
333+
return re.escape(anchor) + re.escape(os.path.sep).join(parts)
335334
else:
336-
return str(path)
335+
if structure.is_absolute():
336+
return str(structure)
337+
return str(base / structure)
337338

338339
def find_assignments(self,
339340
nbgrader_step: str = "*",

nbgrader/tests/test_coursedir.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ def test_coursedir_configurable(conf, course_dir):
2020
assert coursedir.root == course_dir
2121

2222

23-
@pytest.mark.parametrize("root", [None, os.path.sep + "[special]~root"])
23+
@pytest.mark.parametrize("root", [
24+
None, # Keep the course_dir fixture
25+
os.path.sep + "[special]~root",
26+
"C:\\Users\\Student",
27+
])
2428
def test_coursedir_format_path(conf, root):
2529
if root is not None:
2630
conf.CourseDirectory.root = root

0 commit comments

Comments
 (0)