diff --git a/bak/__init__.py b/bak/__init__.py index f6030c3..4ac7512 100644 --- a/bak/__init__.py +++ b/bak/__init__.py @@ -1 +1 @@ -BAK_VERSION = "0.2.2a7" +BAK_VERSION = "0.2.2a10" diff --git a/bak/cli.py b/bak/cli.py index cfcb4a3..3bd6060 100644 --- a/bak/cli.py +++ b/bak/cli.py @@ -1,6 +1,5 @@ import functools from pathlib import Path -from typing import Union import click @@ -114,7 +113,7 @@ def bak_down(filename: str, keep: bool, quietly: bool, destination: str, bakfile if destination: destination = Path(destination).expanduser().resolve() if not isinstance(keep, tuple): - if keep == -1 or keep == 'all': + if keep in [-1, 'all']: keep = True elif keep == 0: keep = False @@ -241,4 +240,4 @@ def bak_list(colors, relpaths, compare, filename): @click.argument("setting", required=True) @click.argument("value", required=False, nargs=-1, type=str) def bak_config(get, setting, value): - commands.bak_config_command(get, setting, value) \ No newline at end of file + commands.bak_config_command(get, setting, value) diff --git a/bak/commands/__init__.py b/bak/commands/__init__.py index 61546e0..b51c082 100644 --- a/bak/commands/__init__.py +++ b/bak/commands/__init__.py @@ -176,7 +176,7 @@ def __identify_baks(entries): def __distinguish_baks(bakfiles): filenames = set(_bakfile.orig_abspath for _bakfile in bakfiles) - _identified_baks = dict() + _identified_baks = {} for _filename in filenames: _identified_baks[_filename] = __identify_baks( [_bakfile for _bakfile in bakfiles if _bakfile.orig_abspath == _filename]) @@ -391,9 +391,9 @@ def bak_up_cmd(filename: Path, bakfile_number: int=0): # Disambiguate if len(old_bakfile) == 1: old_bakfile = old_bakfile[0] - if bakfile_number > 1: + if bakfile_number and bakfile_number > 1: console.print(f"Only found 1 bakfile for {filename}") - elif bakfile_number > 0: + elif bakfile_number and bakfile_number > 0: old_bakfile = __get_bakfile_entry(filename, bakfile_number=bakfile_number, console=console) else: old_bakfile = __do_select_bakfile(old_bakfile, @@ -461,75 +461,40 @@ def bak_down_cmd(filename: Path, else: destination = Path(bakfile_entry.orig_abspath) - if quiet: - confirm = 'y' - else: - if all((new_destination, destination.exists())): - if not click.confirm(f"Overwrite {destination}?", default=False): - click.echo("Cancelled.") - return - - erase = '' - keep_which = '' - joint = '' - _all = '' - baks_to_keep: list[int] = None - if keep_bakfile: - erase = 'keep' - if isinstance(keep_bakfile, list): - keep_bakfile = sorted(set(keep_bakfile)) - try: - baks_to_keep = [int(i) for i in keep_bakfile] - except ValueError: - if keep_bakfile != ['all']: - click.echo("Error: bak down --keep only accepts bakfile #s or the word 'all'") - click.echo("Cancelled.") - return - if len(keep_bakfile) > 1: - if len(keep_bakfile) > 2: - joint = ', ' - else: - joint = ' ' - keep_bakfile[-1] = "and " + keep_bakfile[-1] - keep_which = ' ' + joint.join(keep_bakfile) - else: - keep_which = ' ' + keep_bakfile[0] - if keep_which.endswith('all'): - _all = ' all' - elif len(keep_which.strip()) == 1: - keep_which = ' #' + keep_which.lstrip() - else: - keep_which = 's ' + keep_which.lstrip() - - confirm_prompt = f"Confirm: Restore {filename}" - - multiples = bool(len(bakfile_entries)) - restore_from = f"{index + 1}" \ - if (multiples or index != 0) \ - else (f"{bakfile_number}" if bakfile_number else '') - confirm_prompt += f" from bakfile #{restore_from}" if restore_from else '' - - confirm_prompt += f" to {destination}" if new_destination else "" - confirm_prompt += f" and {erase}{_all} bakfile{keep_which if not _all else ('s' if multiples else '')}?" - - confirm = click.confirm(confirm_prompt, default=False) + confirm = True if quiet else _bak_down_confirm_helper(filename, + bakfile_number, + bakfile_entries, + destination, + new_destination, + keep_bakfile, + index) if not confirm: console.print("Cancelled.") return - + try: copy2(bakfile_entry.bakfile_loc, destination) except PermissionError: _sudo_bak_down_helper(bakfile_entry.bakfile_loc, destination) if not keep_bakfile and not new_destination: - click.echo(f"NOT KEEP_BAKFILE: {keep_bakfile}") for entry in bakfile_entries: if entry.restored: if entry.bakfile_loc != bakfile_entry.bakfile_loc: db_handler.set_restored_flag(entry, False) db_handler.set_restored_flag(bakfile_entry, True) + baks_to_keep = [] + if isinstance(keep_bakfile, list): + keep_bakfile = sorted(set(keep_bakfile)) + try: + baks_to_keep = [int(i) for i in keep_bakfile] + except ValueError: + if keep_bakfile != ['all']: + click.echo("Error: bak down --keep only accepts bakfile #s or the word 'all'") + click.echo("Cancelled.") + return + args = [bakfile_entries] if not keep_bakfile else [bakfile_entry, bakfile_entries, new_destination, @@ -537,6 +502,55 @@ def bak_down_cmd(filename: Path, helper = __keep_bakfiles if keep_bakfile else __remove_bakfiles helper(*args) +def _bak_down_confirm_helper(filename, + bakfile_number, + bakfile_entries, + destination, + new_destination, + keep_bakfile, + index): +# TODO at this point, just turn this into something stateful + if all((new_destination, destination.exists())): + if not click.confirm(f"Overwrite {destination}?", default=False): + click.echo("Cancelled.") + return False + + erase = 'erase' + keep_which = '' + joint = '' + _all = '' + if keep_bakfile: + erase = 'keep' + if isinstance(keep_bakfile, list): + if len(keep_bakfile) > 1: + if len(keep_bakfile) > 2: + joint = ', ' + else: + joint = ' ' + keep_bakfile[-1] = "and " + keep_bakfile[-1] + keep_which = ' ' + joint.join(keep_bakfile) + else: + keep_which = ' ' + keep_bakfile[0] + if keep_which.endswith('all'): + _all = ' all' + elif len(keep_which.strip()) == 1: + keep_which = ' #' + keep_which.lstrip() + else: + keep_which = 's ' + keep_which.lstrip() + + confirm_prompt = f"Confirm: Restore {filename}" + + multiples = bool(len(bakfile_entries)) + restore_from = f"{index + 1}" \ + if (multiples or index != 0) \ + else (f"{bakfile_number}" if bakfile_number else '') + confirm_prompt += f" from bakfile #{restore_from}" if restore_from else '' + + confirm_prompt += f" to {destination}" if new_destination else "" + confirm_prompt += f" and {erase}{_all} bakfile{keep_which if not _all else ('s' if multiples else '')}?" + + return click.confirm(confirm_prompt, default=False) + def bak_del_cmd(filename:Path, bakfile_number:int, quietly=False): """ Deletes a bakfile by number """ diff --git a/bak/data/bak_db.py b/bak/data/bak_db.py index 6634444..9dda01d 100644 --- a/bak/data/bak_db.py +++ b/bak/data/bak_db.py @@ -44,12 +44,12 @@ def create_bakfile_entry(self, bakfile_obj: BakFile): db_conn.commit() def del_bakfile_entry(self, bak_entry: BakFile): - with sqlite3.connect(self.db_loc) as db_conn: + with sqlite3.connect(self.db_loc) as db_conn: db_conn.execute( """ DELETE FROM bakfiles WHERE bakfile=:bakfile """, (bak_entry.bakfile_loc,)) - db_conn.commit() + db_conn.commit() def del_all_entries(self, bak_entry: BakFile): with sqlite3.connect(self.db_loc) as db_conn: