df
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
This commit is contained in:
@@ -26,7 +26,7 @@ class Get_Metadata(Cmdlet):
|
||||
super().__init__(
|
||||
name="get-metadata",
|
||||
summary="Print metadata for files by hash and storage backend.",
|
||||
usage="get-metadata [-query \"hash:<sha256>\"] [-store <backend>]",
|
||||
usage='get-metadata [-query "hash:<sha256>"] [-store <backend>]',
|
||||
alias=["meta"],
|
||||
arg=[
|
||||
SharedArgs.QUERY,
|
||||
@@ -52,15 +52,16 @@ class Get_Metadata(Cmdlet):
|
||||
explicit = meta.get("time_imported")
|
||||
if isinstance(explicit, (int, float)):
|
||||
return int(explicit)
|
||||
|
||||
|
||||
# Try parsing string timestamps
|
||||
if isinstance(explicit, str):
|
||||
try:
|
||||
import datetime as _dt
|
||||
|
||||
return int(_dt.datetime.fromisoformat(explicit).timestamp())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
@@ -70,14 +71,24 @@ class Get_Metadata(Cmdlet):
|
||||
return ""
|
||||
try:
|
||||
import datetime as _dt
|
||||
|
||||
return _dt.datetime.utcfromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _build_table_row(title: str, store: str, path: str, mime: str, size_bytes: Optional[int],
|
||||
dur_seconds: Optional[int], imported_ts: Optional[int], url: list[str],
|
||||
hash_value: Optional[str], pages: Optional[int] = None) -> Dict[str, Any]:
|
||||
def _build_table_row(
|
||||
title: str,
|
||||
store: str,
|
||||
path: str,
|
||||
mime: str,
|
||||
size_bytes: Optional[int],
|
||||
dur_seconds: Optional[int],
|
||||
imported_ts: Optional[int],
|
||||
url: list[str],
|
||||
hash_value: Optional[str],
|
||||
pages: Optional[int] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""Build a table row dict with metadata fields."""
|
||||
size_mb = None
|
||||
size_int: Optional[int] = None
|
||||
@@ -156,34 +167,38 @@ class Get_Metadata(Cmdlet):
|
||||
|
||||
query_hash = sh.parse_single_hash_query(parsed.get("query"))
|
||||
if parsed.get("query") and not query_hash:
|
||||
log("No hash available - use -query \"hash:<sha256>\"", file=sys.stderr)
|
||||
log('No hash available - use -query "hash:<sha256>"', file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
# Get hash and store from parsed args or result
|
||||
file_hash = query_hash or get_field(result, "hash")
|
||||
storage_source = parsed.get("store") or get_field(result, "store")
|
||||
|
||||
|
||||
if not file_hash:
|
||||
log("No hash available - use -query \"hash:<sha256>\"", file=sys.stderr)
|
||||
log('No hash available - use -query "hash:<sha256>"', file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
if not storage_source:
|
||||
log("No storage backend specified - use -store to specify", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
# Use storage backend to get metadata
|
||||
try:
|
||||
from Store import Store
|
||||
|
||||
storage = Store(config)
|
||||
backend = storage[storage_source]
|
||||
|
||||
|
||||
# Get metadata from backend
|
||||
metadata = backend.get_metadata(file_hash)
|
||||
|
||||
|
||||
if not metadata:
|
||||
log(f"No metadata found for hash {file_hash[:8]}... in {storage_source}", file=sys.stderr)
|
||||
log(
|
||||
f"No metadata found for hash {file_hash[:8]}... in {storage_source}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
|
||||
# Extract title from tags if available
|
||||
title = get_field(result, "title") or file_hash[:16]
|
||||
if not get_field(result, "title"):
|
||||
@@ -196,7 +211,7 @@ class Get_Metadata(Cmdlet):
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# Extract metadata fields
|
||||
mime_type = metadata.get("mime") or metadata.get("ext", "")
|
||||
file_size = metadata.get("size")
|
||||
@@ -224,13 +239,15 @@ class Get_Metadata(Cmdlet):
|
||||
if len(nums) == 2:
|
||||
duration_seconds = float(nums[0] * 60 + nums[1])
|
||||
else:
|
||||
duration_seconds = float(nums[0] * 3600 + nums[1] * 60 + nums[2])
|
||||
duration_seconds = float(
|
||||
nums[0] * 3600 + nums[1] * 60 + nums[2]
|
||||
)
|
||||
else:
|
||||
duration_seconds = None
|
||||
pages = metadata.get("pages")
|
||||
url = metadata.get("url") or []
|
||||
imported_ts = self._extract_imported_ts(metadata)
|
||||
|
||||
|
||||
# Normalize url
|
||||
if isinstance(url, str):
|
||||
try:
|
||||
@@ -239,7 +256,7 @@ class Get_Metadata(Cmdlet):
|
||||
url = []
|
||||
if not isinstance(url, list):
|
||||
url = []
|
||||
|
||||
|
||||
# Build display row
|
||||
row = self._build_table_row(
|
||||
title=title,
|
||||
@@ -253,14 +270,14 @@ class Get_Metadata(Cmdlet):
|
||||
hash_value=file_hash,
|
||||
pages=pages,
|
||||
)
|
||||
|
||||
|
||||
table_title = f"get-metadata: {title}" if title else "get-metadata"
|
||||
table = ResultTable(table_title).init_command(table_title, "get-metadata", list(args))
|
||||
self._add_table_body_row(table, row)
|
||||
ctx.set_last_result_table_overlay(table, [row], row)
|
||||
ctx.emit(row)
|
||||
return 0
|
||||
|
||||
|
||||
except KeyError:
|
||||
log(f"Storage backend '{storage_source}' not found", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user