Skip to content

Commit

Permalink
implement --delete option
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Feb 20, 2024
1 parent e84db08 commit 6767dca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
30 changes: 26 additions & 4 deletions papermerge_cli/lib/importer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import os
from pathlib import Path

from rich.console import Console

from papermerge_cli.rest import create_folder, get_me, upload_document
from papermerge_cli.schema import Folder, User

console = Console()


def upload_file_or_folder(
host: str,
token: str,
file_or_folder: Path,
parent_id=None
parent_id=None,
delete: bool = False
) -> None:
user: User = get_me(host=host, token=token)

Expand All @@ -22,8 +27,11 @@ def upload_file_or_folder(
host=host,
token=token,
file_path=file_or_folder,
parent_id=parent_id
parent_id=parent_id,
)
if delete:
remove(file_or_folder)

return

for entry in os.scandir(file_or_folder):
Expand All @@ -32,8 +40,11 @@ def upload_file_or_folder(
host=host,
token=token,
file_path=Path(entry.path),
parent_id=parent_id
parent_id=parent_id,
)

if delete:
remove(Path(entry.path))
else:
folder_title = Path(entry.path).name

Expand All @@ -47,5 +58,16 @@ def upload_file_or_folder(
host=host,
token=token,
parent_id=folder.id,
file_or_folder=Path(entry.path)
file_or_folder=Path(entry.path),
delete=delete
)


def remove(path: Path):
try:
if path.is_file():
os.remove(path)
else:
os.rmdir(path)
except IOError:
console.print(f"Error while removing {path}", style="red")
23 changes: 14 additions & 9 deletions papermerge_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def import_command(
token=ctx.obj['TOKEN'],
file_or_folder=Path(file_or_folder),
parent_id=target_id,
delete=delete
)
except Exception as ex:
console.print(ex)
Expand All @@ -170,15 +171,19 @@ def list_nodes_command(
If in case no specific node is requested - will list content
of the user's home folder
"""
data: Paginator[Node] = list_nodes(
host=ctx.obj['HOST'],
token=ctx.obj['TOKEN'],
inbox=inbox,
parent_id=parent_id,
page_number=page_number,
page_size=page_size,
order_by=order_by
)
try:
data: Paginator[Node] = list_nodes(
host=ctx.obj['HOST'],
token=ctx.obj['TOKEN'],
inbox=inbox,
parent_id=parent_id,
page_number=page_number,
page_size=page_size,
order_by=order_by
)
except Exception as ex:
console.print(ex, style="red")
return

output: Table = format_nodes.list_nodes(data)
if len(output.rows):
Expand Down

0 comments on commit 6767dca

Please sign in to comment.