-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopie.py
74 lines (61 loc) · 2.33 KB
/
opie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import click
import os
import sys
import importlib.util
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
plugin_folder = os.path.join(os.path.dirname(__file__), 'commands')
class OpieCLI(click.MultiCommand):
opie_logo = r"""
___ ____ _ _______
/ _ \ | _ \ (_) | |_____|
| | | | | | | | | | | |__
| | | | | |_| | | | | |__|
| |_| | | __/ | | | |_____
\___/ |_| |_| |_|_____|
*================================*
| Your OP-1 's best frand! |
*================================*
"""
def __init__(self, **attrs):
click.MultiCommand.__init__(self, invoke_without_command=True, no_args_is_help=False, chain=False, **attrs)
def list_commands(self, ctx):
rv = []
for filename in os.listdir(plugin_folder):
if filename.endswith('.py'):
rv.append(filename[:-3])
rv.sort()
return rv
def get_command(self, ctx, name):
ns = {}
fn = os.path.join(plugin_folder, name + '.py')
with open(fn) as f:
code = compile(f.read(), fn, 'exec')
eval(code, ns, ns)
return click.Command(name, callback=ns['cli'].callback)
def get_command_description(self, name):
spec = importlib.util.spec_from_file_location(name, os.path.join(plugin_folder, name + '.py'))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return getattr(module, 'description', 'No description available.')
def print_help(self):
commands = self.list_commands(None)
print("Available commands: \n")
for cmd in commands:
description = self.get_command_description(cmd)
print(f" {cmd}: {description}")
def invoke(self, ctx,):
print(self.opie_logo)
self.print_help()
while True:
choice = input("\nEnter a command or type exit: ").strip().lower()
if choice in ['quit', 'exit']:
print("Exiting...\nPlease return to OP-1RepackerGUI")
return
elif choice in self.list_commands(ctx):
command = self.get_command(ctx, choice)
return command.invoke(ctx)
else:
print(f"Invalid command: {choice}. Please select from the list of available commands")
cli = OpieCLI()
if __name__ == '__main__':
cli()