Add YAPF style + ignore, and format tracked Python files
This commit is contained in:
81
SYS/utils.py
81
SYS/utils.py
@@ -123,7 +123,9 @@ def create_metadata_sidecar(file_path: Path, metadata: dict) -> None:
|
||||
with open(metadata_path, "w", encoding="utf-8") as f:
|
||||
json.dump(metadata, f, ensure_ascii=False, indent=2)
|
||||
except OSError as exc:
|
||||
raise RuntimeError(f"Failed to write metadata sidecar {metadata_path}: {exc}") from exc
|
||||
raise RuntimeError(
|
||||
f"Failed to write metadata sidecar {metadata_path}: {exc}"
|
||||
) from exc
|
||||
|
||||
|
||||
def create_tags_sidecar(file_path: Path, tags: set) -> None:
|
||||
@@ -194,7 +196,8 @@ def ffprobe(file_path: str) -> dict:
|
||||
return {}
|
||||
|
||||
metadata = {}
|
||||
fmt = probe.get("format", {})
|
||||
fmt = probe.get("format",
|
||||
{})
|
||||
metadata["duration"] = float(fmt.get("duration", 0)) if "duration" in fmt else None
|
||||
metadata["size"] = int(fmt.get("size", 0)) if "size" in fmt else None
|
||||
metadata["format_name"] = fmt.get("format_name", None)
|
||||
@@ -204,19 +207,38 @@ def ffprobe(file_path: str) -> dict:
|
||||
codec_type = stream.get("codec_type")
|
||||
if codec_type == "audio":
|
||||
metadata["audio_codec"] = stream.get("codec_name")
|
||||
metadata["bitrate"] = int(stream.get("bit_rate", 0)) if "bit_rate" in stream else None
|
||||
metadata["bitrate"] = int(
|
||||
stream.get("bit_rate",
|
||||
0)
|
||||
) if "bit_rate" in stream else None
|
||||
metadata["samplerate"] = (
|
||||
int(stream.get("sample_rate", 0)) if "sample_rate" in stream else None
|
||||
int(stream.get("sample_rate",
|
||||
0)) if "sample_rate" in stream else None
|
||||
)
|
||||
metadata["channels"] = int(stream.get("channels", 0)) if "channels" in stream else None
|
||||
metadata["channels"] = int(
|
||||
stream.get("channels",
|
||||
0)
|
||||
) if "channels" in stream else None
|
||||
elif codec_type == "video":
|
||||
metadata["video_codec"] = stream.get("codec_name")
|
||||
metadata["width"] = int(stream.get("width", 0)) if "width" in stream else None
|
||||
metadata["height"] = int(stream.get("height", 0)) if "height" in stream else None
|
||||
metadata["width"] = int(
|
||||
stream.get("width",
|
||||
0)
|
||||
) if "width" in stream else None
|
||||
metadata["height"] = int(
|
||||
stream.get("height",
|
||||
0)
|
||||
) if "height" in stream else None
|
||||
elif codec_type == "image":
|
||||
metadata["image_codec"] = stream.get("codec_name")
|
||||
metadata["width"] = int(stream.get("width", 0)) if "width" in stream else None
|
||||
metadata["height"] = int(stream.get("height", 0)) if "height" in stream else None
|
||||
metadata["width"] = int(
|
||||
stream.get("width",
|
||||
0)
|
||||
) if "width" in stream else None
|
||||
metadata["height"] = int(
|
||||
stream.get("height",
|
||||
0)
|
||||
) if "height" in stream else None
|
||||
|
||||
return metadata
|
||||
|
||||
@@ -239,11 +261,16 @@ def decode_cbor(data: bytes) -> Any:
|
||||
def jsonify(value: Any) -> Any:
|
||||
"""Convert *value* into a JSON-friendly structure."""
|
||||
if isinstance(value, dict):
|
||||
return {str(key): jsonify(val) for key, val in value.items()}
|
||||
return {
|
||||
str(key): jsonify(val)
|
||||
for key, val in value.items()
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [jsonify(item) for item in value]
|
||||
if isinstance(value, bytes):
|
||||
return {"__bytes__": base64.b64encode(value).decode("ascii")}
|
||||
return {
|
||||
"__bytes__": base64.b64encode(value).decode("ascii")
|
||||
}
|
||||
return value
|
||||
|
||||
|
||||
@@ -362,12 +389,12 @@ def format_metadata_value(key: str, value) -> str:
|
||||
elif key in ("duration", "length"):
|
||||
return format_duration(value)
|
||||
elif key in (
|
||||
"time_modified",
|
||||
"time_imported",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"indexed_at",
|
||||
"timestamp",
|
||||
"time_modified",
|
||||
"time_imported",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"indexed_at",
|
||||
"timestamp",
|
||||
):
|
||||
return format_timestamp(value)
|
||||
else:
|
||||
@@ -413,9 +440,13 @@ def extract_link_from_result(result: Any) -> Any | None:
|
||||
return result.get("url") or result.get("link") or result.get("href")
|
||||
|
||||
return (
|
||||
getattr(result, "url", None)
|
||||
or getattr(result, "link", None)
|
||||
or getattr(result, "href", None)
|
||||
getattr(result,
|
||||
"url",
|
||||
None) or getattr(result,
|
||||
"link",
|
||||
None) or getattr(result,
|
||||
"href",
|
||||
None)
|
||||
)
|
||||
|
||||
|
||||
@@ -466,7 +497,11 @@ def get_api_key(config: dict[str, Any], service: str, key_path: str) -> str | No
|
||||
return None
|
||||
|
||||
|
||||
def add_direct_link_to_result(result: Any, direct_link: str, original_link: str) -> None:
|
||||
def add_direct_link_to_result(
|
||||
result: Any,
|
||||
direct_link: str,
|
||||
original_link: str
|
||||
) -> None:
|
||||
"""Add direct link information to result object.
|
||||
|
||||
Args:
|
||||
@@ -515,7 +550,9 @@ def _normalise_rule(rule: dict[str, Any]) -> dict[str, Any] | None:
|
||||
force_screenshot = bool(rule.get("force_screenshot"))
|
||||
extra_tags_raw = rule.get("extra_tags")
|
||||
if isinstance(extra_tags_raw, str):
|
||||
extra_tags = [part.strip() for part in extra_tags_raw.split(",") if part.strip()]
|
||||
extra_tags = [
|
||||
part.strip() for part in extra_tags_raw.split(",") if part.strip()
|
||||
]
|
||||
elif isinstance(extra_tags_raw, (list, tuple, set)):
|
||||
extra_tags = [str(item).strip() for item in extra_tags_raw if str(item).strip()]
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user