Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions tests/test_restore/cmd/test_end_to_end_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def test_no_file_trashed(self):
def test_original_file_not_existing(self):
self.fake_trash_dir.add_trashinfo3("foo", "/path", datetime(2000,1,1,0,0,1))

result = self.run_command("trash-restore", ["/"], input='0')
result = self.run_command("trash-restore", ["/"])

self.assertEqual(" 0 2000-01-01 00:00:01 /path\n"
"What file to restore [0..0]: \n"
self.assertEqual("2000-01-01 00:00:01 /path\n"
"[Errno 2] No such file or directory: '%s/files/foo'\n" %
self.trash_dir,
result.output())
Expand Down Expand Up @@ -67,11 +66,11 @@ def test_restore_with_relative_path(self):

result = self.run_command("trash-restore",
["%(curdir)s" % {'curdir': "."},
'--sort=path'], input='0')
'--sort=path'])

self.assertEqual("""\
0 2000-01-01 00:00:01 %(curdir)s/path/to/file1
What file to restore [0..0]: """ % {'curdir': self.curdir},
2000-01-01 00:00:01 %(curdir)s/path/to/file1
""" % {'curdir': self.curdir},
result.stdout)
self.assertEqual("", result.stderr)
self.assertEqual("contents", read_file(pj(self.curdir, "path/to/file1")))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_restore/cmd/test_listing_in_restore_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ class FakeHandler(Handler):
def __init__(self, original_locations):
self.original_locations = original_locations

def handle_trashed_files(self, trashed_files, _overwrite):
def handle_trashed_files(self, trashed_files, _overwrite, _no_ask):
for trashed_file in trashed_files:
self.original_locations.append(trashed_file.original_location)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_default_path(self):
args = self.parser.parse_restore_args([''], "curdir")

self.assertEqual(RunRestoreArgs(path='curdir',
path_passed=False,
sort=Sort.ByDate,
trash_dir=None,
overwrite=False),
Expand All @@ -22,6 +23,7 @@ def test_path_specified_relative_path(self):
args = self.parser.parse_restore_args(['', 'path'], "curdir")

self.assertEqual(RunRestoreArgs(path='curdir/path',
path_passed=True,
sort=Sort.ByDate,
trash_dir=None,
overwrite=False),
Expand All @@ -31,6 +33,7 @@ def test_path_specified_fullpath(self):
args = self.parser.parse_restore_args(['', '/a/path'], "ignored")

self.assertEqual(RunRestoreArgs(path='/a/path',
path_passed=True,
sort=Sort.ByDate,
trash_dir=None,
overwrite=False),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test(self):
self.input.set_reply('0')

self.asking_user.restore_asking_the_user(['trashed_file1',
'trashed_file2'], False)
'trashed_file2'], False, False)

self.assertEqual('What file to restore [0..1]: ',
self.input.last_prompt())
Expand All @@ -34,7 +34,7 @@ def test2(self):
self.input.raise_exception(KeyboardInterrupt)

self.asking_user.restore_asking_the_user(['trashed_file1',
'trashed_file2'], False)
'trashed_file2'], False, False)

self.assertEqual('What file to restore [0..1]: ',
self.input.last_prompt())
Expand Down
1 change: 1 addition & 0 deletions trashcli/restore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __repr__(self):
class RunRestoreArgs(
NamedTuple('RunRestoreArgs', [
('path', str),
('path_passed', bool),
('sort', Sort),
('trash_dir', Optional[str]),
('overwrite', bool),
Expand Down
22 changes: 15 additions & 7 deletions trashcli/restore/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ def __init__(self,
def handle_trashed_files(self,
trashed_files, # type: List[TrashedFile]
overwrite, # type: bool
single_no_ask, # type: bool
):
if not trashed_files:
self.report_no_files_found(self.cwd.getcwd_as_realpath())
else:
for i, trashed_file in enumerate(trashed_files):
self.output.println("%4d %s %s" % (i,
trashed_file.deletion_date,
trashed_file.original_location))
self.restore_asking_the_user(trashed_files, overwrite)
if single_no_ask and len(trashed_files) == 1:
trashed_file = trashed_files[0]
self.output.println("%s %s" % (trashed_file.deletion_date,
trashed_file.original_location))
self.restore_asking_the_user(trashed_files, overwrite, no_ask=True)
else:
for i, trashed_file in enumerate(trashed_files):
self.output.println("%4d %s %s" % (i,
trashed_file.deletion_date,
trashed_file.original_location))
self.restore_asking_the_user(trashed_files, overwrite)

def restore_asking_the_user(self, trashed_files, overwrite=False):
def restore_asking_the_user(self, trashed_files, overwrite=False, no_ask=False):
my_output = OutputRecorder()
restore_asking_the_user = RestoreAskingTheUser(self.input,
self.restorer,
my_output)
restore_asking_the_user.restore_asking_the_user(trashed_files,
overwrite)
overwrite,
no_ask)
my_output.apply_to(self.output)

def report_no_files_found(self, directory): # type: (str) -> None
Expand Down
2 changes: 2 additions & 0 deletions trashcli/restore/restore_arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def parse_restore_args(self,
if parsed.version:
return PrintVersionArgs(argv0=sys_argv[0])
else:
path_passed = parsed.path != ""
path = os.path.normpath(
os.path.join(curdir + os.path.sep, parsed.path))

return RunRestoreArgs(path=path,
path_passed=path_passed,
sort=cast(Sort, {
'path': Sort.ByPath,
'date': Sort.ByDate,
Expand Down
11 changes: 10 additions & 1 deletion trashcli/restore/restore_asking_the_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ def read_user_input(self,
return Right(
InputRead(user_input, args.trashed_files, args.overwrite))

def restore_asking_the_user(self, trashed_files, overwrite):
def restore_asking_the_user(self, trashed_files, overwrite, no_ask):
input = Right(Context(trashed_files, overwrite))
compose(input, [
select_all,
self.restore_selected_files,
] if no_ask else [
self.read_user_input,
trashed_files_to_restore,
self.restore_selected_files,
Expand Down Expand Up @@ -138,6 +141,12 @@ def trashed_files_to_restore(input_read, # type: InputRead
return Left(Die("Invalid entry: %s" % e))


def select_all(input_read, # type: InputRead
): # type: (...) -> Either[Die, SelectedFiles]
selected_files = SelectedFiles(input_read.trashed_files, input_read.overwrite)
return Right(selected_files)


class InvalidEntry(Exception):
pass

Expand Down
4 changes: 3 additions & 1 deletion trashcli/restore/run_restore_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def run_action(self, args, # type: RunRestoreArgs
trashed_files = sort_files(args.sort, trashed_files)

self.handler.handle_trashed_files(trashed_files,
args.overwrite)
args.overwrite,
args.path_passed)

def all_files_trashed_from_path(self,
path, # type: str
Expand All @@ -44,6 +45,7 @@ class Handler:
def handle_trashed_files(self,
trashed_files,
overwrite, # type: bool
single_no_ask, # type: bool
):
raise NotImplementedError()

Expand Down