updating and refactoring codebase for improved performance and maintainability
This commit is contained in:
+49
-35
@@ -13,11 +13,41 @@ from SYS.models import PipelineStageContext
|
||||
from SYS.logger import log, debug, debug_panel, is_debug_enabled
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
from SYS.worker import WorkerManagerRegistry, WorkerStages
|
||||
from SYS.cli_parsing import SelectionSyntax, SelectionFilterSyntax
|
||||
from SYS.rich_display import stdout_console
|
||||
from SYS.background_notifier import ensure_background_notifier
|
||||
from SYS.result_table import Table
|
||||
# SYS.worker deferred: ffmpeg+attr+rich (~260ms) loaded lazily on first pipeline run.
|
||||
_worker_mod: Any = None
|
||||
# SYS.cli_parsing deferred: prompt_toolkit (~300ms) loaded lazily on first selection.
|
||||
_cli_parsing_mod: Any = None
|
||||
|
||||
|
||||
def _worker():
|
||||
global _worker_mod
|
||||
if _worker_mod is None:
|
||||
import SYS.worker as _m
|
||||
_worker_mod = _m
|
||||
return _worker_mod
|
||||
|
||||
|
||||
def _cli_parsing():
|
||||
global _cli_parsing_mod
|
||||
if _cli_parsing_mod is None:
|
||||
import SYS.cli_parsing as _m
|
||||
_cli_parsing_mod = _m
|
||||
return _cli_parsing_mod
|
||||
|
||||
|
||||
# SYS.rich_display deferred: rich (~100ms) loaded lazily on first console output.
|
||||
# SYS.background_notifier deferred: rich/attr/ffmpeg loaded lazily on first notifier use.
|
||||
# SYS.result_table deferred: textual (~140ms) loaded lazily on first Table use.
|
||||
_result_table_mod: Any = None
|
||||
|
||||
|
||||
def _result_table():
|
||||
global _result_table_mod
|
||||
if _result_table_mod is None:
|
||||
from SYS.result_table import Table as _T
|
||||
_result_table_mod = _T
|
||||
return _result_table_mod
|
||||
|
||||
import re
|
||||
from datetime import datetime
|
||||
from SYS.cmdlet_catalog import import_cmd_module
|
||||
@@ -680,12 +710,14 @@ def set_last_result_table(
|
||||
"""
|
||||
state = _get_pipeline_state()
|
||||
|
||||
# Push current table to history before replacing
|
||||
# Push current table to history before replacing.
|
||||
# No .copy() needed: last_result_items is about to be replaced by reference,
|
||||
# not mutated in place, so the old list reference is safe to keep in history.
|
||||
if state.last_result_table is not None:
|
||||
state.result_table_history.append(
|
||||
(
|
||||
state.last_result_table,
|
||||
state.last_result_items.copy(),
|
||||
state.last_result_items,
|
||||
state.last_result_subject,
|
||||
)
|
||||
)
|
||||
@@ -724,26 +756,6 @@ def set_last_result_table(
|
||||
logger.exception("Failed to sort result_table and reorder items")
|
||||
|
||||
|
||||
if (
|
||||
result_table is not None
|
||||
and hasattr(result_table, "sort_by_title")
|
||||
and not getattr(result_table, "preserve_order", False)
|
||||
):
|
||||
try:
|
||||
result_table.sort_by_title()
|
||||
# Re-order items list to match the sorted table
|
||||
if state.display_items and hasattr(result_table, "rows"):
|
||||
sorted_items: List[Any] = []
|
||||
for row in result_table.rows:
|
||||
src_idx = getattr(row, "source_index", None)
|
||||
if isinstance(src_idx, int) and 0 <= src_idx < len(state.display_items):
|
||||
sorted_items.append(state.display_items[src_idx])
|
||||
if len(sorted_items) == len(result_table.rows):
|
||||
state.display_items = sorted_items
|
||||
except Exception:
|
||||
logger.exception("Failed to sort overlay result_table and reorder items")
|
||||
|
||||
|
||||
def set_last_result_table_overlay(
|
||||
result_table: Optional[Any],
|
||||
items: Optional[List[Any]] = None,
|
||||
@@ -1423,7 +1435,7 @@ class PipelineExecutor:
|
||||
new_first_stage: List[str] = []
|
||||
for token in first_stage_tokens:
|
||||
if token.startswith("@"): # selection
|
||||
selection = SelectionSyntax.parse(token)
|
||||
selection = _cli_parsing().SelectionSyntax.parse(token)
|
||||
if selection is not None:
|
||||
first_stage_selection_indices = sorted(
|
||||
[i - 1 for i in selection]
|
||||
@@ -1848,6 +1860,7 @@ class PipelineExecutor:
|
||||
}
|
||||
if output_fn:
|
||||
kwargs["output"] = output_fn
|
||||
from SYS.background_notifier import ensure_background_notifier
|
||||
ensure_background_notifier(worker_manager, **kwargs)
|
||||
except Exception:
|
||||
logger.exception("Failed to enable background notifier for session_worker_ids=%r", session_worker_ids)
|
||||
@@ -2633,9 +2646,9 @@ class PipelineExecutor:
|
||||
)
|
||||
|
||||
piped_result: Any = None
|
||||
worker_manager = WorkerManagerRegistry.ensure(config)
|
||||
worker_manager = _worker().WorkerManagerRegistry.ensure(config)
|
||||
pipeline_text = " | ".join(" ".join(stage) for stage in stages)
|
||||
pipeline_session = WorkerStages.begin_pipeline(
|
||||
pipeline_session = _worker().WorkerStages.begin_pipeline(
|
||||
worker_manager,
|
||||
pipeline_text=pipeline_text,
|
||||
config=config
|
||||
@@ -2790,8 +2803,8 @@ class PipelineExecutor:
|
||||
|
||||
if cmd_name.startswith("@"): # selection stage
|
||||
selection_token = raw_stage_name
|
||||
selection = SelectionSyntax.parse(selection_token)
|
||||
filter_spec = SelectionFilterSyntax.parse(selection_token)
|
||||
selection = _cli_parsing().SelectionSyntax.parse(selection_token)
|
||||
filter_spec = _cli_parsing().SelectionFilterSyntax.parse(selection_token)
|
||||
is_select_all = selection_token.strip() == "@*"
|
||||
if selection is None and filter_spec is None and not is_select_all:
|
||||
print(f"Invalid selection: {selection_token}\n")
|
||||
@@ -2849,7 +2862,7 @@ class PipelineExecutor:
|
||||
elif filter_spec is not None:
|
||||
selected_indices = [
|
||||
i for i, item in enumerate(items_list)
|
||||
if SelectionFilterSyntax.matches(item, filter_spec)
|
||||
if _cli_parsing().SelectionFilterSyntax.matches(item, filter_spec)
|
||||
]
|
||||
else:
|
||||
selected_indices = sorted(
|
||||
@@ -2894,7 +2907,7 @@ class PipelineExecutor:
|
||||
if base_table is not None and hasattr(base_table, "copy_with_title"):
|
||||
new_table = base_table.copy_with_title(getattr(base_table, "title", "") or "Results")
|
||||
else:
|
||||
new_table = Table(getattr(base_table, "title", "") if base_table is not None else "Results")
|
||||
new_table = _result_table()(getattr(base_table, "title", "") if base_table is not None else "Results")
|
||||
|
||||
try:
|
||||
if base_table is not None and getattr(base_table, "table", None):
|
||||
@@ -2918,6 +2931,7 @@ class PipelineExecutor:
|
||||
logger.exception("Failed to set last_result_table_overlay for filter selection")
|
||||
|
||||
try:
|
||||
from SYS.rich_display import stdout_console
|
||||
stdout_console().print()
|
||||
stdout_console().print(new_table)
|
||||
except Exception:
|
||||
@@ -3118,7 +3132,7 @@ class PipelineExecutor:
|
||||
pipe_idx = pipe_index_by_stage.get(stage_index)
|
||||
|
||||
overlay_table: Any | None = None
|
||||
session = WorkerStages.begin_stage(
|
||||
session = _worker().WorkerStages.begin_stage(
|
||||
worker_manager,
|
||||
cmd_name=cmd_name,
|
||||
stage_tokens=stage_tokens,
|
||||
|
||||
Reference in New Issue
Block a user