- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
Latest commit
55 lines (45 loc) · 1.74 KB
/
Copy pathexample.py
File metadata and controls
55 lines (45 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
importos
importglob
importsys
importimportlib
# Add src/ to path so unity_animation_player can be imported
_src_path=os.path.join(os.path.dirname(__file__), 'src')
if_src_pathnotinsys.path:
sys.path.insert(0, os.path.abspath(_src_path))
# Automatically scan all files ending with _example.py in the examples folder
examples_dir=os.path.join(os.path.dirname(__file__), 'examples')
example_files=glob.glob(os.path.join(examples_dir, '*_example.py'))
# Extract example names from filenames (remove path and suffix)
EXAMPLE= []
forfile_pathinexample_files:
filename=os.path.basename(file_path)
example_name=filename.replace('_example.py', '')
EXAMPLE.append(example_name)
def_run_example(example_name):
"""Import and run the specified example module."""
import_path=f"examples.{example_name}_example"
try:
module=importlib.import_module(import_path)
ifhasattr(module, 'main'):
module.main()
exceptExceptionase:
print(f"Error running example '{example_name}': {e}")
importtraceback
traceback.print_exc()
# Check if an example name is provided as a command-line argument
iflen(sys.argv) >1:
example_name=sys.argv[1]
# Verify if the example exists in the scanned list to prevent arbitrary imports
ifexample_nameinEXAMPLE:
_run_example(example_name)
else:
print(f"Error: Example '{example_name}' not found.")
print("Available examples:")
fori, exinenumerate(EXAMPLE):
print(f"{i+1}. {ex}")
else:
print("Choose an example:")
fori, exampleinenumerate(EXAMPLE):
print(f"{i+1}. {example}")
choice=int(input("Enter your choice: "))
_run_example(EXAMPLE[choice-1])