11import os
22import sqlite3
3- from pathlib import Path
4-
53from datetime import datetime
4+ from pathlib import Path
65from shutil import copy2
76from subprocess import call
87from sys import stderr , stdout
1110
1211import click
1312from config import Config
14-
1513from rich import box
1614from rich .color import Color
1715from rich .console import Console
1816from rich .style import Style
1917from 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
231229def 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
279282def __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