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

Commit 736a787

Browse files
Serene-ArcChanceNCounter
authored andcommitted
Add decorator to check for directories
1 parent d7e65ea commit 736a787

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

bak/__main__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import os
23
from datetime import datetime
34
from pathlib import Path
@@ -15,6 +16,20 @@ def __print_help():
1516
click.echo(bak.get_help(ctx))
1617

1718

19+
def check_file_reference(args_key: str = 'filename'):
20+
def on_decorator(func):
21+
@functools.wraps(func)
22+
def on_call(*args, **kwargs):
23+
try:
24+
if Path(kwargs[args_key]).expanduser().resolve().is_dir():
25+
raise IsADirectoryError('{} is a directory'.format(kwargs[args_key]))
26+
except IndexError:
27+
pass
28+
return func(*args, **kwargs)
29+
return on_call
30+
return on_decorator
31+
32+
1833
basic_help_text = "bak FILENAME (creates a bakfile)\n\n" +\
1934
"See also: bak COMMAND --help"
2035

@@ -25,6 +40,7 @@ def bak():
2540

2641

2742
@bak.command("\0", hidden=True)
43+
@check_file_reference()
2844
@click.option("--version", required=False, is_flag=True)
2945
@click.argument("filename", required=False, type=click.Path(exists=True))
3046
# Ensures that 'bak --help' is printed if it doesn't get a filename
@@ -39,6 +55,7 @@ def create(filename, version):
3955

4056

4157
@bak.command("up", help="Replace a .bakfile with a fresh copy of the parent file")
58+
@check_file_reference()
4259
@click.argument("filename", required=True, type=click.Path(exists=True))
4360
def bak_up(filename):
4461
if not filename:
@@ -85,6 +102,7 @@ def bak_off(filename, quietly):
85102
@click.option("--using", "--in", "--with",
86103
help="Program to open (default: $PAGER or less)",
87104
required=False, hidden=True)
105+
@check_file_reference()
88106
@click.argument("filename", required=True, type=click.Path(exists=True))
89107
def bak_print(filename, using):
90108
filename = Path(filename).expanduser().resolve()
@@ -98,6 +116,7 @@ def bak_print(filename, using):
98116
@click.argument("to_where_you_once_belonged",
99117
required=True,
100118
type=click.Path(exists=True))
119+
@check_file_reference()
101120
def bak_get(to_where_you_once_belonged):
102121
to_where_you_once_belonged = Path(to_where_you_once_belonged).expanduser().resolve()
103122
commands.bak_getfile_cmd(to_where_you_once_belonged)
@@ -108,6 +127,7 @@ def bak_get(to_where_you_once_belonged):
108127
@click.option("--using", "--with",
109128
help="Program to use instead of system diff",
110129
required=False)
130+
@check_file_reference()
111131
@click.argument("filename", required=True, type=click.Path(exists=True))
112132
def bak_diff(filename, using):
113133
filename = Path(filename).expanduser().resolve()
@@ -125,11 +145,15 @@ def bak_diff(filename, using):
125145
# help="List a particular file's .bakfiles",
126146
required=False,
127147
type=click.Path(exists=True))
148+
@check_file_reference()
128149
def bak_list(relpaths, filename):
129150
if filename:
130151
filename = Path(filename).expanduser().resolve()
131152
commands.show_bak_list(filename=filename or None, relative_paths=relpaths)
132153

133154

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

0 commit comments

Comments
 (0)