audacia/scripts/piped-work/pipe_test.py

81 lines
2.1 KiB
Python
Raw Normal View History

2019-03-22 14:16:18 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2019-03-22 14:16:18 +00:00
"""Tests the audacity pipe.
Keep pipe_test.py short!!
You can make more complicated longer tests to test other functionality
or to generate screenshots etc in other scripts.
Make sure Audacity is running first and that mod-script-pipe is enabled
before running this script.
Requires Python 2.7 or later. Python 3 is strongly recommended.
"""
import os
import sys
2019-03-22 14:16:18 +00:00
if sys.platform == 'win32':
print("pipe-test.py, running on windows")
TONAME = '\\\\.\\pipe\\ToSrvPipe'
FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
EOL = '\r\n\0'
else:
2019-03-22 14:16:18 +00:00
print("pipe-test.py, running on linux or mac")
TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
EOL = '\n'
2019-03-22 14:16:18 +00:00
print("Write to \"" + TONAME +"\"")
if not os.path.exists(TONAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
print("Read from \"" + FROMNAME +"\"")
if not os.path.exists(FROMNAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
2019-03-22 14:16:18 +00:00
print("-- Both pipes exist. Good.")
2019-03-22 14:16:18 +00:00
TOFILE = open(TONAME, 'w')
print("-- File to write to has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File to read from has now been opened too\r\n")
2019-03-22 14:16:18 +00:00
def send_command(command):
"""Send a single command."""
print("Send: >>> \n"+command)
TOFILE.write(command + EOL)
TOFILE.flush()
2019-03-22 14:16:18 +00:00
def get_response():
"""Return the command response."""
result = ''
line = ''
while True:
result += line
2019-03-22 14:16:18 +00:00
line = FROMFILE.readline()
if line == '\n' and len(result) > 0:
break
return result
2019-03-22 14:16:18 +00:00
def do_command(command):
"""Send one command, and return the response."""
send_command(command)
response = get_response()
print("Rcvd: <<< \n" + response)
return response
2019-03-22 14:16:18 +00:00
def quick_test():
"""Example list of commands."""
do_command('Help: Command=Help')
do_command('Help: Command="GetInfo"')
#do_command('SetPreference: Name=GUI/Theme Value=classic Reload=1')
2019-03-22 14:16:18 +00:00
quick_test()