-
Notifications
You must be signed in to change notification settings - Fork 5
/
start_example.py
82 lines (68 loc) · 2.02 KB
/
start_example.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
75
76
77
78
79
80
81
82
import os
import sys
"""
Script to execute example-files from outside the pygvisuals-package
"""
def has_main_loop(f):
"""
Simple test to see if a py-file has a method called 'main_loop'
"""
if not f.lower().endswith(".py"):
return False
try:
descriptor = open(f)
except:
return False
try:
while True:
try:
line = descriptor.readline()
except:
line = None
if not line:
break
if line.startswith("def main_loop():"):
descriptor.close()
return True
descriptor.close()
return False
except:
descriptor.close()
return False
### Scan Directory ###
example_directories = [os.path.join(os.path.split(__file__)[0], "examples")]
print("Searching in: " + ", ".join(example_directories))
examples = []
for path in example_directories:
for root, dirs, files in os.walk(path):
for f in files:
if has_main_loop(os.path.join(root, f)):
examples.append(os.path.join(root, f))
print("List of executable examples:")
for e in range(len(examples)):
print(str(e) + ":", examples[e])
### Handle User-Input ###
print("")
print("Choose an example to execute with its index! (type 'exit' to exit)")
index = None
while index == None:
userinput = str(input("index: "))
if userinput.lower() == "exit":
sys.exit()
break
try:
index = int(userinput)
if index >= len(examples) or index < 0:
print("Could not find example of index", index)
index = None
except:
print("Invalid index!")
index = None
print("Executing:", examples[index])
try:
import importlib.util
spec = importlib.util.spec_from_file_location(os.path.split(examples[index])[1], examples[index])
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except Exception as ex:
__import__(examples[index].replace(os.sep, "."))