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

Commit 5afd02f

Browse files
Merge pull request #48 from Serene-Arc/bug_fix_25
closes #25
2 parents d7e65ea + 3267aad commit 5afd02f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

bak/__main__.py

Lines changed: 27 additions & 0 deletions
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,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+
1839
basic_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))
4366
def 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))
89113
def 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()
101126
def 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))
112138
def 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()
128155
def bak_list(relpaths, filename):
129156
if filename:
130157
filename = Path(filename).expanduser().resolve()

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)