- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstart_example.py
More file actions
Latest commit
82 lines (68 loc) · 2.02 KB
/
Copy pathstart_example.py
File metadata and controls
82 lines (68 loc) · 2.02 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
74
75
76
77
78
79
80
81
82
importos
importsys
"""
Script to execute example-files from outside the pygvisuals-package
"""
defhas_main_loop(f):
"""
Simple test to see if a py-file has a method called 'main_loop'
"""
ifnotf.lower().endswith(".py"):
returnFalse
try:
descriptor=open(f)
except:
returnFalse
try:
whileTrue:
try:
line=descriptor.readline()
except:
line=None
ifnotline:
break
ifline.startswith("def main_loop():"):
descriptor.close()
returnTrue
descriptor.close()
returnFalse
except:
descriptor.close()
returnFalse
### Scan Directory ###
example_directories= [os.path.join(os.path.split(__file__)[0], "examples")]
print("Searching in: "+", ".join(example_directories))
examples= []
forpathinexample_directories:
forroot, dirs, filesinos.walk(path):
forfinfiles:
ifhas_main_loop(os.path.join(root, f)):
examples.append(os.path.join(root, f))
print("List of executable examples:")
foreinrange(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
whileindex==None:
userinput=str(input("index: "))
ifuserinput.lower() =="exit":
sys.exit()
break
try:
index=int(userinput)
ifindex>=len(examples) orindex<0:
print("Could not find example of index", index)
index=None
except:
print("Invalid index!")
index=None
print("Executing:", examples[index])
try:
importimportlib.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)
exceptExceptionasex:
__import__(examples[index].replace(os.sep, "."))