Source code for imednet.cli.utils.args

"""CLI argument parsing utilities."""

from __future__ import annotations

import argparse
import itertools
import os
import re
import sys
import weakref
from collections.abc import Callable, Collection, Iterable, Sequence
from typing import Any

from imednet.constants import DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE
from imednet.utils.filters import (
    compile_sort_parameter,
    extract_filter_attributes,
    normalize_filter_connectors,
    parse_filter_expressions,
    validate_record_filter,
)
from imednet.utils.security import sanitize_diagnostic_text

from .output import print_error
from .terminal import OUTPUT_FORMAT_ENV

STUDY_KEY_ARG = "The key identifying the study."
DEFAULT_INTERACTIVE_LIMIT = DEFAULT_PAGE_SIZE


_STUDY_KEY_FILTER_ATTRS: frozenset[str] = frozenset({"study_key", "studyKey"})


_ENDPOINT_FILTER_ATTRS_CACHE: weakref.WeakKeyDictionary[type[Any], frozenset[str] | None] = (
    weakref.WeakKeyDictionary()
)
_ENDPOINT_SORT_META_CACHE: weakref.WeakKeyDictionary[
    type[Any], tuple[frozenset[str] | None, bool]
] = weakref.WeakKeyDictionary()


[docs]def resolve_endpoint_filter_attributes(model: type[Any] | None) -> frozenset[str] | None: """Resolve documented endpoint ``FILTER_ATTRIBUTES`` for a registered SDK model.""" if model is None: return None target_model: type[Any] = model if target_model in _ENDPOINT_FILTER_ATTRS_CACHE: return _ENDPOINT_FILTER_ATTRS_CACHE[target_model] from imednet.endpoints.registry import ENDPOINT_REGISTRY resolved: frozenset[str] | None = None for endpoint_cls in ENDPOINT_REGISTRY.values(): if getattr(endpoint_cls, "MODEL", None) is target_model: attrs = getattr(endpoint_cls, "FILTER_ATTRIBUTES", None) if isinstance(attrs, frozenset): resolved = attrs break _ENDPOINT_FILTER_ATTRS_CACHE[target_model] = resolved return resolved
[docs]def resolve_endpoint_sort_metadata( model: type[Any] | None, ) -> tuple[frozenset[str] | None, bool]: """Resolve endpoint ``(SORT_ATTRIBUTES, ALLOW_RECORD_DATA_SORT)`` for a registered model.""" if model is None: return None, False target_model: type[Any] = model if target_model in _ENDPOINT_SORT_META_CACHE: return _ENDPOINT_SORT_META_CACHE[target_model] from imednet.endpoints.registry import ENDPOINT_REGISTRY resolved_sort: frozenset[str] | None = None allow_rd = False for endpoint_cls in ENDPOINT_REGISTRY.values(): if getattr(endpoint_cls, "MODEL", None) is target_model: attrs = getattr(endpoint_cls, "SORT_ATTRIBUTES", None) if isinstance(attrs, frozenset): resolved_sort = attrs allow_rd = bool(getattr(endpoint_cls, "ALLOW_RECORD_DATA_SORT", False)) break _ENDPOINT_SORT_META_CACHE[target_model] = (resolved_sort, allow_rd) return resolved_sort, allow_rd
def _contains_study_key_filter(parsed: dict[str, Any]) -> bool: """Return True if parsed filters include ``study_key`` or ``studyKey``.""" if _STUDY_KEY_FILTER_ATTRS.intersection(parsed): return True raw_filter = parsed.get("filter") if isinstance(raw_filter, str): return bool(_STUDY_KEY_FILTER_ATTRS.intersection(extract_filter_attributes(raw_filter))) return False def _sanitize_cli_arg_error(msg: str) -> str: """Mask inline clinical PHI and redact standalone credential tokens in CLI arg errors.""" redacted = msg for env_var in ("IMEDNET_API_KEY", "IMEDNET_SECURITY_KEY", "IMEDNET_OIDC_TOKEN"): val = os.environ.get(env_var) if val: for candidate in (val, val.strip()): if candidate: redacted = re.sub( rf"(?<![A-Za-z0-9_]){re.escape(candidate)}(?![A-Za-z0-9_])", "********", redacted, ) return sanitize_diagnostic_text(redacted)
[docs]def parse_filter_args( filter_args: list[str] | None, *, model: type[Any] | None = None, allowed_attributes: Collection[str] | None = None, disallow_study_key: bool = False, multi_bound_attributes: Collection[str] | None = None, ) -> dict[str, Any] | None: """Parse repeatable CLI ``--filter`` strings via the shared SPI filter parser. Supports legacy ``key=value`` equality, Table 2 iMednet comparison operators (``<``, ``<=``, ``>``, ``>=``, ``==``, ``!=``), compound expressions joined exclusively by ``;`` (AND) or ``,`` (OR), endpoint ``allowed_attributes`` allowlists, and Pydantic ``model`` scalar coercion. Exits with status code ``1`` on ``stderr`` if validation fails. """ if not filter_args: return None effective_allowed: Collection[str] | None = ( allowed_attributes if allowed_attributes is not None else resolve_endpoint_filter_attributes(model) ) if disallow_study_key and effective_allowed is not None: effective_allowed = frozenset( attr for attr in effective_allowed if attr not in _STUDY_KEY_FILTER_ATTRS ) try: parsed = parse_filter_expressions( filter_args, model=model, allowed_attributes=effective_allowed, allow_regex=False, multi_bound_attributes=multi_bound_attributes, ) except ValueError as exc: print_error(f"Error: {_sanitize_cli_arg_error(str(exc))}") sys.exit(1) if disallow_study_key and _contains_study_key_filter(parsed): print_error( "Error: Invalid filter: 'study_key' and 'studyKey' cannot be used in " "--filter when positional study_key is provided." ) sys.exit(1) return parsed
[docs]def parse_record_data_filter(record_data_filter: str | None) -> str | None: """Validate a CLI ``--record-data-filter`` string via ``validate_record_filter``. Supports comparison operators including ``=~``, exclusive ``;`` (AND) or ``,`` (OR) connectors, and literal ``none`` (case-insensitive) bypass. Exits with status code ``1`` on ``stderr`` if validation fails. """ if record_data_filter is None: return None stripped = record_data_filter.strip() if not stripped: print_error("Error: Invalid record data filter: cannot be empty.") sys.exit(1) try: validate_record_filter(record_data_filter) except ValueError as exc: print_error(f"Error: Invalid record data filter: {_sanitize_cli_arg_error(str(exc))}") sys.exit(1) if stripped.lower() == "none": return stripped return normalize_filter_connectors(stripped)
[docs]def positive_int(value: str) -> int: """Validate and return a strictly positive integer (> 0) for argparse options.""" if isinstance(value, bool): raise argparse.ArgumentTypeError("value must be a positive integer (> 0)") try: parsed = int(value) except ValueError as exc: raise argparse.ArgumentTypeError(f"invalid int value: {value!r}") from exc if parsed <= 0: raise argparse.ArgumentTypeError("value must be a positive integer (> 0)") return parsed
[docs]class UniqueStoreAction(argparse.Action): """Store action that rejects conflicting duplicate flag values."""
[docs] def __call__( self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, values: Any, option_string: str | None = None, ) -> None: """Store flag value on namespace or abort with exit code 2 on conflicting values.""" del option_string existing = getattr(namespace, self.dest, None) if existing is not None and existing != values: key_name = self.dest.removesuffix("_flag") parser.error(f"conflicting values provided for {key_name}: '{existing}' and '{values}'") setattr(namespace, self.dest, values)
def _is_blank_arg(val: str | None) -> bool: return val is None or not str(val).strip()
[docs]def add_study_key_args( parser: argparse.ArgumentParser, *, flag_names: Sequence[str] = ("-s", "--study-key"), include_positional: bool = True, positional_help: str = STUDY_KEY_ARG, flag_help: str = STUDY_KEY_ARG, ) -> None: """Register hybrid ``--study-key`` flag and optional positional ``study_key``.""" available_flags = [f for f in flag_names if f not in parser._option_string_actions] if available_flags: parser.add_argument( *available_flags, dest="study_key_flag", default=None, action=UniqueStoreAction, help=flag_help, ) if include_positional: has_pos = any( not action.option_strings and action.dest == "study_key" for action in parser._actions ) if not has_pos: parser.add_argument("study_key", nargs="?", default=None, help=positional_help)
[docs]def resolve_study_key( parser: argparse.ArgumentParser, args: argparse.Namespace, *, required: bool = True, missing_error: str = "the following arguments are required: study_key", ) -> str | None: """Resolve hybrid positional ``study_key`` and ``--study-key`` flag, exiting 2 on conflict.""" flag_val: str | None = getattr(args, "study_key_flag", None) pos_val: str | None = getattr(args, "study_key", None) flag_present = not _is_blank_arg(flag_val) pos_present = not _is_blank_arg(pos_val) if flag_present and pos_present: if flag_val != pos_val: parser.error( f"conflicting values provided for study_key: '{flag_val}' (flag) and '{pos_val}' (positional)" ) resolved: str | None = flag_val elif flag_present: resolved = flag_val elif pos_present: resolved = pos_val else: resolved = None if resolved is None and required: parser.error(missing_error) if resolved is not None or hasattr(args, "study_key"): args.study_key = resolved return resolved
[docs]def resolve_hybrid_study_and_key( parser: argparse.ArgumentParser, args: argparse.Namespace, *, secondary_dest: str = "subject_key", secondary_flag_dest: str = "subject_key_flag", ) -> tuple[str, str]: """Resolve hybrid positional and flag pairs for ``study_key`` and a secondary key.""" flag_study: str | None = getattr(args, "study_key_flag", None) flag_secondary: str | None = getattr(args, secondary_flag_dest, None) pos_study: str | None = getattr(args, "study_key", None) pos_secondary: str | None = getattr(args, secondary_dest, None) if flag_study is not None and flag_secondary is not None: study_key: str | None = flag_study secondary_key: str | None = flag_secondary elif flag_study is not None and flag_secondary is None: study_key = flag_study if pos_secondary is not None and pos_study is not None: if ( not _is_blank_arg(pos_study) and not _is_blank_arg(flag_study) and pos_study != flag_study ): parser.error( f"conflicting values provided for study_key: '{flag_study}' (flag) and '{pos_study}' (positional)" ) secondary_key = pos_secondary elif pos_study is not None: secondary_key = pos_study else: secondary_key = None elif flag_study is None and flag_secondary is not None: secondary_key = flag_secondary if pos_study is not None and pos_secondary is not None: if ( not _is_blank_arg(pos_secondary) and not _is_blank_arg(flag_secondary) and pos_secondary != flag_secondary ): parser.error( f"conflicting values provided for {secondary_dest}: '{flag_secondary}' (flag) and '{pos_secondary}' (positional)" ) study_key = pos_study elif pos_study is not None: study_key = pos_study else: study_key = None else: study_key = pos_study secondary_key = pos_secondary missing_study = _is_blank_arg(study_key) missing_secondary = _is_blank_arg(secondary_key) if missing_study and missing_secondary: parser.error(f"the following arguments are required: study_key, {secondary_dest}") elif missing_study: parser.error("the following arguments are required: study_key") elif missing_secondary: parser.error(f"the following arguments are required: {secondary_dest}") assert study_key is not None assert secondary_key is not None return study_key, secondary_key
[docs]def add_simulation_arg(parser: argparse.ArgumentParser, *, suppress_default: bool = True) -> None: """Add ``--simulation`` option to run against offline ``SimulationTransport`` fixtures.""" if "--simulation" in parser._option_string_actions: return kwargs: dict[str, Any] = { "dest": "simulation", "action": "store_true", "help": "Run offline against deterministic synthetic simulation data without live API credentials.", } if suppress_default: kwargs["default"] = argparse.SUPPRESS parser.add_argument("--simulation", **kwargs)
[docs]def add_no_interactive(parser: argparse.ArgumentParser, *, suppress_default: bool = True) -> None: """Add ``--no-interactive`` option to disable interactive UI, spinners, and prompts.""" if "--no-interactive" in parser._option_string_actions: return kwargs: dict[str, Any] = { "action": "store_true", "help": "Disable interactive terminal UI, live spinners, and stdin prompts.", } if suppress_default: kwargs["default"] = argparse.SUPPRESS parser.add_argument("--no-interactive", **kwargs)
[docs]def add_output_format(parser: argparse.ArgumentParser) -> None: """Add common machine response and non-interactive flags to a leaf read command.""" parser.add_argument( "--format", dest="output_format", choices=("table", "json"), default=None, help="Response format: human table (default) or versioned JSON on stdout.", ) add_no_interactive(parser)
[docs]def resolve_output_format( output_format: str | None = None, *, default: str = "table", ) -> str: """Resolve CLI ``--format`` option against ``IMEDNET_CLI_OUTPUT_FORMAT``.""" if output_format is not None: return output_format if os.environ.get(OUTPUT_FORMAT_ENV, "").strip().lower() == "json": return "json" return default
[docs]def add_list_query_args( parser: argparse.ArgumentParser, *, include_filter: bool = True, filter_dest: str = "filter_args", filter_help: str | None = None, include_record_data_filter: bool = False, ) -> None: """Attach server-side filter, sort, limit, and unbounded flags to a list subparser.""" if include_filter: parser.add_argument( "-f", "--filter", action="append", dest=filter_dest, default=None, help=filter_help or ( "Server filter expression using =, ==, !=, <, <=, >, >= and " "';' (AND) or ',' (OR) connectors. Repeat for multiple AND filters." ), ) if include_record_data_filter: parser.add_argument( "--record-data-filter", dest="record_data_filter", default=None, help=( "Server recordDataFilter expression for Form Variable values using " "==, !=, <, <=, >, >=, =~ (e.g., 'AESER==Serious;AGE>=18' or 'AETERM=~^Head')." ), ) parser.add_argument( "--sort", dest="sort", default=None, help="Server sort specification (e.g., 'dateCreated,desc' or 'recordId,asc').", ) parser.add_argument( "--limit", dest="limit", type=int, default=None, help="Maximum number of items to fetch before stopping.", ) parser.add_argument( "--all", dest="fetch_all", action="store_true", default=False, help="Fetch all matching pages even in interactive TTY sessions.", )
[docs]def prepare_list_query( *, filter_args: list[str] | None = None, record_data_filter: str | None = None, sort: str | None = None, limit: int | None = None, fetch_all: bool = False, output_format: str | None = None, has_output_file: bool = False, no_interactive: bool = False, model: type[Any] | None = None, allowed_attributes: Collection[str] | None = None, disallow_study_key: bool = False, multi_bound_attributes: Collection[str] | None = None, sort_attributes: Collection[str] | None = None, allow_record_data_sort: bool | None = None, ) -> tuple[dict[str, Any], int | None]: """Validate CLI query options and resolve SDK ``list()`` kwargs and item bound. Returns: Tuple of ``(request_kwargs, effective_limit)``. """ if limit is not None and fetch_all: print_error("Error: Cannot combine --limit and --all.") sys.exit(1) if limit is not None and limit <= 0: print_error("Error: --limit must be a positive integer.") sys.exit(1) compiled_sort: str | list[str] | None = None if sort is not None: if not sort.strip(): print_error("Error: --sort cannot be empty.") sys.exit(1) eff_sort_attrs = sort_attributes eff_allow_rd = allow_record_data_sort if eff_sort_attrs is None and model is not None: eff_sort_attrs, resolved_rd = resolve_endpoint_sort_metadata(model) if eff_allow_rd is None: eff_allow_rd = resolved_rd try: compiled_sort = compile_sort_parameter( sort, sort_attributes=eff_sort_attrs, model=model, allow_record_data=eff_allow_rd, ) except ValueError as exc: print_error(f"Error: {_sanitize_cli_arg_error(str(exc))}") sys.exit(1) request_kwargs: dict[str, Any] = {} parsed_filters = parse_filter_args( filter_args, model=model, allowed_attributes=allowed_attributes, disallow_study_key=disallow_study_key, multi_bound_attributes=multi_bound_attributes, ) if parsed_filters: request_kwargs.update(parsed_filters) parsed_record_data_filter = parse_record_data_filter(record_data_filter) if parsed_record_data_filter is not None: request_kwargs["record_data_filter"] = parsed_record_data_filter if compiled_sort is not None: request_kwargs["sort"] = compiled_sort resolved_format = resolve_output_format(output_format) effective_limit: int | None = None if limit is not None: effective_limit = limit elif ( not fetch_all and not no_interactive and resolved_format == "table" and not has_output_file ): try: from .grid import is_interactive_terminal is_tty = is_interactive_terminal() except ImportError: is_tty = False if is_tty: effective_limit = DEFAULT_INTERACTIVE_LIMIT if effective_limit is not None: request_kwargs["size"] = min(effective_limit, MAX_PAGE_SIZE) return request_kwargs, effective_limit
[docs]def collect_list_items_with_remainder( source: Iterable[Any], effective_limit: int | None, ) -> tuple[list[Any], bool, Callable[[], list[Any]] | None]: """Materialize items up to ``effective_limit`` and return a remainder loader when truncated. Preserves the live ``_PaginatedIterator`` cursor so subsequent ``[a]`` loads drain remaining pages starting at page 1 without re-fetching page 0. Returns: Tuple of ``(items, truncated, load_remaining)``. """ if effective_limit is None: return list(source), False, None if isinstance(source, Sequence): truncated = len(source) > effective_limit initial = list(source[:effective_limit]) if not truncated: return initial, False, None remaining = list(source[effective_limit:]) def _drain_sequence() -> list[Any]: rest = list(remaining) remaining.clear() return rest return initial, True, _drain_sequence if hasattr(source, "has_more"): iterator = iter(source) items = list(itertools.islice(iterator, effective_limit)) truncated = bool(getattr(source, "has_more", False)) if not truncated: return items, False, None def _drain_paginated() -> list[Any]: return list(iterator) return items, True, _drain_paginated iterator = iter(source) batch = list(itertools.islice(iterator, effective_limit + 1)) truncated = len(batch) > effective_limit initial = batch[:effective_limit] if not truncated: return initial, False, None overflow = batch[effective_limit:] def _drain_iterable() -> list[Any]: rest = [*overflow, *iterator] overflow.clear() return rest return initial, True, _drain_iterable
[docs]def collect_list_items( source: Iterable[Any], effective_limit: int | None, ) -> tuple[list[Any], bool]: """Materialize items from an SDK ``list()`` iterable up to ``effective_limit``. Detects server-side truncation without issuing an extra page request when backed by ``_PaginatedIterator``. Returns: Tuple of ``(items, truncated)``. """ items, truncated, _ = collect_list_items_with_remainder(source, effective_limit) return items, truncated