-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeclient.py
306 lines (251 loc) · 9.2 KB
/
pipeclient.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Automate Audacity via mod-script-pipe.
Pipe Client may be used as a command-line script to send commands to
Audacity via the mod-script-pipe interface, or loaded as a module.
Requires Python 2.7 or later. Python 3 strongly recommended.
======================
Command Line Interface
======================
usage: pipeclient.py [-h] [-t] [-s ] [-d]
Arguments
---------
-h,--help: optional
show short help and exit
-t, --timeout: float, optional
timeout for reply in seconds (default: 10)
-s, --show-time: bool, optional
show command execution time (default: True)
-d, --docs: optional
show this documentation and exit
Example
-------
$ python3 pipeclient.py -t 20 -s False
Launches command line interface with 20 second time-out for
returned message, and don't show the execution time.
When prompted, enter the command to send (not quoted), or 'Q' to quit.
$ Enter command or 'Q' to quit: GetInfo: Type=Tracks Format=LISP
============
Module Usage
============
Note that on a critical error (such as broken pipe), the module just exits.
If a more graceful shutdown is required, replace the sys.exit()'s with
exceptions.
Example
-------
# Import the module:
>>> import pipeclient
# Create a client instance:
>>> client = pipeclient.PipeClient()
# Send a command:
>>> client.write("Command", timer=True)
# Read the last reply:
>>> print(client.read())
See Also
--------
PipeClient.write : Write a command to _write_pipe.
PipeClient.read : Read Audacity's reply from pipe.
Copyright Steve Daulton 2018
Released under terms of the GNU General Public License version 2:
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html />
"""
import os
import sys
import threading
import time
import errno
import argparse
if sys.version_info[0] < 3 and sys.version_info[1] < 7:
sys.exit('PipeClient Error: Python 2.7 or later required')
# Platform specific constants
if sys.platform == 'win32':
WRITE_NAME = '\\\\.\\pipe\\ToSrvPipe'
READ_NAME = '\\\\.\\pipe\\FromSrvPipe'
EOL = '\r\n\0'
else:
# Linux or Mac
PIPE_BASE = '/tmp/audacity_script_pipe.'
WRITE_NAME = PIPE_BASE + 'to.' + str(os.getuid())
READ_NAME = PIPE_BASE + 'from.' + str(os.getuid())
EOL = '\n'
class PipeClient():
"""Write / read client access to Audacity via named pipes.
Normally there should be just one instance of this class. If
more instances are created, they all share the same state.
__init__ calls _write_thread_start() and _read_thread_start() on
first instantiation.
Parameters
----------
None
Attributes
----------
reader_pipe_broken : event object
Set if pipe reader fails. Audacity may have crashed
reply_ready : event object
flag cleared when command sent and set when response received
timer : bool
When true, time the command execution (default False)
reply : string
message received when Audacity completes the command
See Also
--------
write : Write a command to _write_pipe.
read : Read Audacity's reply from pipe.
"""
reader_pipe_broken = threading.Event()
reply_ready = threading.Event()
_shared_state = {}
def __new__(cls, enc='', *p, **k):
self = object.__new__(cls, *p, **k)
self.__dict__ = cls._shared_state
return self
def __init__(self, enc=''):
self.timer = False
self._start_time = 0
self._write_pipe = None
self.reply = ''
self.enc = enc
if not self._write_pipe:
self._write_thread_start()
self._read_thread_start()
def _write_thread_start(self):
"""Start _write_pipe thread"""
# Pipe is opened in a new thread so that we don't
# freeze if Audacity is not running.
write_thread = threading.Thread(target=self._write_pipe_open)
write_thread.daemon = True
write_thread.start()
# Allow a little time for connection to be made.
time.sleep(0.1)
if not self._write_pipe:
sys.exit('PipeClientError: Write pipe cannot be opened.')
def _write_pipe_open(self):
"""Open _write_pipe."""
if self.enc:
self._write_pipe = open(WRITE_NAME, 'w', newline='',
encoding=self.enc)
else:
self._write_pipe = open(WRITE_NAME, 'w', newline='')
def _read_thread_start(self):
"""Start read_pipe thread."""
read_thread = threading.Thread(target=self._reader)
read_thread.daemon = True
read_thread.start()
def write(self, command, timer=False):
"""Write a command to _write_pipe.
Parameters
----------
command : string
The command to send to Audacity
timer : bool, optional
If true, time the execution of the command
Example
-------
write("GetInfo: Type=Labels", timer=True):
"""
self.timer = timer
print('Sending command:', command)
self._write_pipe.write(command + EOL)
# Check that read pipe is alive
if PipeClient.reader_pipe_broken.isSet():
sys.exit('PipeClient: Read-pipe error.')
try:
self._write_pipe.flush()
if self.timer:
self._start_time = time.time()
self.reply = ''
PipeClient.reply_ready.clear()
except IOError as err:
if err.errno == errno.EPIPE:
sys.exit('PipeClient: Write-pipe error.')
else:
raise
def _reader(self):
"""Read FIFO in worker thread."""
# Thread will wait at this read until it connects.
# Connection should occur as soon as _write_pipe has connected.
read_pipe = None
if self.enc:
read_pipe = open(READ_NAME, 'r', newline='', encoding=self.enc)
else:
read_pipe = open(READ_NAME, 'r', newline='')
message = ''
pipe_ok = True
while pipe_ok:
line = read_pipe.readline()
# Stop timer as soon as we get first line of response.
stop_time = time.time()
while pipe_ok and line != '\n':
message += line
line = read_pipe.readline()
if line == '':
# No data in read_pipe indicates that the pipe is broken
# (Audacity may have crashed).
PipeClient.reader_pipe_broken.set()
pipe_ok = False
if self.timer:
xtime = (stop_time - self._start_time) * 1000
message += 'Execution time: {0:.2f}ms'.format(xtime)
self.reply = message
PipeClient.reply_ready.set()
message = ''
read_pipe.close()
def read(self):
"""Read Audacity's reply from pipe.
Returns
-------
string
The reply from the last command sent to Audacity, or null string
if reply not received. Null string usually indicates that Audacity
is still processing the last command.
"""
if not PipeClient.reply_ready.isSet():
return ''
return self.reply
def bool_from_string(strval):
"""Return boolean value from string"""
if strval.lower() in ('true', 't', '1', 'yes', 'y'):
return True
if strval.lower() in ('false', 'f', '0', 'no', 'n'):
return False
raise argparse.ArgumentTypeError('Boolean value expected.')
def main():
"""Interactive command-line for PipeClient"""
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--timeout', type=float, metavar='', default=10,
help="timeout for reply in seconds (default: 10")
parser.add_argument('-s', '--show-time', metavar='True/False',
nargs='?', type=bool_from_string,
const='t', default='t', dest='show',
help='show command execution time (default: True)')
parser.add_argument('-d', '--docs', action='store_true',
help='show documentation and exit')
parser.add_argument('-e', '--pipe-encoding', type=str, default='',
help='non-default encoding to use for r/w pipes')
args = parser.parse_args()
if args.docs:
print(__doc__)
sys.exit(0)
client = PipeClient(enc=args.pipe_encoding)
while True:
reply = ''
if sys.version_info[0] < 3:
message = raw_input("\nEnter command or 'Q' to quit: ")
else:
message = input("\nEnter command or 'Q' to quit: ")
start = time.time()
if message.upper() == 'Q':
sys.exit(0)
elif message == '':
pass
else:
client.write(message, timer=args.show)
while reply == '':
time.sleep(0.1) # allow time for reply
if time.time() - start > args.timeout:
reply = 'PipeClient: Reply timed-out.'
else:
reply = client.read()
print(reply)
if __name__ == '__main__':
main()