refactor: replace inline JSON reads with read_json_safe in resolver, civitai, and huggingface
This commit is contained in:
+7
-13
@@ -43,6 +43,7 @@ from .path_utils import (
|
||||
prefer_local_base_directory,
|
||||
dedupe_local_base_directories,
|
||||
get_filename_from_path,
|
||||
read_json_safe,
|
||||
)
|
||||
|
||||
|
||||
@@ -438,11 +439,9 @@ def search_local_matches_by_hash(
|
||||
if not metadata_path:
|
||||
continue
|
||||
|
||||
try:
|
||||
with open(metadata_path, "r", encoding="utf-8") as handle:
|
||||
metadata = json.load(handle)
|
||||
except Exception as exc:
|
||||
log.debug(f"Could not read metadata sidecar for hash match: {metadata_path} ({exc})")
|
||||
metadata = read_json_safe(metadata_path, None)
|
||||
if not isinstance(metadata, dict) or not metadata:
|
||||
log.debug(f"Could not read metadata sidecar for hash match: {metadata_path}")
|
||||
continue
|
||||
|
||||
metadata_hashes = _extract_model_sha256_from_metadata(metadata, model)
|
||||
@@ -505,14 +504,9 @@ def get_local_model_hash_metadata(
|
||||
if not first_metadata_path:
|
||||
first_metadata_path = metadata_path
|
||||
|
||||
try:
|
||||
with open(metadata_path, "r", encoding="utf-8") as handle:
|
||||
metadata = json.load(handle)
|
||||
except Exception as exc:
|
||||
log.debug(f"Could not read metadata sidecar for local hash lookup: {metadata_path} ({exc})")
|
||||
continue
|
||||
|
||||
if not isinstance(metadata, dict):
|
||||
metadata = read_json_safe(metadata_path, None)
|
||||
if not isinstance(metadata, dict) or not metadata:
|
||||
log.debug(f"Could not read metadata sidecar for local hash lookup: {metadata_path}")
|
||||
continue
|
||||
|
||||
last_hash_status = str(metadata.get("hash_status") or "").strip()
|
||||
|
||||
+4
-10
@@ -32,7 +32,7 @@ from ..type_utils import (
|
||||
normalize_model_image,
|
||||
)
|
||||
from ..progress import report_progress, get_progress_reporter
|
||||
from ..path_utils import calculate_file_sha256, get_filename_from_path
|
||||
from ..path_utils import calculate_file_sha256, get_filename_from_path, read_json_safe
|
||||
from ..log_system import create_module_logger
|
||||
log = create_module_logger(__name__)
|
||||
|
||||
@@ -1498,17 +1498,11 @@ def _read_model_metadata(metadata_path: str) -> Optional[Dict[str, Any]]:
|
||||
Read model metadata from a JSON file.
|
||||
Returns the metadata if found and valid, None otherwise.
|
||||
"""
|
||||
if not metadata_path or not os.path.exists(metadata_path):
|
||||
data = read_json_safe(metadata_path, None)
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(metadata_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Validate it's a valid metadata file with CivitAI info
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
|
||||
files = _as_metadata_list(data.get("files"))
|
||||
file_hashes = [
|
||||
_as_metadata_dict(file_info).get("hashes")
|
||||
@@ -1542,7 +1536,7 @@ def _read_model_metadata(metadata_path: str) -> Optional[Dict[str, Any]]:
|
||||
log.info(f"Successfully read metadata from: {metadata_path}")
|
||||
return data
|
||||
except Exception as e:
|
||||
log.debug(f"Error reading metadata file {metadata_path}: {e}")
|
||||
log.debug(f"Error parsing metadata file {metadata_path}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ from ..progress import report_progress, get_progress_reporter
|
||||
from ..log_system import create_module_logger
|
||||
log = create_module_logger(__name__)
|
||||
|
||||
from ..path_utils import write_json_atomic, METADATA_DIR
|
||||
from ..path_utils import write_json_atomic, METADATA_DIR, read_json_safe
|
||||
from ..type_utils import check_credential_http, parse_size_header as _parse_size_header, fetch_remote_file_size
|
||||
|
||||
HF_API_URL = "https://huggingface.co/api"
|
||||
@@ -133,20 +133,13 @@ def _is_author_index_fresh(index: Optional[Dict[str, Any]]) -> bool:
|
||||
|
||||
|
||||
def _read_persistent_author_indexes() -> Dict[str, Any]:
|
||||
if not os.path.exists(HF_AUTHOR_INDEX_CACHE_PATH):
|
||||
return {"version": HF_AUTHOR_INDEX_CACHE_VERSION, "authors": {}}
|
||||
|
||||
try:
|
||||
with open(HF_AUTHOR_INDEX_CACHE_PATH, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
if data.get("version") != HF_AUTHOR_INDEX_CACHE_VERSION:
|
||||
return {"version": HF_AUTHOR_INDEX_CACHE_VERSION, "authors": {}}
|
||||
if not isinstance(data.get("authors"), dict):
|
||||
data["authors"] = {}
|
||||
return data
|
||||
except Exception as e:
|
||||
log.debug(f"Error reading HuggingFace author index cache: {e}")
|
||||
default_val = {"version": HF_AUTHOR_INDEX_CACHE_VERSION, "authors": {}}
|
||||
data = read_json_safe(HF_AUTHOR_INDEX_CACHE_PATH, default_val)
|
||||
if not isinstance(data, dict) or data.get("version") != HF_AUTHOR_INDEX_CACHE_VERSION:
|
||||
return {"version": HF_AUTHOR_INDEX_CACHE_VERSION, "authors": {}}
|
||||
if not isinstance(data.get("authors"), dict):
|
||||
data["authors"] = {}
|
||||
return data
|
||||
|
||||
|
||||
def _write_persistent_author_index(author: str, index: Dict[str, Any]) -> None:
|
||||
|
||||
Reference in New Issue
Block a user