1+ import functools
12import os
23from datetime import datetime
34from pathlib import Path
@@ -15,6 +16,26 @@ def __print_help():
1516 click .echo (bak .get_help (ctx ))
1617
1718
19+ def normalize_path (args_key : str = 'filename' ):
20+ def on_decorator (func ):
21+ @functools .wraps (func )
22+ def on_call (* args , ** kwargs ):
23+ try :
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 ):
33+ pass
34+ return func (* args , ** kwargs )
35+ return on_call
36+ return on_decorator
37+
38+
1839basic_help_text = "bak FILENAME (creates a bakfile)\n \n " + \
1940 "See also: bak COMMAND --help"
2041
@@ -25,6 +46,7 @@ def bak():
2546
2647
2748@bak .command ("\0 " , hidden = True )
49+ @normalize_path ()
2850@click .option ("--version" , required = False , is_flag = True )
2951@click .argument ("filename" , required = False , type = click .Path (exists = True ))
3052# Ensures that 'bak --help' is printed if it doesn't get a filename
@@ -39,6 +61,7 @@ def create(filename, version):
3961
4062
4163@bak .command ("up" , help = "Replace a .bakfile with a fresh copy of the parent file" )
64+ @normalize_path ()
4265@click .argument ("filename" , required = True , type = click .Path (exists = True ))
4366def bak_up (filename ):
4467 if not filename :
@@ -85,6 +108,7 @@ def bak_off(filename, quietly):
85108@click .option ("--using" , "--in" , "--with" ,
86109 help = "Program to open (default: $PAGER or less)" ,
87110 required = False , hidden = True )
111+ @normalize_path ()
88112@click .argument ("filename" , required = True , type = click .Path (exists = True ))
89113def bak_print (filename , using ):
90114 filename = Path (filename ).expanduser ().resolve ()
@@ -98,6 +122,7 @@ def bak_print(filename, using):
98122@click .argument ("to_where_you_once_belonged" ,
99123 required = True ,
100124 type = click .Path (exists = True ))
125+ @normalize_path ()
101126def bak_get (to_where_you_once_belonged ):
102127 to_where_you_once_belonged = Path (to_where_you_once_belonged ).expanduser ().resolve ()
103128 commands .bak_getfile_cmd (to_where_you_once_belonged )
@@ -108,6 +133,7 @@ def bak_get(to_where_you_once_belonged):
108133@click .option ("--using" , "--with" ,
109134 help = "Program to use instead of system diff" ,
110135 required = False )
136+ @normalize_path ()
111137@click .argument ("filename" , required = True , type = click .Path (exists = True ))
112138def bak_diff (filename , using ):
113139 filename = Path (filename ).expanduser ().resolve ()
@@ -125,6 +151,7 @@ def bak_diff(filename, using):
125151 # help="List a particular file's .bakfiles",
126152 required = False ,
127153 type = click .Path (exists = True ))
154+ @normalize_path ()
128155def bak_list (relpaths , filename ):
129156 if filename :
130157 filename = Path (filename ).expanduser ().resolve ()
0 commit comments