80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any, Dict, Sequence
|
||
|
|
import json
|
||
|
|
|
||
|
|
from helper import hydrus as hydrus_wrapper
|
||
|
|
from ._shared import Cmdlet, CmdletArg, normalize_hash
|
||
|
|
from helper.logger import log
|
||
|
|
|
||
|
|
CMDLET = Cmdlet(
|
||
|
|
name="delete-note",
|
||
|
|
summary="Delete a named note from a Hydrus file.",
|
||
|
|
usage="i | del-note [-hash <sha256>] <name>",
|
||
|
|
aliases=["del-note"],
|
||
|
|
args=[
|
||
|
|
|
||
|
|
],
|
||
|
|
details=[
|
||
|
|
"- Removes the note with the given name from the Hydrus file.",
|
||
|
|
],
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||
|
|
# Help
|
||
|
|
try:
|
||
|
|
if any(str(a).lower() in {"-?", "/?", "--help", "-h", "help", "--cmdlet"} for a in args):
|
||
|
|
log(json.dumps(CMDLET, ensure_ascii=False, indent=2))
|
||
|
|
return 0
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
if not args:
|
||
|
|
log("Requires the note name/key to delete")
|
||
|
|
return 1
|
||
|
|
override_hash: str | None = None
|
||
|
|
rest: list[str] = []
|
||
|
|
i = 0
|
||
|
|
while i < len(args):
|
||
|
|
a = args[i]
|
||
|
|
low = str(a).lower()
|
||
|
|
if low in {"-hash", "--hash", "hash"} and i + 1 < len(args):
|
||
|
|
override_hash = str(args[i + 1]).strip()
|
||
|
|
i += 2
|
||
|
|
continue
|
||
|
|
rest.append(a)
|
||
|
|
i += 1
|
||
|
|
if not rest:
|
||
|
|
log("Requires the note name/key to delete")
|
||
|
|
return 1
|
||
|
|
name = str(rest[0] or '').strip()
|
||
|
|
if not name:
|
||
|
|
log("Requires a non-empty note name/key")
|
||
|
|
return 1
|
||
|
|
|
||
|
|
# Handle @N selection which creates a list - extract the first item
|
||
|
|
if isinstance(result, list) and len(result) > 0:
|
||
|
|
result = result[0]
|
||
|
|
|
||
|
|
hash_hex = normalize_hash(override_hash) if override_hash else normalize_hash(getattr(result, "hash_hex", None))
|
||
|
|
if not hash_hex:
|
||
|
|
log("Selected result does not include a Hydrus hash")
|
||
|
|
return 1
|
||
|
|
try:
|
||
|
|
client = hydrus_wrapper.get_client(config)
|
||
|
|
except Exception as exc:
|
||
|
|
log(f"Hydrus client unavailable: {exc}")
|
||
|
|
return 1
|
||
|
|
|
||
|
|
if client is None:
|
||
|
|
log("Hydrus client unavailable")
|
||
|
|
return 1
|
||
|
|
try:
|
||
|
|
service_name = "my notes"
|
||
|
|
client.delete_notes(hash_hex, [name], service_name)
|
||
|
|
except Exception as exc:
|
||
|
|
log(f"Hydrus delete-note failed: {exc}")
|
||
|
|
return 1
|
||
|
|
log(f"Deleted note '{name}'")
|
||
|
|
return 0
|