Skip to content

Commit cbf22f8

Browse files
committed
API: file_write(): add backup kwd, fail if file exists
1 parent 075513d commit cbf22f8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/sphinx_autodoc/main.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pj = os.path.join
1111

1212

13-
def backup(src, prefix="."):
13+
def _backup(src, prefix="."):
1414
"""Backup (copy) `src` to <src><prefix><num>, where <num> is an integer
1515
starting at 0 which is incremented until there is no destination with that
1616
name.
@@ -43,9 +43,12 @@ def backup(src, prefix="."):
4343
copy(src, dst)
4444

4545

46-
def file_write(fn, txt):
46+
def file_write(fn, txt, backup=False):
4747
if os.path.exists(fn):
48-
backup(fn, prefix=".bak")
48+
if backup:
49+
_backup(fn, prefix=".bak")
50+
else:
51+
raise Exception(f"file exists: {fn}")
4952
with open(fn, "w") as fd:
5053
fd.write(txt)
5154

@@ -422,4 +425,4 @@ def main():
422425
package_name=package_name,
423426
bar=bar,
424427
)
425-
file_write(index_fn, txt)
428+
file_write(index_fn, txt, backup=True)

0 commit comments

Comments
 (0)