-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_parse.py
More file actions
40 lines (32 loc) · 1.07 KB
/
test_parse.py
File metadata and controls
40 lines (32 loc) · 1.07 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
# A utility method to capture the output of the textconv stripts as strings
import sys
import os.path
from unittest.mock import patch
from io import StringIO
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
import amxd_textconv
import maxpat_textconv
import als_textconv
def parse(path):
mains = {
".amxd": amxd_textconv.main,
".maxpat": maxpat_textconv.main,
".als": als_textconv.main,
}
file_extension = os.path.splitext(path)[1]
if file_extension in mains:
# route std output to a cystom StringIO
old_stdout = sys.stdout
sys.stdout = actualStringIo = StringIO()
# set the main arguments
old_sys_argv = sys.argv
sys.argv = [old_sys_argv[0]] + [path]
# call the main function of the appropriate script
try:
patch("sys.argv", ["prog", path])
mains[file_extension]()
finally:
sys.argv = old_sys_argv
sys.stdout = old_stdout
return actualStringIo.getvalue()[:-1]
return ""