Skip to content

Commit 6767dca

Browse files
committed
implement --delete option
1 parent e84db08 commit 6767dca

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

papermerge_cli/lib/importer.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import os
22
from pathlib import Path
33

4+
from rich.console import Console
5+
46
from papermerge_cli.rest import create_folder, get_me, upload_document
57
from papermerge_cli.schema import Folder, User
68

9+
console = Console()
10+
711

812
def upload_file_or_folder(
913
host: str,
1014
token: str,
1115
file_or_folder: Path,
12-
parent_id=None
16+
parent_id=None,
17+
delete: bool = False
1318
) -> None:
1419
user: User = get_me(host=host, token=token)
1520

@@ -22,8 +27,11 @@ def upload_file_or_folder(
2227
host=host,
2328
token=token,
2429
file_path=file_or_folder,
25-
parent_id=parent_id
30+
parent_id=parent_id,
2631
)
32+
if delete:
33+
remove(file_or_folder)
34+
2735
return
2836

2937
for entry in os.scandir(file_or_folder):
@@ -32,8 +40,11 @@ def upload_file_or_folder(
3240
host=host,
3341
token=token,
3442
file_path=Path(entry.path),
35-
parent_id=parent_id
43+
parent_id=parent_id,
3644
)
45+
46+
if delete:
47+
remove(Path(entry.path))
3748
else:
3849
folder_title = Path(entry.path).name
3950

@@ -47,5 +58,16 @@ def upload_file_or_folder(
4758
host=host,
4859
token=token,
4960
parent_id=folder.id,
50-
file_or_folder=Path(entry.path)
61+
file_or_folder=Path(entry.path),
62+
delete=delete
5163
)
64+
65+
66+
def remove(path: Path):
67+
try:
68+
if path.is_file():
69+
os.remove(path)
70+
else:
71+
os.rmdir(path)
72+
except IOError:
73+
console.print(f"Error while removing {path}", style="red")

papermerge_cli/main.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def import_command(
151151
token=ctx.obj['TOKEN'],
152152
file_or_folder=Path(file_or_folder),
153153
parent_id=target_id,
154+
delete=delete
154155
)
155156
except Exception as ex:
156157
console.print(ex)
@@ -170,15 +171,19 @@ def list_nodes_command(
170171
If in case no specific node is requested - will list content
171172
of the user's home folder
172173
"""
173-
data: Paginator[Node] = list_nodes(
174-
host=ctx.obj['HOST'],
175-
token=ctx.obj['TOKEN'],
176-
inbox=inbox,
177-
parent_id=parent_id,
178-
page_number=page_number,
179-
page_size=page_size,
180-
order_by=order_by
181-
)
174+
try:
175+
data: Paginator[Node] = list_nodes(
176+
host=ctx.obj['HOST'],
177+
token=ctx.obj['TOKEN'],
178+
inbox=inbox,
179+
parent_id=parent_id,
180+
page_number=page_number,
181+
page_size=page_size,
182+
order_by=order_by
183+
)
184+
except Exception as ex:
185+
console.print(ex, style="red")
186+
return
182187

183188
output: Table = format_nodes.list_nodes(data)
184189
if len(output.rows):

0 commit comments

Comments
 (0)