updating and refactoring codebase for improved performance and maintainability

This commit is contained in:
2026-05-03 17:29:32 -07:00
parent b7d3dc5f2d
commit 77cab1bd27
17 changed files with 590 additions and 294 deletions
+82 -22
View File
@@ -40,11 +40,58 @@ build_pipeline_preview = sh.build_pipeline_preview
get_field = sh.get_field
from SYS.utils import sha256_file, unique_path, sanitize_filename
from SYS.metadata import write_metadata
# Canonical supported filetypes for all stores/cmdlets
SUPPORTED_MEDIA_EXTENSIONS = ALL_SUPPORTED_EXTENSIONS
class _CommandDependencies:
"""Command-scope cache for Store and plugin instances to avoid repeated instantiation."""
def __init__(self, config: Dict[str, Any]) -> None:
self.config = config
self._store: Optional[Store] = None
self._plugins: Dict[str, Any] = {}
def get_store(self) -> Optional[Store]:
"""Lazily initialize and return the command-scope Store instance."""
if self._store is None:
try:
self._store = Store(self.config)
except Exception:
self._store = None
return self._store
def get_plugin(self, name: str) -> Optional[Any]:
"""Cached plugin lookup by name."""
from ProviderCore.registry import get_plugin
norm_name = str(name or "").strip().lower()
if not norm_name:
return None
if norm_name in self._plugins:
return self._plugins[norm_name]
plugin = get_plugin(norm_name, self.config)
self._plugins[norm_name] = plugin
return plugin
def get_plugin_with_capability(self, name: str, capability: str) -> Optional[Any]:
"""Cached plugin lookup with capability check."""
from ProviderCore.registry import get_plugin_with_capability
norm_name = str(name or "").strip().lower()
if not norm_name:
return None
cache_key = f"{norm_name}#{capability}"
if cache_key in self._plugins:
return self._plugins[cache_key]
plugin = get_plugin_with_capability(norm_name, capability, self.config)
self._plugins[cache_key] = plugin
return plugin
DEBUG_PIPE_NOTE_PREVIEW_LENGTH = 256
# Protocol schemes that identify a remote resource / not a local file path.
@@ -220,11 +267,9 @@ class Add_File(Cmdlet):
parsed = parse_cmdlet_args(args, self)
progress = PipelineProgress(ctx)
# Initialize Store for backend resolution
try:
storage_registry = Store(config)
except Exception:
storage_registry = None
# Initialize command-scope dependency context (caches Store/plugins)
deps = _CommandDependencies(config)
storage_registry = deps.get_store()
path_arg = parsed.get("path")
location = parsed.get("store")
@@ -348,7 +393,7 @@ class Add_File(Cmdlet):
is_storage_backend_location = False
if location:
try:
store_for_lookup = storage_registry or Store(config)
store_for_lookup = storage_registry or deps.get_store()
is_storage_backend_location = Add_File._resolve_backend_by_name(store_for_lookup, str(location)) is not None
except Exception:
is_storage_backend_location = False
@@ -368,6 +413,7 @@ class Add_File(Cmdlet):
plugin_instance,
config,
store_instance=storage_registry,
deps=deps,
)
effective_storage_backend_name = plugin_storage_backend or (
@@ -629,10 +675,11 @@ class Add_File(Cmdlet):
config,
export_destination=(Path(location) if location and not is_storage_backend_location else None),
store_instance=storage_registry,
deps=deps,
)
if not media_path and plugin_name:
media_path, file_hash, temp_dir_to_cleanup = Add_File._download_piped_source(
pipe_obj, config, storage_registry
pipe_obj, config, storage_registry, deps=deps
)
if media_path:
try:
@@ -702,7 +749,7 @@ class Add_File(Cmdlet):
if location:
try:
store = storage_registry or Store(config)
store = storage_registry or deps.get_store()
resolved_backend = Add_File._resolve_backend_by_name(store, str(location))
if resolved_backend is not None:
code = self._handle_storage_backend(
@@ -833,7 +880,8 @@ class Add_File(Cmdlet):
Add_File._apply_pending_relationships(
pending_relationship_pairs,
config,
store_instance=storage_registry
store_instance=storage_registry,
deps=deps
)
except Exception:
pass
@@ -1063,6 +1111,7 @@ class Add_File(Cmdlet):
config: Dict[str,
Any],
store_instance: Optional[Store] = None,
deps: Optional[_CommandDependencies] = None,
) -> None:
"""Persist relationships to backends that support relationships.
@@ -1071,8 +1120,11 @@ class Add_File(Cmdlet):
if not pending:
return
if deps is None:
deps = _CommandDependencies(config)
try:
store = store_instance if store_instance is not None else Store(config)
store = store_instance if store_instance is not None else deps.get_store()
except Exception:
return
@@ -1343,6 +1395,7 @@ class Add_File(Cmdlet):
Any],
export_destination: Optional[Path] = None,
store_instance: Optional[Any] = None,
deps: Optional[_CommandDependencies] = None,
) -> Tuple[Optional[Path],
Optional[str],
Optional[Path]]:
@@ -1371,9 +1424,9 @@ class Add_File(Cmdlet):
if r_hash and r_store:
try:
store = store_instance
if not store:
store = Store(config)
if deps is None:
deps = _CommandDependencies(config)
store = store_instance or deps.get_store()
backend = Add_File._resolve_backend_by_name(store, r_store)
if backend is not None:
@@ -1441,6 +1494,7 @@ class Add_File(Cmdlet):
result,
pipe_obj,
config,
deps=deps,
)
if downloaded_path:
pipe_obj.path = str(downloaded_path)
@@ -1471,14 +1525,16 @@ class Add_File(Cmdlet):
config: Dict[str, Any],
*,
store_instance: Optional[Any] = None,
deps: Optional[_CommandDependencies] = None,
) -> Optional[str]:
plugin_key = Add_File._normalize_provider_key(plugin_name)
if not plugin_key:
return None
from ProviderCore.registry import get_plugin_with_capability
if deps is None:
deps = _CommandDependencies(config)
file_provider = get_plugin_with_capability(plugin_key, "upload", config)
file_provider = deps.get_plugin_with_capability(plugin_key, "upload")
if file_provider is None:
return None
@@ -1528,6 +1584,7 @@ class Add_File(Cmdlet):
result: Any,
pipe_obj: models.PipeObject,
config: Dict[str, Any],
deps: Optional[_CommandDependencies] = None,
) -> Tuple[Optional[Path], Optional[str], Optional[Path]]:
plugin_key = None
for source in (
@@ -1544,9 +1601,10 @@ class Add_File(Cmdlet):
if not plugin_key:
return None, None, None
from ProviderCore.registry import get_plugin
if deps is None:
deps = _CommandDependencies(config)
plugin = get_plugin(plugin_key, config)
plugin = deps.get_plugin(plugin_key)
if plugin is None:
return None, None, None
@@ -1562,16 +1620,17 @@ class Add_File(Cmdlet):
pipe_obj: models.PipeObject,
config: Dict[str, Any],
store_instance: Optional[Any],
deps: Optional[_CommandDependencies] = None,
) -> Tuple[Optional[Path], Optional[str], Optional[Path]]:
r_hash = str(getattr(pipe_obj, "hash", None) or getattr(pipe_obj, "file_hash", None) or "").strip()
r_store = str(getattr(pipe_obj, "store", None) or "").strip()
if not (r_hash and r_store):
return None, None, None
try:
store = store_instance or Store(config)
except Exception:
store = None
if deps is None:
deps = _CommandDependencies(config)
store = store_instance or deps.get_store()
backend = Add_File._resolve_backend_by_name(store, r_store) if store is not None else None
if backend is None:
return None, None, None
@@ -2244,6 +2303,7 @@ class Add_File(Cmdlet):
relationships = Add_File._get_relationships(result, pipe_obj)
try:
write_sidecar(target_path, tags, url, f_hash)
from SYS.metadata import write_metadata # lazy: avoids 1000+ module chain at startup
write_metadata(
target_path,
hash_value=f_hash,