2025-11-25 20:09:33 -08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any, Dict, Sequence
|
2025-12-01 01:10:16 -08:00
|
|
|
import sys
|
2025-11-25 20:09:33 -08:00
|
|
|
|
2025-12-01 01:10:16 -08:00
|
|
|
import pipeline as ctx
|
2025-12-16 23:23:43 -08:00
|
|
|
from . import _shared as sh
|
|
|
|
|
|
|
|
|
|
Cmdlet, CmdletArg, SharedArgs, parse_cmdlet_args, get_field, normalize_hash = (
|
|
|
|
|
sh.Cmdlet,
|
|
|
|
|
sh.CmdletArg,
|
|
|
|
|
sh.SharedArgs,
|
|
|
|
|
sh.parse_cmdlet_args,
|
|
|
|
|
sh.get_field,
|
|
|
|
|
sh.normalize_hash,
|
|
|
|
|
)
|
2025-12-11 19:04:02 -08:00
|
|
|
from SYS.logger import log
|
|
|
|
|
from Store import Store
|
2025-11-25 20:09:33 -08:00
|
|
|
|
|
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
class Delete_Url(Cmdlet):
|
|
|
|
|
"""Delete URL associations from files via hash+store."""
|
2025-12-14 00:53:52 -08:00
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
super().__init__(
|
|
|
|
|
name="delete-url",
|
|
|
|
|
summary="Remove a URL association from a file",
|
|
|
|
|
usage="@1 | delete-url <url>",
|
|
|
|
|
arg=[
|
|
|
|
|
SharedArgs.HASH,
|
|
|
|
|
SharedArgs.STORE,
|
|
|
|
|
CmdletArg("url", required=True, description="URL to remove"),
|
|
|
|
|
],
|
|
|
|
|
detail=[
|
|
|
|
|
"- Removes URL association from file identified by hash+store",
|
|
|
|
|
"- Multiple url can be comma-separated",
|
|
|
|
|
],
|
|
|
|
|
exec=self.run,
|
|
|
|
|
)
|
|
|
|
|
self.register()
|
2025-11-25 20:09:33 -08:00
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
def run(self, result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
|
|
|
|
"""Delete URL from file via hash+store backend."""
|
|
|
|
|
parsed = parse_cmdlet_args(args, self)
|
2025-12-01 01:10:16 -08:00
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
# Extract hash and store from result or args
|
|
|
|
|
file_hash = parsed.get("hash") or get_field(result, "hash")
|
|
|
|
|
store_name = parsed.get("store") or get_field(result, "store")
|
|
|
|
|
url_arg = parsed.get("url")
|
2025-12-01 01:10:16 -08:00
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
if not file_hash:
|
|
|
|
|
log("Error: No file hash provided")
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if not store_name:
|
|
|
|
|
log("Error: No store name provided")
|
|
|
|
|
return 1
|
|
|
|
|
|
2025-12-01 01:10:16 -08:00
|
|
|
if not url_arg:
|
2025-12-11 12:47:30 -08:00
|
|
|
log("Error: No URL provided")
|
|
|
|
|
return 1
|
2025-12-01 01:10:16 -08:00
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
# Normalize hash
|
|
|
|
|
file_hash = normalize_hash(file_hash)
|
|
|
|
|
if not file_hash:
|
|
|
|
|
log("Error: Invalid hash format")
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
# Parse url (comma-separated)
|
2025-12-11 19:04:02 -08:00
|
|
|
urls = [u.strip() for u in str(url_arg).split(',') if u.strip()]
|
|
|
|
|
if not urls:
|
2025-12-11 12:47:30 -08:00
|
|
|
log("Error: No valid url provided")
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
# Get backend and delete url
|
2025-12-01 01:10:16 -08:00
|
|
|
try:
|
2025-12-11 19:04:02 -08:00
|
|
|
storage = Store(config)
|
2025-12-11 12:47:30 -08:00
|
|
|
backend = storage[store_name]
|
2025-12-11 19:04:02 -08:00
|
|
|
|
|
|
|
|
backend.delete_url(file_hash, urls)
|
|
|
|
|
for u in urls:
|
|
|
|
|
ctx.emit(f"Deleted URL: {u}")
|
2025-12-11 12:47:30 -08:00
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
log(f"Error: Storage backend '{store_name}' not configured")
|
|
|
|
|
return 1
|
2025-12-01 01:10:16 -08:00
|
|
|
except Exception as exc:
|
2025-12-11 12:47:30 -08:00
|
|
|
log(f"Error deleting URL: {exc}", file=sys.stderr)
|
|
|
|
|
return 1
|
2025-12-06 00:10:19 -08:00
|
|
|
|
|
|
|
|
|
2025-12-14 00:53:52 -08:00
|
|
|
CMDLET = Delete_Url()
|