-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (53 loc) · 1.74 KB
/
Copy pathmain.py
File metadata and controls
74 lines (53 loc) · 1.74 KB
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
# main.py
import pkgutil
import importlib
import inspect
from PyProgramBase import ProgramBase
def discover_programs():
"""Auto-load all ProgramBase subclasses from modules starting with 'Py'."""
programs = []
package = "programs"
# iterate through modules inside 'programs'
for loader, module_name, _ in pkgutil.iter_modules([package]):
# only load modules beginning with "Py"
if not module_name.startswith("Py"):
continue
module = importlib.import_module(f"{package}.{module_name}")
# inspect module for subclasses of ProgramBase
for _, obj in inspect.getmembers(module, inspect.isclass):
if issubclass(obj, ProgramBase) and obj is not ProgramBase:
programs.append(obj)
return programs
def build_menu(program_classes):
"""Map numbers → (label, class)."""
menu = {}
for i, cls in enumerate(program_classes, start=1):
menu[i] = (cls.__name__, cls)
return menu
def show_menu(menu):
print("""
========================
MAIN MENU
========================
""")
for number, (label, _) in menu.items():
print(f"{number}. {label}")
print("0. Exit\n")
def main():
program_classes = discover_programs()
menu = build_menu(program_classes)
while True:
show_menu(menu)
choice = input("Enter option: ").strip()
if choice == "0":
print("Goodbye!")
break
if not choice.isdigit() or int(choice) not in menu:
print("Invalid selection.\n")
continue
_, program_cls = menu[int(choice)]
instance = program_cls()
instance.run()
print("\n--- Finished ---\n")
if __name__ == "__main__":
main()