Skip to content

Commit

Permalink
implement --delete option (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur authored Feb 20, 2024
1 parent e84db08 commit 226ca9d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ can be found in [changelog.d folder](https://github.com/papermerge/papermerge-cl

<!-- towncrier release notes start -->

## 0.7.1 - 2024-02-20

### Fixed

- papermerge-cli import option --delete without any function [Issue#592](https://github.com/ciur/papermerge/issues/592)


## 0.7.0 - 2023-12-22

- pydantic dependency upgraded from 1.x to v2.5
Expand Down
28 changes: 26 additions & 2 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 @@ -24,6 +29,9 @@ def upload_file_or_folder(
file_path=file_or_folder,
parent_id=parent_id
)
if delete:
remove(file_or_folder)

return

for entry in os.scandir(file_or_folder):
Expand All @@ -34,6 +42,9 @@ def upload_file_or_folder(
file_path=Path(entry.path),
parent_id=parent_id
)

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

Expand All @@ -47,5 +58,18 @@ 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
)
if delete:
remove(Path(entry.path))


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
4 changes: 2 additions & 2 deletions papermerge_cli/schema/paginator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections.abc import Sequence
from typing import Generic, TypeVar

from pydantic.generics import GenericModel
from pydantic import BaseModel

T = TypeVar('T')


class Paginator(GenericModel, Generic[T]):
class Paginator(BaseModel, Generic[T]):
page_size: int
page_number: int
num_pages: int
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "papermerge-cli"
version = "0.7.0"
version = "0.7.1"
description = "Command line utility for your Papermerge DMS instance"
authors = ["Eugen Ciur <[email protected]>"]
license = "Apache 2.0"
Expand Down

0 comments on commit 226ca9d

Please sign in to comment.