Skip to content

Commit

Permalink
Revert unintended effects of #1966 (#1974)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Steven Silvester <[email protected]>
  • Loading branch information
3 people authored May 6, 2023
1 parent 27804f1 commit f0d86bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
20 changes: 3 additions & 17 deletions nbconvert/nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _classes_default(self):
output_base = Unicode(
"{notebook_name}",
help="""Overwrite base name use for output files.
Supports pattern replacements '{notebook_name}' and '{notebook_filename}'.
Supports pattern replacements '{notebook_name}'.
""",
).tag(config=True)

Expand Down Expand Up @@ -420,9 +420,7 @@ def _notebook_filename_to_name(self, notebook_filename):
"""
basename = os.path.basename(notebook_filename)
notebook_name = basename[: basename.rfind(".")]
notebook_name = self.output_base.format(
notebook_name=notebook_name, notebook_filename=notebook_filename
)
notebook_name = self.output_base.format(notebook_name=notebook_name)

return notebook_name

Expand Down Expand Up @@ -513,7 +511,7 @@ def write_single_notebook(self, output, resources):
raise KeyError(msg)

notebook_name = resources["unique_key"]
if self.use_output_suffix:
if self.use_output_suffix and self.output_base == "{notebook_name}":
notebook_name += resources.get("output_suffix", "")

write_results = self.writer.write(output, resources, notebook_name=notebook_name)
Expand Down Expand Up @@ -585,18 +583,6 @@ def convert_notebooks(self):
if ext == self.exporter.file_extension:
self.output_base = base

# Validate that output_base does not cause us to overwrite already generated
# files
notebook_names = [self._notebook_filename_to_name(fn) for fn in self.notebooks]
if len(notebook_names) != len(set(notebook_names)):
msg = (
"Conversion would override an already generated output. "
"This is probably due to --output or output_base configuration "
"leading to non-unique output names. "
f"Output notebook names were: {notebook_names}"
)
raise ValueError(msg)

# convert each notebook
if not self.from_stdin:
for notebook_filename in self.notebooks:
Expand Down
25 changes: 20 additions & 5 deletions nbconvert/tests/test_nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,16 +654,31 @@ def test_output_base(self):
for nbn in notebook_names:
assert os.path.isfile(f"{nbn}.md")

with pytest.raises(OSError), self.create_temp_cwd([x + ".ipynb" for x in notebook_names]):
self.nbconvert("*.ipynb --output notebook_test_name --to markdown")

# Test single output with static output name
with self.create_temp_cwd([notebook_names[0] + ".ipynb"]):
self.nbconvert("*.ipynb --output notebook_test_name --to markdown")
nbname = notebook_names[0]
with self.create_temp_cwd([nbname + ".ipynb"]):
self.nbconvert(f"{nbname}.ipynb --output notebook_test_name --to markdown")
assert os.path.isfile("notebook_test_name.md")

self.nbconvert(f"{nbname}.ipynb --to notebook")
assert os.path.isfile("notebook1.nbconvert.ipynb")

self.nbconvert(f"{nbname}.ipynb --to notebook --output out.ipynb")
assert os.path.isfile("out.ipynb")

# Test double extension fix
with self.create_temp_cwd([notebook_names[0] + ".ipynb"]):
self.nbconvert("*.ipynb --output notebook_test_name.md --to markdown")
assert os.path.isfile("notebook_test_name.md")
assert not os.path.isfile("notebook_test_name.md.md")

def test_same_filename_different_dir(self):
"""
Check if files with same name in different directories pose a problem
"""
with self.create_temp_cwd() as temp_wd:
self.copy_files_to(["notebook1.ipynb"], temp_wd + "/dir1")
self.copy_files_to(["notebook1.ipynb"], temp_wd + "/dir2")
self.nbconvert("dir1/notebook1.ipynb dir2/notebook1.ipynb --to markdown")
assert os.path.isfile("dir1/notebook1.md")
assert os.path.isfile("dir2/notebook1.md")

0 comments on commit f0d86bc

Please sign in to comment.