Source code for xraylabtool.interfaces.completion_v2.integration

"""Integration layer for the new completion system.

This module provides backward compatibility and integration with the existing CLI
while gradually migrating to the new virtual environment-centric completion system.
"""

import argparse
import os
from typing import Any

from .cli import completion_main
from .installer import CompletionInstaller


[docs] def legacy_install_completion_main(args: argparse.Namespace) -> int: """Legacy install completion main function with new system backend. This function maintains backward compatibility with the existing CLI while using the new completion system internally. """ installer = CompletionInstaller() # Handle legacy arguments shell = getattr(args, "shell", None) or getattr(args, "install_completion", None) force = getattr(args, "force", False) or getattr(args, "uninstall", False) system_wide = getattr(args, "system", False) test_mode = getattr(args, "test", False) uninstall_mode = getattr(args, "uninstall", False) try: if test_mode: # Test completion functionality return test_completion_installation() elif uninstall_mode: # Uninstall completion success = installer.uninstall() return 0 if success else 1 elif system_wide: # System-wide installation not supported in new system print( "โš ๏ธ System-wide installation is not supported in the new completion" " system." ) print( "๐Ÿ’ก The new system installs completion per virtual environment for" " better isolation." ) print( " This ensures completion is available only when the relevant" " environment is active." ) # Offer alternative current_env = installer.env_detector.get_current_environment() if current_env: print(f"\n๐Ÿ”ง Installing in current environment: {current_env.name}") success = installer.install(shell=shell, force=force) return 0 if success else 1 else: print("\nโŒ No active virtual environment detected.") print(" Please activate a virtual environment and try again.") return 1 else: # Regular installation in virtual environment success = installer.install(shell=shell, force=force) return 0 if success else 1 except KeyboardInterrupt: print("\nโš ๏ธ Operation cancelled by user") return 1 except Exception as e: print(f"โŒ Error: {e}") return 1
[docs] def legacy_uninstall_completion_main(args: Any) -> int: """Legacy uninstall completion main function with new system backend.""" installer = CompletionInstaller() # Handle legacy arguments shell = getattr(args, "shell", None) if hasattr(args, "shell") else None system_wide = getattr(args, "system", False) if hasattr(args, "system") else False cleanup = getattr(args, "cleanup", False) if hasattr(args, "cleanup") else False try: if cleanup: # Clean up all environments success = installer.uninstall(all_envs=True) return 0 if success else 1 elif system_wide: print( "โš ๏ธ System-wide uninstallation is not applicable in the new completion" " system." ) print("๐Ÿ’ก The new system manages completion per virtual environment.") # Offer to clean up current environment or all environments choice = ( input("\nRemove from current environment only? (y/N): ").strip().lower() ) if choice in ("y", "yes"): success = installer.uninstall() return 0 if success else 1 else: choice = input("Remove from all environments? (y/N): ").strip().lower() if choice in ("y", "yes"): success = installer.uninstall(all_envs=True) return 0 if success else 1 else: print("Operation cancelled.") return 0 else: # Regular uninstallation from current environment # Use the legacy method for backward compatibility with tests success = installer.uninstall_completion( shell_type=shell, cleanup_session=True ) return 0 if success else 1 except KeyboardInterrupt: print("\nโš ๏ธ Operation cancelled by user") return 1 except Exception as e: print(f"โŒ Error: {e}") return 1
[docs] def test_completion_installation() -> int: """Test if completion is working in the current environment.""" installer = CompletionInstaller() print("๐Ÿงช Testing completion installation...") # Check if we're in a virtual environment current_env = installer.env_detector.get_current_environment() if not current_env: print("โŒ No active virtual environment detected.") print(" Completion testing requires an active virtual environment.") return 1 print(f"๐Ÿ“ Testing in environment: {current_env.name} ({current_env.env_type})") # Check if completion is installed if not current_env.has_completion: print("โŒ Completion is not installed in this environment.") print(" Run 'xraylabtool install-completion' to install it.") return 1 print("โœ… Completion is installed in this environment.") # Test shell-specific completion shell = installer._detect_current_shell() print(f"๐Ÿš Detected shell: {shell}") # Check if completion script exists and is readable completion_dir = current_env.path / "share" / "xraylabtool" / "completion" script_filename = installer.completion_manager.get_filename(shell) script_path = completion_dir / script_filename if script_path.exists(): print(f"โœ… Completion script found: {script_path}") # Test if script is executable (on Unix-like systems) if os.name != "nt" and not os.access(script_path, os.X_OK): print("โš ๏ธ Completion script is not executable.") print(" This may cause issues with completion loading.") # Provide testing instructions print("\n๐Ÿ“‹ To test completion manually:") print(" 1. Start a new terminal session") print(" 2. Activate this environment") print(" 3. Type 'xraylabtool ' and press TAB") print(" 4. You should see available commands") print("\n๐Ÿ’ก If completion doesn't work:") print(" โ€ข Try starting a new shell session") print(" โ€ข Check if your shell supports completion") print(" โ€ข Reinstall with 'xraylabtool install-completion --force'") return 0 else: print(f"โŒ Completion script not found: {script_path}") print(" The completion may not be properly installed.") return 1
[docs] def handle_new_completion_command(args: list[str]) -> int: """Handle new 'completion' subcommand.""" # Remove 'completion' from args if args and args[0] == "completion": args = args[1:] return completion_main(args)
# Backward compatibility exports install_completion_main = legacy_install_completion_main uninstall_completion_main = legacy_uninstall_completion_main