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

Commit 3267aad

Browse files
Extend decorator: user output, convert arg to Path
1 parent 736a787 commit 3267aad

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

bak/__main__.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ def __print_help():
1616
click.echo(bak.get_help(ctx))
1717

1818

19-
def check_file_reference(args_key: str = 'filename'):
19+
def normalize_path(args_key: str = 'filename'):
2020
def on_decorator(func):
2121
@functools.wraps(func)
2222
def on_call(*args, **kwargs):
2323
try:
24-
if Path(kwargs[args_key]).expanduser().resolve().is_dir():
25-
raise IsADirectoryError('{} is a directory'.format(kwargs[args_key]))
26-
except IndexError:
24+
# expand path
25+
arg = Path(kwargs[args_key]).expanduser().resolve()
26+
if arg.is_dir():
27+
click.echo(f"Error: bak cannot operate on directories ({arg})")
28+
return
29+
else:
30+
kwargs[args_key] = arg
31+
# Account for optional params and params that default to None or False
32+
except (IndexError, KeyError, TypeError):
2733
pass
2834
return func(*args, **kwargs)
2935
return on_call
@@ -40,7 +46,7 @@ def bak():
4046

4147

4248
@bak.command("\0", hidden=True)
43-
@check_file_reference()
49+
@normalize_path()
4450
@click.option("--version", required=False, is_flag=True)
4551
@click.argument("filename", required=False, type=click.Path(exists=True))
4652
# Ensures that 'bak --help' is printed if it doesn't get a filename
@@ -55,7 +61,7 @@ def create(filename, version):
5561

5662

5763
@bak.command("up", help="Replace a .bakfile with a fresh copy of the parent file")
58-
@check_file_reference()
64+
@normalize_path()
5965
@click.argument("filename", required=True, type=click.Path(exists=True))
6066
def bak_up(filename):
6167
if not filename:
@@ -102,7 +108,7 @@ def bak_off(filename, quietly):
102108
@click.option("--using", "--in", "--with",
103109
help="Program to open (default: $PAGER or less)",
104110
required=False, hidden=True)
105-
@check_file_reference()
111+
@normalize_path()
106112
@click.argument("filename", required=True, type=click.Path(exists=True))
107113
def bak_print(filename, using):
108114
filename = Path(filename).expanduser().resolve()
@@ -116,7 +122,7 @@ def bak_print(filename, using):
116122
@click.argument("to_where_you_once_belonged",
117123
required=True,
118124
type=click.Path(exists=True))
119-
@check_file_reference()
125+
@normalize_path()
120126
def bak_get(to_where_you_once_belonged):
121127
to_where_you_once_belonged = Path(to_where_you_once_belonged).expanduser().resolve()
122128
commands.bak_getfile_cmd(to_where_you_once_belonged)
@@ -127,7 +133,7 @@ def bak_get(to_where_you_once_belonged):
127133
@click.option("--using", "--with",
128134
help="Program to use instead of system diff",
129135
required=False)
130-
@check_file_reference()
136+
@normalize_path()
131137
@click.argument("filename", required=True, type=click.Path(exists=True))
132138
def bak_diff(filename, using):
133139
filename = Path(filename).expanduser().resolve()
@@ -145,15 +151,12 @@ def bak_diff(filename, using):
145151
# help="List a particular file's .bakfiles",
146152
required=False,
147153
type=click.Path(exists=True))
148-
@check_file_reference()
154+
@normalize_path()
149155
def bak_list(relpaths, filename):
150156
if filename:
151157
filename = Path(filename).expanduser().resolve()
152158
commands.show_bak_list(filename=filename or None, relative_paths=relpaths)
153159

154160

155161
if __name__ == "__main__":
156-
try:
157-
bak()
158-
except IsADirectoryError as e:
159-
print('Must pass a file, not a directory to bak: {}'.format(e))
162+
bak()

bak/commands/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def _assemble_bakfile(filename: Path):
6767
def _get_bakfile_entry(filename: Path,
6868
select_prompt=default_select_prompt,
6969
err=True):
70-
entries = db_handler.get_bakfile_entries(filename.resolve())
71-
if (entries is False) or len(entries) == 0:
70+
entries = db_handler.get_bakfile_entries(filename)
71+
if not entries:
7272
return None
7373
# If there's only one bakfile corresponding to filename, return that.
7474
# If there's more than one, disambiguate.
@@ -166,7 +166,7 @@ def show_bak_list(filename: Optional[Path] = None,
166166
i = 1
167167
for _bakfile in bakfiles:
168168
table.add_row(str(i),
169-
(filename.resolve()) if
169+
os.path.relpath(filename) if
170170
relative_paths else
171171
_bakfile.orig_abspath,
172172
_bakfile.date_created.split('.')[0],
@@ -355,13 +355,13 @@ def bak_diff_cmd(filename: (bakfile.BakFile, Path), command='diff'):
355355
console = Console()
356356

357357
bak_to_diff = filename if isinstance(filename, bakfile.BakFile) else \
358-
_get_bakfile_entry(filename.resolve(),
358+
_get_bakfile_entry(filename,
359359
select_prompt=(
360360
("Enter a number to diff a .bakfile, or:\n(V)iew (L)ist (C)ancel", "C")))
361361
if not command:
362362
command = cfg['bak_diff_exec'] or 'diff'
363363
if bak_to_diff is None:
364-
console.print(f"No bakfiles found for {filename.resolve()}")
364+
console.print(f"No bakfiles found for {filename}")
365365
return
366366
if not bak_to_diff:
367367
return

0 commit comments

Comments
 (0)