Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.

Commit 6029078

Browse files
Merge pull request #54 from Serene-Arc/feature_38
Enables restoring bakfiles to locations other than the original
2 parents 417d88a + 1f7c8e9 commit 6029078

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

bak/__main__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ def bak_up(filename):
8080
is_flag=True,
8181
default=False,
8282
help="No confirmation prompt")
83+
@click.option('-d', '--destination', default=None, type=str)
8384
@click.argument("filename", required=True)
84-
def bak_down(filename, keep, quietly):
85+
def bak_down(filename: str, keep: bool, quietly: bool, destination: str):
8586
if not filename:
8687
click.echo("A filename or operation is required.\n"
8788
"\tbak --help")
8889
filename = Path(filename).expanduser().resolve()
89-
commands.bak_down_cmd(filename, keep, quietly)
90+
if destination:
91+
destination = Path(destination).expanduser().resolve()
92+
commands.bak_down_cmd(filename, destination, keep, quietly)
9093

9194

9295
@bak.command("off", help="Use when finished to delete .bakfiles")

bak/commands/__init__.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os
22
import sqlite3
3-
from pathlib import Path
4-
53
from datetime import datetime
4+
from pathlib import Path
65
from shutil import copy2
76
from subprocess import call
87
from sys import stderr, stdout
@@ -11,14 +10,13 @@
1110

1211
import click
1312
from config import Config
14-
1513
from rich import box
1614
from rich.color import Color
1715
from rich.console import Console
1816
from rich.style import Style
1917
from rich.table import Table
2018

21-
from bak.data import bakfile, bak_db
19+
from bak.data import bak_db, bakfile
2220

2321
# TODO: customizable file extension
2422

@@ -145,7 +143,7 @@ def show_bak_list(filename: Optional[Path] = None,
145143
db_handler.get_all_entries()
146144

147145
console = Console()
148-
if bakfiles == []:
146+
if not bakfiles:
149147
console.print(f"No .bakfiles found for "
150148
f"{filename}" if
151149
filename else "No .bakfiles found")
@@ -165,7 +163,7 @@ def show_bak_list(filename: Optional[Path] = None,
165163
i = 1
166164
for _bakfile in bakfiles:
167165
table.add_row(str(i),
168-
os.path.relpath(filename) if
166+
filename.relative_to(Path.cwd()) if
169167
relative_paths else
170168
_bakfile.orig_abspath,
171169
_bakfile.date_created.split('.')[0],
@@ -229,14 +227,17 @@ def bak_up_cmd(filename: Path):
229227

230228

231229
def bak_down_cmd(filename: Path,
230+
destination: Optional[Path],
232231
keep_bakfile: bool = False,
233232
quiet: bool = False):
234233
""" Restore `filename` from .bakfile. Prompts if ambiguous (such as
235234
when there are multiple .bakfiles of `filename`)
236235
237236
Args:
238-
filename (str|os.path)
237+
filename (str|Path)
239238
keep_bakfile (bool): If False, .bakfile is deleted (default: False)
239+
quiet (bool): If True, does not ask user to confirm
240+
destination (None|Path): destination path to restore to
240241
"""
241242
console = Console()
242243
bakfile_entries = db_handler.get_bakfile_entries(filename)
@@ -252,33 +253,35 @@ def bak_down_cmd(filename: Path,
252253
return
253254
elif not bakfile_entry:
254255
return
256+
if not destination:
257+
destination = Path(bakfile_entry.orig_abspath).expanduser()
255258

256259
if quiet:
257260
confirm = 'y'
258261
else:
259-
confirm_prompt = f"Confirm: Restore {filename} and erase bakfiles?\n" \
262+
if destination != bakfile_entry.orig_abspath:
263+
if destination.exists():
264+
confirm = click.confirm(f"Overwrite {destination}?")
265+
266+
confirm_prompt = f"Confirm: Restore {filename} to {destination} and erase bakfiles?" \
260267
if not keep_bakfile else \
261-
f"Confirm: Restore {filename} and keep bakfiles?\n"
262-
confirm_prompt += "(y/n)"
263-
confirm = click.prompt(confirm_prompt, default='n')
264-
if confirm.lower()[0] != 'y':
268+
f"Confirm: Restore {filename} to {destination} and keep bakfiles?"
269+
confirm = click.confirm(confirm_prompt, default=False)
270+
if not confirm:
265271
console.print("Cancelled.")
266272
return
267273
if not keep_bakfile:
268-
os.rename(bakfile_entry.bakfile_loc, bakfile_entry.orig_abspath)
274+
Path(bakfile_entry.bakfile_loc).rename(destination)
269275
for entry in bakfile_entries:
270-
# bakfile_entry's bakfile has already been moved
271-
# trying to rm it would print a failure
272-
if entry != bakfile_entry:
273-
os.remove(entry.bakfile_loc)
276+
Path(entry.bakfile_loc).unlink(missing_ok=True)
274277
db_handler.del_bakfile_entry(entry)
275278
else:
276-
copy2(bakfile_entry.bakfile_loc, bakfile_entry.orig_abspath)
279+
copy2(bakfile_entry.bakfile_loc, destination)
277280

278281

279282
def __remove_bakfiles(bakfile_entries):
280283
for entry in bakfile_entries:
281-
os.remove(entry.bakfile_loc)
284+
Path(entry.bakfile_loc).unlink()
282285
db_handler.del_bakfile_entry(entry)
283286

284287

@@ -324,7 +327,7 @@ def bak_print_cmd(bak_to_print: (str, bakfile.BakFile),
324327
"C"))
325328
if _bak_to_print is None:
326329
console.print(
327-
f"No bakfiles found for {os.path.abspath(bak_to_print)}")
330+
f"No bakfiles found for {Path(bak_to_print).resolve()}")
328331
else:
329332
bak_to_print = _bak_to_print
330333
if not isinstance(bak_to_print, bakfile.BakFile):
@@ -342,7 +345,7 @@ def bak_getfile_cmd(bak_to_get: (str, bakfile.BakFile)):
342345
filename = bak_to_get
343346
bak_to_get = _get_bakfile_entry(bak_to_get, err=True)
344347
if bak_to_get is None:
345-
console.print(f"No bakfiles found for {os.path.abspath(filename)}")
348+
console.print(f"No bakfiles found for {Path(filename).resolve()}")
346349
return # _get_bakfile_entry() handles failures, so just exit
347350
print(bak_to_get.bakfile_loc)
348351

0 commit comments

Comments
 (0)