forked from Impelon/PyGVisuals
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_example.py
More file actions
68 lines (58 loc) · 1.69 KB
/
start_example.py
File metadata and controls
68 lines (58 loc) · 1.69 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
import os, 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 ###
examples = []
for root, dirs, files in os.walk(os.path.join("pygvisuals", "examples")):
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 = raw_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]
__import__(examples[index].replace(os.sep, "."))