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