Add YAPF style + ignore, and format tracked Python files

This commit is contained in:
2025-12-29 18:42:02 -08:00
parent c019c00aed
commit 507946a3e4
108 changed files with 11664 additions and 6494 deletions

View File

@@ -13,7 +13,6 @@ from SYS.logger import log, debug
from result_table import ResultTable
import pipeline as ctx
_MATRIX_PENDING_ITEMS_KEY = "matrix_pending_items"
_MATRIX_PENDING_TEXT_KEY = "matrix_pending_text"
@@ -226,11 +225,10 @@ def _extract_url(item: Any) -> Optional[str]:
if isinstance(item, dict):
for key in ("url", "source_url", "path", "target"):
raw = item.get(key)
if (
isinstance(raw, str)
and raw.strip()
and raw.strip().startswith(("http://", "https://"))
):
if (isinstance(raw,
str) and raw.strip() and raw.strip().startswith(
("http://",
"https://"))):
return raw.strip()
except Exception:
pass
@@ -269,15 +267,16 @@ def _extract_hash_from_hydrus_file_url(url: str) -> Optional[str]:
return None
def _maybe_download_hydrus_file(
item: Any, config: Dict[str, Any], output_dir: Path
) -> Optional[str]:
def _maybe_download_hydrus_file(item: Any,
config: Dict[str,
Any],
output_dir: Path) -> Optional[str]:
"""If the item looks like a Hydrus file (hash + Hydrus URL), download it using Hydrus access key headers.
This avoids 401 from Hydrus when the URL is /get_files/file?hash=... without headers.
"""
try:
from config import get_hydrus_access_key, get_hydrus_url
from SYS.config import get_hydrus_access_key, get_hydrus_url
from API.HydrusNetwork import HydrusNetwork as HydrusClient, download_hydrus_file
# Prefer per-item Hydrus instance name when it matches a configured instance.
@@ -319,19 +318,23 @@ def _maybe_download_hydrus_file(
) and _extract_hash_from_hydrus_file_url(url) == file_hash
hydrus_instances: set[str] = set()
try:
store_cfg = (config or {}).get("store") if isinstance(config, dict) else None
store_cfg = (config
or {}).get("store") if isinstance(config,
dict) else None
if isinstance(store_cfg, dict):
hydrus_cfg = store_cfg.get("hydrusnetwork")
if isinstance(hydrus_cfg, dict):
hydrus_instances = {
str(k).strip().lower() for k in hydrus_cfg.keys() if str(k).strip()
str(k).strip().lower()
for k in hydrus_cfg.keys() if str(k).strip()
}
except Exception:
hydrus_instances = set()
store_hint = store_name.lower() in {"hydrus", "hydrusnetwork"} or (
store_name.lower() in hydrus_instances
)
store_hint = store_name.lower() in {
"hydrus",
"hydrusnetwork"
} or (store_name.lower() in hydrus_instances)
if not (is_hydrus_url or store_hint):
return None
@@ -341,8 +344,13 @@ def _maybe_download_hydrus_file(
# Best-effort extension from Hydrus metadata.
suffix = ".hydrus"
try:
meta_response = client.fetch_file_metadata(hashes=[file_hash], include_mime=True)
entries = meta_response.get("metadata") if isinstance(meta_response, dict) else None
meta_response = client.fetch_file_metadata(
hashes=[file_hash],
include_mime=True
)
entries = meta_response.get("metadata"
) if isinstance(meta_response,
dict) else None
if isinstance(entries, list) and entries:
entry = entries[0]
if isinstance(entry, dict):
@@ -362,7 +370,9 @@ def _maybe_download_hydrus_file(
# Avoid clobbering; pick a unique name.
dest = output_dir / f"{file_hash}_{uuid.uuid4().hex[:10]}{suffix}"
headers = {"Hydrus-Client-API-Access-Key": access_key}
headers = {
"Hydrus-Client-API-Access-Key": access_key
}
download_hydrus_file(file_url, headers, dest, timeout=30.0)
if dest.exists():
return str(dest)
@@ -416,9 +426,8 @@ def _resolve_upload_path(item: Any, config: Dict[str, Any]) -> Optional[str]:
if isinstance(config, dict):
base_tmp = config.get("temp")
output_dir = (
Path(str(base_tmp)).expanduser()
if base_tmp
else (Path(tempfile.gettempdir()) / "Medios-Macina")
Path(str(base_tmp)).expanduser() if base_tmp else
(Path(tempfile.gettempdir()) / "Medios-Macina")
)
output_dir = output_dir / "matrix" / "hydrus"
hydrus_path = _maybe_download_hydrus_file(item, config, output_dir)
@@ -441,19 +450,15 @@ def _resolve_upload_path(item: Any, config: Dict[str, Any]) -> Optional[str]:
if isinstance(config, dict):
base_tmp = config.get("temp")
output_dir = (
Path(str(base_tmp)).expanduser()
if base_tmp
else (Path(tempfile.gettempdir()) / "Medios-Macina")
Path(str(base_tmp)).expanduser() if base_tmp else
(Path(tempfile.gettempdir()) / "Medios-Macina")
)
output_dir = output_dir / "matrix"
output_dir.mkdir(parents=True, exist_ok=True)
result = _download_direct_file(url, output_dir, quiet=True)
if (
result
and hasattr(result, "path")
and isinstance(result.path, Path)
and result.path.exists()
):
if (result and hasattr(result,
"path") and isinstance(result.path,
Path) and result.path.exists()):
return str(result.path)
except Exception as exc:
debug(f"[matrix] Failed to download URL for upload: {exc}")
@@ -503,7 +508,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
text_value = _extract_text_arg(args)
if not text_value:
try:
text_value = str(ctx.load_value(_MATRIX_PENDING_TEXT_KEY, default="") or "").strip()
text_value = str(
ctx.load_value(_MATRIX_PENDING_TEXT_KEY,
default="") or ""
).strip()
except Exception:
text_value = ""
@@ -544,7 +552,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
)
continue
upload_jobs.append({"path": str(media_path), "pipe_obj": item})
upload_jobs.append({
"path": str(media_path),
"pipe_obj": item
})
for rid in room_ids:
sent_any_for_room = False
@@ -553,14 +564,21 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
if not file_path:
continue
try:
link = provider.upload_to_room(file_path, rid, pipe_obj=job.get("pipe_obj"))
link = provider.upload_to_room(
file_path,
rid,
pipe_obj=job.get("pipe_obj")
)
debug(f"✓ Sent {Path(file_path).name} -> {rid}")
if link:
log(link)
sent_any_for_room = True
except Exception as exc:
any_failed = True
log(f"Matrix send failed for {Path(file_path).name}: {exc}", file=sys.stderr)
log(
f"Matrix send failed for {Path(file_path).name}: {exc}",
file=sys.stderr
)
# Optional caption-like follow-up message (sent once per room).
if text_value and sent_any_for_room:
@@ -581,7 +599,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
# Default stage: show rooms, then wait for @N selection to resume sending.
selected_items = _normalize_to_list(result)
if not selected_items:
log("Usage: @N | .matrix (select items first, then pick a room)", file=sys.stderr)
log(
"Usage: @N | .matrix (select items first, then pick a room)",
file=sys.stderr
)
return 1
ctx.store_value(_MATRIX_PENDING_ITEMS_KEY, selected_items)
@@ -601,7 +622,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
try:
configured_ids = None
if not _has_flag(args, "-all"):
ids = [str(v).strip() for v in _parse_config_room_filter_ids(config) if str(v).strip()]
ids = [
str(v).strip() for v in _parse_config_room_filter_ids(config)
if str(v).strip()
]
if ids:
configured_ids = ids
@@ -613,7 +637,8 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
# Diagnostics if a configured filter yields no rows (provider filtered before name lookups for speed).
if not rooms and not _has_flag(args, "-all"):
configured_ids_dbg = [
str(v).strip() for v in _parse_config_room_filter_ids(config) if str(v).strip()
str(v).strip() for v in _parse_config_room_filter_ids(config)
if str(v).strip()
]
if configured_ids_dbg:
try:
@@ -640,7 +665,9 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
for room in rooms:
row = table.add_row()
name = str(room.get("name") or "").strip() if isinstance(room, dict) else ""
room_id = str(room.get("room_id") or "").strip() if isinstance(room, dict) else ""
room_id = str(room.get("room_id") or ""
).strip() if isinstance(room,
dict) else ""
row.add_column("Name", name)
row.add_column("Room", room_id)
@@ -669,7 +696,8 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
CMDLET = Cmdlet(
name=".matrix",
alias=["matrix", "rooms"],
alias=["matrix",
"rooms"],
summary="Send selected items to a Matrix room",
usage="@N | .matrix",
arg=[