Source code for imednet_workflows.wizard

"""Interactive console wizard and parameter validator for iMednet workflows.

Provides step-by-step interactive prompts, live parameter validation,
and stderr progress reporting to preserve clean stdout stream outputs.
"""

from __future__ import annotations

import sys
from datetime import UTC, datetime
from typing import Any

from imednet.spi.cli import (
    is_json_output_mode,
    parse_email_notify,
    print_error,
    redact_cli_secrets,
    supports_interactive_stdin,
    validate_cli_path_segment,
)

from .state_ledger import DEFAULT_LEDGER_PATH, BaseStateProvider, LedgerState, get_state_provider


[docs]def ensure_interactive_stdin(*, force_non_interactive: bool = False) -> None: """Ensure interactive stdin prompts are permitted, exiting with code 1 otherwise.""" if force_non_interactive or not supports_interactive_stdin(): print_error( "Error: Interactive prompts require an interactive TTY on stdin and cannot run " "when stdin is piped or non-interactive mode is active. " "Pass required options via CLI flags." ) sys.exit(1)
[docs]def prompt_input( prompt_text: str, default: str | None = None, *, allow_clear: bool = True, ) -> str: """Prompt the user for input on stderr and read from stdin. Args: prompt_text: Prompt message describing the expected input. default: Optional default value if the user provides an empty line. allow_clear: When True, typing ``-`` clears any default and returns ``""``. Returns: The stripped string provided by the user, or the default. Raises: SystemExit: If stdin is non-interactive or end-of-file (EOF) is encountered. """ ensure_interactive_stdin() if default is not None: sys.stderr.write(f"{prompt_text} [{default}]: ") else: sys.stderr.write(f"{prompt_text}: ") sys.stderr.flush() line = sys.stdin.readline() if line == "": print_error("Error: Input stream closed (EOF). Operation cancelled.") sys.exit(1) stripped = line.strip() if allow_clear and stripped == "-": return "" if not stripped and default is not None: return default return stripped
[docs]def prompt_confirm(prompt_text: str, default: bool = False) -> bool: """Prompt the user for a boolean confirmation on stderr. Args: prompt_text: Description of the confirmation action. default: Default boolean choice if user presses Enter. Returns: True if confirmed, False otherwise. Raises: SystemExit: If stdin is non-interactive or end-of-file (EOF) is encountered. """ ensure_interactive_stdin() suffix = " [Y/n]: " if default else " [y/N]: " sys.stderr.write(f"{prompt_text}{suffix}") sys.stderr.flush() line = sys.stdin.readline() if line == "": print_error("Error: Input stream closed (EOF). Operation cancelled.") sys.exit(1) choice = line.strip().lower() if not choice: return default return choice in ("y", "yes")
[docs]def validate_iso8601_timestamp(ts: str) -> tuple[bool, datetime | None, str | None]: """Validate that a string is a valid ISO-8601 UTC timestamp. Args: ts: Input timestamp string (e.g. '2026-05-22T12:00:00Z'). Returns: A tuple of (is_valid, parsed_datetime_utc, error_message). """ if not ts or not ts.strip(): return False, None, "Timestamp cannot be empty. Example: '2026-05-22T12:00:00Z'." raw = ts.strip() normalized = raw.replace("Z", "+00:00") try: dt = datetime.fromisoformat(normalized) except ValueError as err: return ( False, None, f"Invalid ISO-8601 timestamp format: '{raw}'. Expected 'YYYY-MM-DDTHH:MM:SSZ' (e.g., '2026-05-22T12:00:00Z'). Details: {err}", ) # Normalize to UTC dt = dt.replace(tzinfo=UTC) if dt.tzinfo is None else dt.astimezone(UTC) return True, dt, None
[docs]def prompt_iso8601_timestamp( prompt_text: str = "Enter ISO-8601 UTC timestamp (e.g., '2026-05-22T12:00:00Z')", default: str | None = None, ) -> tuple[str, datetime]: """Interactively prompt for and validate an ISO-8601 UTC timestamp until valid. Args: prompt_text: Message describing the timestamp prompt. default: Optional default timestamp string. Returns: A tuple of (raw_timestamp_string, parsed_datetime_utc). """ while True: value = prompt_input(prompt_text, default=default, allow_clear=False) is_valid, dt, err = validate_iso8601_timestamp(value) if is_valid and dt is not None: return value, dt print_progress(f"Validation Error: {err}")
[docs]def validate_filter_syntax( filter_str: str, *, model: type[Any] | None = None, ) -> tuple[bool, str | None, Any | None, str | None]: """Validate a filter parameter string via the shared SPI parser. Args: filter_str: Raw filter string (single clause or compound ';' / ',' clauses). model: Optional endpoint resource model class used to validate filter keys. Returns: A tuple of (is_valid, key, parsed_value, error_message). """ from imednet.spi.cli import resolve_endpoint_filter_attributes from imednet.spi.utils import parse_filter_expressions allowed_attrs = resolve_endpoint_filter_attributes(model) normalized = filter_str.strip() if isinstance(filter_str, str) else filter_str try: parsed = parse_filter_expressions( [normalized], model=model, allowed_attributes=allowed_attrs, ) except ValueError as exc: return False, None, None, str(exc) if len(parsed) == 1: key, parsed_val = next(iter(parsed.items())) return True, key, parsed_val, None return True, "filter", parsed, None
def _validate_filter_with_context( filter_str: str, accepted_filters: list[str], *, model: type[Any] | None = None, ) -> tuple[bool, str | None]: """Validate a filter candidate both standalone and alongside accepted filters.""" is_valid, _, _, err = validate_filter_syntax(filter_str, model=model) if not is_valid: return False, err if not accepted_filters: return True, None from imednet.spi.cli import resolve_endpoint_filter_attributes from imednet.spi.utils import parse_filter_expressions allowed_attrs = resolve_endpoint_filter_attributes(model) try: parse_filter_expressions( [*accepted_filters, filter_str], model=model, allowed_attributes=allowed_attrs, ) except ValueError as exc: return False, str(exc) return True, None
[docs]def prompt_filters( category_name: str, example: str, existing_filters: list[str] | None = None, *, model: type[Any] | None = None, ) -> list[str]: """Interactively prompt and validate key-value filters for a category. Args: category_name: Name of the filter category (e.g., 'subject filter'). example: An illustrative example string (e.g., 'subject_status=Screened'). existing_filters: Filters already supplied on the command line. model: Optional endpoint resource model class used to validate filter keys. Returns: A list of validated filter strings. """ filters: list[str] = [] if existing_filters: for f in existing_filters: is_valid, err = _validate_filter_with_context(f, filters, model=model) if is_valid: filters.append(f) else: print_progress(f"Validation Error in provided filter: {err}") corrected = prompt_input( f"Enter corrected {category_name} to replace invalid '{f}' (e.g., '{example}')" ) if not corrected: print_progress( f"Error: Malformed filter '{f}' was not corrected. Aborting to prevent unfiltered extraction." ) sys.exit(1) while True: c_valid, c_err = _validate_filter_with_context(corrected, filters, model=model) if c_valid: filters.append(corrected) break print_progress(f"Validation Error: {c_err}") corrected = prompt_input( f"Enter corrected {category_name} to replace invalid '{f}' (e.g., '{example}')" ) if not corrected: print_progress( f"Error: Malformed filter '{f}' was not corrected. Aborting to prevent unfiltered extraction." ) sys.exit(1) # If filters were already provided, confirm or add more prompt_msg = ( f"Enter {category_name} (e.g., '{example}', or press Enter to skip/finish)" if not filters else f"Enter additional {category_name} (e.g., '{example}', '-' to clear, or press Enter to finish)" ) while True: entry = prompt_input(prompt_msg, allow_clear=False) if entry == "-": filters.clear() break if not entry: break is_valid, err = _validate_filter_with_context(entry, filters, model=model) if is_valid: filters.append(entry) prompt_msg = ( f"Enter additional {category_name} ('-' to clear, or press Enter to finish)" ) else: print_progress(f"Validation Error: {err}") return filters
[docs]def prompt_study_key(study_key: str | None = None) -> str: """Ensure a valid non-empty study key, prompting if needed. Args: study_key: Existing study key from CLI arguments. Returns: Non-empty study key string. """ if study_key and study_key.strip(): cleaned = study_key.strip() validate_cli_path_segment(cleaned, name="study_key") return cleaned while True: val = prompt_input("Enter study key", allow_clear=False) if val: validate_cli_path_segment(val, name="study_key") return val print_progress( "Validation Error: Study key cannot be empty. Please enter a valid study key." )
[docs]def prompt_stream_name(stream: str | None = None) -> str: """Ensure a valid non-empty stream name, prompting if needed. Args: stream: Existing stream name from CLI arguments. Returns: Non-empty stream name string. """ if stream and stream.strip(): cleaned = stream.strip() validate_cli_path_segment(cleaned, name="stream") return cleaned while True: val = prompt_input("Enter stream name (e.g., 'records')", allow_clear=False) if val: validate_cli_path_segment(val, name="stream") return val print_progress( "Validation Error: Stream name cannot be empty. Please enter a valid stream name." )
[docs]def prompt_positive_int(prompt_text: str, default: int = 1) -> int: """Prompt for a positive integer with immediate validation. Args: prompt_text: Message describing the expected number. default: Default positive integer value. Returns: Validated positive integer (> 0). """ while True: raw = prompt_input(prompt_text, default=str(default), allow_clear=False) try: val = int(raw) if val > 0: return val print_progress("Validation Error: Value must be a positive integer (> 0).") except ValueError: print_progress(f"Validation Error: '{raw}' is not a valid integer.")
[docs]def prompt_subject_count( prompt_text: str = "Enter number of test subjects", default: int = 2, ) -> int: """Prompt for a UAT subject count bounded within 1..100 before Study inspection.""" while True: raw = prompt_input(prompt_text, default=str(default), allow_clear=False) try: val = int(raw) except ValueError: print_progress(f"Validation Error: '{raw}' is not a valid integer.") continue if 1 <= val <= 100: return val print_progress("Validation Error: subject_count must be between 1 and 100.")
[docs]def prompt_email_notify( prompt_text: str = ( "Enter email notification ('true', 'false', email address, '-' to clear, or leave blank)" ), default: str | bool | None = None, ) -> bool | str | None: """Prompt for and validate an optional ``--email-notify`` value before Study inspection.""" default_str = ( "" if default is None else (str(default).lower() if isinstance(default, bool) else str(default)) ) while True: raw = prompt_input(prompt_text, default=default_str, allow_clear=True) if not raw: return None try: return parse_email_notify(raw) except ValueError as err: print_progress(f"Validation Error: {redact_cli_secrets(str(err))}")
[docs]def prompt_non_negative_int(prompt_text: str, default: int = 0) -> int: """Prompt for a non-negative integer with immediate validation. Args: prompt_text: Message describing the expected number. default: Default non-negative integer value. Returns: Validated non-negative integer (>= 0). """ while True: raw = prompt_input(prompt_text, default=str(default), allow_clear=False) try: val = int(raw) if val >= 0: return val print_progress("Validation Error: Value must be a non-negative integer (>= 0).") except ValueError: print_progress(f"Validation Error: '{raw}' is not a valid integer.")
[docs]def display_ledger_streams( ledger_path: str = DEFAULT_LEDGER_PATH, study_key: str | None = None, *, state_provider: BaseStateProvider | None = None, ) -> LedgerState | None: """Display active ledger streams to stderr for interactive inspection. Args: ledger_path: Path to the pipeline ledger JSON file. study_key: Optional study key to filter displayed streams. state_provider: Optional pre-initialized state provider to reuse. Returns: The loaded :class:`LedgerState` when readable, or ``None`` on error. """ ledger = state_provider if state_provider is not None else get_state_provider(ledger_path) try: state = ledger.read_state() except Exception as err: print_progress(f"Notice: Could not read existing ledger at {ledger_path}: {err}") return None print_progress(f"\n--- Current Ledger Streams ({ledger_path}) ---") header = "Study Key | Stream Name | Last Timestamp (UTC) | Records Processed | Status | Error Message" print_progress(header) print_progress("-" * len(header)) found = False for s_key, study_state in state.studies.items(): if study_key and s_key != study_key: continue for stream_name, stream_state in study_state.streams.items(): found = True ts_str = ( stream_state.last_timestamp.isoformat() if stream_state.last_timestamp else "None" ) print_progress( f"{s_key} | {stream_name} | {ts_str} | {stream_state.records_processed} | {stream_state.last_run_status} | {stream_state.error_message or ''}" ) if not found: print_progress("No active stream records found in ledger.") print_progress("") return state