File tree Expand file tree Collapse file tree 2 files changed +40
-13
lines changed Expand file tree Collapse file tree 2 files changed +40
-13
lines changed Original file line number Diff line number Diff line change 1
1
import os
2
2
from pathlib import Path
3
3
4
+ from rich .console import Console
5
+
4
6
from papermerge_cli .rest import create_folder , get_me , upload_document
5
7
from papermerge_cli .schema import Folder , User
6
8
9
+ console = Console ()
10
+
7
11
8
12
def upload_file_or_folder (
9
13
host : str ,
10
14
token : str ,
11
15
file_or_folder : Path ,
12
- parent_id = None
16
+ parent_id = None ,
17
+ delete : bool = False
13
18
) -> None :
14
19
user : User = get_me (host = host , token = token )
15
20
@@ -22,8 +27,11 @@ def upload_file_or_folder(
22
27
host = host ,
23
28
token = token ,
24
29
file_path = file_or_folder ,
25
- parent_id = parent_id
30
+ parent_id = parent_id ,
26
31
)
32
+ if delete :
33
+ remove (file_or_folder )
34
+
27
35
return
28
36
29
37
for entry in os .scandir (file_or_folder ):
@@ -32,8 +40,11 @@ def upload_file_or_folder(
32
40
host = host ,
33
41
token = token ,
34
42
file_path = Path (entry .path ),
35
- parent_id = parent_id
43
+ parent_id = parent_id ,
36
44
)
45
+
46
+ if delete :
47
+ remove (Path (entry .path ))
37
48
else :
38
49
folder_title = Path (entry .path ).name
39
50
@@ -47,5 +58,16 @@ def upload_file_or_folder(
47
58
host = host ,
48
59
token = token ,
49
60
parent_id = folder .id ,
50
- file_or_folder = Path (entry .path )
61
+ file_or_folder = Path (entry .path ),
62
+ delete = delete
51
63
)
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" )
Original file line number Diff line number Diff line change @@ -151,6 +151,7 @@ def import_command(
151
151
token = ctx .obj ['TOKEN' ],
152
152
file_or_folder = Path (file_or_folder ),
153
153
parent_id = target_id ,
154
+ delete = delete
154
155
)
155
156
except Exception as ex :
156
157
console .print (ex )
@@ -170,15 +171,19 @@ def list_nodes_command(
170
171
If in case no specific node is requested - will list content
171
172
of the user's home folder
172
173
"""
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
182
187
183
188
output : Table = format_nodes .list_nodes (data )
184
189
if len (output .rows ):
You can’t perform that action at this time.
0 commit comments