refactor: reuse model path identity
This commit is contained in:
@@ -24,13 +24,7 @@ def model_identity_key(model: Dict[str, Any]) -> str:
|
||||
model_path = str(model.get("path") or "").strip()
|
||||
if not model_path:
|
||||
return ""
|
||||
try:
|
||||
return get_path_identity(model_path)
|
||||
except (OSError, ValueError):
|
||||
try:
|
||||
return os.path.normcase(os.path.abspath(model_path))
|
||||
except (OSError, ValueError):
|
||||
return os.path.normcase(model_path)
|
||||
return get_path_identity(model_path)
|
||||
|
||||
|
||||
def dedupe_models(models: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
|
||||
+1
-5
@@ -41,11 +41,7 @@ def _model_identity(model: Dict[str, str]) -> str:
|
||||
path = model.get("path", "")
|
||||
if not path:
|
||||
return ""
|
||||
|
||||
try:
|
||||
return _path_identity(path)
|
||||
except (OSError, ValueError):
|
||||
return os.path.normcase(os.path.abspath(path))
|
||||
return _path_identity(path)
|
||||
|
||||
|
||||
def get_model_directories() -> Dict[str, Tuple[List[str], set]]:
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from core.metadata_model_utils import dedupe_models, is_model_file_path, model_identity_key
|
||||
from core.scanner import _model_identity
|
||||
|
||||
|
||||
class MetadataModelHelperTests(unittest.TestCase):
|
||||
@@ -45,6 +47,14 @@ class MetadataModelHelperTests(unittest.TestCase):
|
||||
self.assertEqual("", model_identity_key(empty))
|
||||
self.assertEqual([first], dedupe_models(models))
|
||||
|
||||
def test_scanner_and_metadata_identity_share_path_fallback(self):
|
||||
model_path = os.path.join("models", "model.safetensors")
|
||||
expected = os.path.normcase(os.path.abspath(model_path))
|
||||
|
||||
with patch("core.path_utils.os.path.realpath", side_effect=OSError):
|
||||
self.assertEqual(expected, _model_identity({"path": model_path}))
|
||||
self.assertEqual(expected, model_identity_key({"path": model_path}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user