dfdfsdd
This commit is contained in:
@@ -1,28 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Sequence
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, List, Sequence
|
||||
import sys
|
||||
|
||||
from . import register
|
||||
import pipeline as ctx
|
||||
from ._shared import Cmdlet, CmdletArg, SharedArgs, parse_cmdlet_args, get_field, normalize_hash
|
||||
from ._shared import Cmdlet, SharedArgs, parse_cmdlet_args, get_field, normalize_hash
|
||||
from SYS.logger import log
|
||||
from Store import Store
|
||||
|
||||
|
||||
@dataclass
|
||||
class UrlItem:
|
||||
url: str
|
||||
hash: str
|
||||
store: str
|
||||
|
||||
|
||||
class Get_Url(Cmdlet):
|
||||
"""Get url associated with files via hash+store."""
|
||||
|
||||
NAME = "get-url"
|
||||
SUMMARY = "List url associated with a file"
|
||||
USAGE = "@1 | get-url"
|
||||
ARGS = [
|
||||
SharedArgs.HASH,
|
||||
SharedArgs.STORE,
|
||||
]
|
||||
DETAIL = [
|
||||
"- Lists all url associated with file identified by hash+store",
|
||||
]
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(
|
||||
name="get-url",
|
||||
summary="List url associated with a file",
|
||||
usage="@1 | get-url",
|
||||
arg=[
|
||||
SharedArgs.HASH,
|
||||
SharedArgs.STORE,
|
||||
],
|
||||
detail=[
|
||||
"- Lists all url associated with file identified by hash+store",
|
||||
],
|
||||
exec=self.run,
|
||||
)
|
||||
self.register()
|
||||
|
||||
def run(self, result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
"""Get url for file via hash+store backend."""
|
||||
@@ -53,18 +65,34 @@ class Get_Url(Cmdlet):
|
||||
|
||||
urls = backend.get_url(file_hash)
|
||||
|
||||
if urls:
|
||||
for u in urls:
|
||||
# Emit rich object for pipeline compatibility
|
||||
ctx.emit({
|
||||
"url": u,
|
||||
"hash": file_hash,
|
||||
"store": store_name,
|
||||
})
|
||||
return 0
|
||||
else:
|
||||
ctx.emit("No url found")
|
||||
return 0
|
||||
from result_table import ResultTable
|
||||
|
||||
title = str(get_field(result, "title") or "").strip()
|
||||
table_title = "Title"
|
||||
if title:
|
||||
table_title = f"Title: {title}"
|
||||
|
||||
table = ResultTable(table_title, max_columns=1).set_preserve_order(True)
|
||||
table.set_source_command("get-url", [])
|
||||
|
||||
items: List[UrlItem] = []
|
||||
for u in list(urls or []):
|
||||
u = str(u or "").strip()
|
||||
if not u:
|
||||
continue
|
||||
row = table.add_row()
|
||||
row.add_column("Url", u)
|
||||
item = UrlItem(url=u, hash=file_hash, store=str(store_name))
|
||||
items.append(item)
|
||||
ctx.emit(item)
|
||||
|
||||
# Make this a real result table so @.. / @,, can navigate it
|
||||
ctx.set_last_result_table(table if items else None, items, subject=result)
|
||||
|
||||
if not items:
|
||||
log("No url found", file=sys.stderr)
|
||||
|
||||
return 0
|
||||
|
||||
except KeyError:
|
||||
log(f"Error: Storage backend '{store_name}' not configured")
|
||||
@@ -74,7 +102,6 @@ class Get_Url(Cmdlet):
|
||||
return 1
|
||||
|
||||
|
||||
# Register cmdlet
|
||||
register(["get-url", "get_url"])(Get_Url)
|
||||
CMDLET = Get_Url()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user