Coverage for sparkle/CLI/_cli_.py: 0%
42 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-01 13:21 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-01 13:21 +0000
1#!/usr/bin/env python3
2"""Sparkle CLI entry point."""
3import sys
4import os
5from pathlib import Path
7module_path = Path(__file__).parent.resolve()
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]
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]
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 command, command_file = "", Path()
35 for i in range(1, min(max_space, len(sys.argv))):
36 if "--" in sys.argv[i]: # Parameter is never part of the command
37 break
38 command = "_".join(sys.argv[1:i + 1])
39 args = sys.argv[i + 1:]
40 command_file = module_path / f"{command}.py"
41 if command in possible_commands:
42 break
44 if command_file.is_file():
45 os.system(f"python3 {command_file} {' '.join(args)}")
46 elif command == "install_autocomplete":
47 script_path = module_path / "autocomplete.sh"
48 bash_profile = Path.home() / ".bash_profile"
49 if not bash_profile.exists():
50 bash_profile.open("w+").close()
51 bash_profile.open("a").write(
52 "\n#----- Sparkle AutoComplete ----\n"
53 f"source {script_path.absolute()}"
54 "\n#----- Sparkle AutoComplete ----\n")
55 print(f"Sparkle autocomplete installed! To enable, run `source {bash_profile}` "
56 "or restart your terminal.")
57 else:
58 print(f"Sparkle does not understand command <{command}>", end="")
59 from difflib import SequenceMatcher
60 similarities = [SequenceMatcher(None, command, alt).ratio()
61 for alt in possible_commands]
63 if max(similarities) > 0.6:
64 alternative = possible_commands[similarities.index(max(similarities))]
65 print(f". Did you mean <{alternative}>?")
66 else:
67 print()
70if __name__ == "__main__":
71 main()