Coverage for sparkle/CLI/_cli_.py: 0%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-07 15:22 +0000

1#!/usr/bin/env python3 

2"""Sparkle CLI entry point.""" 

3import sys 

4import os 

5from pathlib import Path 

6 

7module_path = Path(__file__).parent.resolve() 

8 

9package_cli_entry_points = [ 

10 module_path / "core" / "compute_features.py", 

11 module_path / "core" / "run_portfolio_selector_core.py", 

12 module_path.parent / "solver" / "solver_cli.py", 

13 module_path.parent / "configurator" / "configurator_cli.py", 

14] 

15 

16 

17def commands() -> list[str]: 

18 """Get list of available commands.""" 

19 module_path = Path(__file__).parent.resolve() 

20 self_name = Path(__file__).name 

21 return [path.stem for path in module_path.iterdir() 

22 if path.is_file() and path.suffix == ".py" and path.name != self_name] 

23 

24 

25def main() -> None: 

26 """Pass through command to launch CLI commands.""" 

27 max_space = max([path.name.count("_") for path in module_path.iterdir() 

28 if path.is_file()]) 

29 if len(sys.argv) < 2: 

30 print("Usage: sparkle <command>") 

31 sys.exit(1) 

32 # Support spaces instead of _ 

33 possible_commands = commands() 

34 for i in range(1, min(max_space, len(sys.argv))): 

35 command = "_".join(sys.argv[1:i + 1]) 

36 args = sys.argv[i + 1:] 

37 command_file = module_path / f"{command}.py" 

38 if command in possible_commands: 

39 break 

40 

41 if command_file.is_file(): 

42 os.system(f"python3 {command_file} {' '.join(args)}") 

43 elif command == "install_autocomplete": 

44 script_path = module_path / "autocomplete.sh" 

45 bash_profile = Path.home() / ".bash_profile" 

46 if not bash_profile.exists(): 

47 bash_profile.open("w+").close() 

48 bash_profile.open("a").write( 

49 "\n#----- Sparkle AutoComplete ----\n" 

50 f"source {script_path.absolute()}" 

51 "\n#----- Sparkle AutoComplete ----\n") 

52 print(f"Sparkle autocomplete installed! To enable, run `source {bash_profile}` " 

53 "or restart your terminal.") 

54 else: 

55 print(f"Sparkle does not understand command <{command}>") 

56 

57 

58if __name__ == "__main__": 

59 main()