-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.py
343 lines (274 loc) · 11.7 KB
/
client.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
ptsl Client - Client class and client context manager.
"""
import io
from contextlib import contextmanager
import json
import sys
import time
from typing import Optional
import grpc
from google.protobuf import json_format
from ptsl import PTSL_pb2_grpc
from ptsl import PTSL_pb2 as pt
from ptsl.errors import CommandError
from ptsl.ops import Operation
PTSL_VERSION = 3
@contextmanager
def open_client(*args, **kwargs):
client = Client(*args, **kwargs)
try:
yield client
finally:
client.close()
class Auditor:
"""
The Auditor is used by the client for reporting-out the status of requests
as they are run.
"""
def __init__(self, enabled: bool) -> None:
self.output_stream = sys.stderr
self.command_sn = 1
self.enabled = enabled
def emit(self, message):
if self.enabled:
time_str = time.strftime("[%Y-%m-%d %H:%M:%S]")
print("%04i%s %s" % (self.command_sn, time_str, message),
file=self.output_stream)
def run_called(self, command: pt.CommandId):
self.emit(f"Started Command {pt.CommandId.Name(command)} " +
"({command})")
def request_json_before_cleanup(self, json_data):
self.emit(f"Created JSON for request message: {json_data}")
def request_json_after_cleanup(self, json_data):
self.emit(f"Re-formatted JSON for request message: {json_data}")
def response_json_before_cleanup(self, json_data):
self.emit(f"Received JSON response body: {json_data}")
def response_json_after_cleanup(self, json_data):
self.emit(f"Re-formatted JSON response body: {json_data}")
def response_was_empty(self):
self.emit("Received empty JSON response")
def run_returning(self):
self.emit("Finished Command")
self.emit(f"{('*' * 60)}\n\n")
self.command_sn += 1
class Client:
"""
Medium-level PTSL interface.
The Client class:
- maintains the grpc stub and channel
- holds PTSL server session data
- manages the connection's registration
- runs `Operation`s on the grpc channel
- packages error responses as exceptions to be thrown.
"""
channel: grpc.Channel
raw_client: PTSL_pb2_grpc.PTSLStub
session_id: str
auditor: Auditor
is_open: bool
def __init__(self,
company_name: Optional[str] = None,
application_name: Optional[str] = None,
certificate_path: Optional[str] = None,
address: str = 'localhost:31416') -> None:
"""
Creates a new client.
..note:: If `certificate_path` is given, the legacy AuthorizeConnection
method will be used for setting up the connection session. If it is
`None`, then `company_name` and `application_name` will be used
with the RegisterConnection method (available since Pro Tools
2023.3).
"""
self.channel = grpc.insecure_channel(address)
self.raw_client = PTSL_pb2_grpc.PTSLStub(self.channel)
self.session_id = ""
self.auditor = Auditor(enabled=False)
try:
self._primitive_check_if_ready()
if certificate_path is not None:
self._primitive_authorize_connection(certificate_path)
elif company_name is not None and application_name is not None:
self._primitive_register_connection(
company_name, application_name)
else:
raise AssertionError(
"company_name and application_name parameters were " +
"not given")
self.is_open = True
except grpc.RpcError as grpc_error:
self.close()
if getattr(grpc_error, 'code')() == grpc.StatusCode.UNAVAILABLE:
print("gRPC endpoint was unavailable, Pro Tools " +
"may not be running.", file=sys.stderr)
raise grpc_error
def run(self, operation: Operation) -> None:
"""
Run an operation on the client.
:raises: `CommandError` if the server returns an error
"""
self.auditor.run_called(operation.command_id())
# convert the request body into JSON
request_body_json = self._prepare_operation_request_json(operation)
response = self._send_sync_request(operation.command_id(),
request_body_json)
operation.status = response.header.status
if response.header.status == pt.Failed:
cleaned_response_error_json = response.response_error_json
# self._response_error_json_cleanup(
# response.response_error_json)
command_errors = json_format.Parse(cleaned_response_error_json,
pt.ResponseError())
raise CommandError(command_errors.errors)
elif response.header.status == pt.Completed:
self._handle_completed_response(operation, response)
else:
# FIXME: dump out for now, will be on the lookout for when
# this happens
assert False, \
f"Unexpected response code {response.header.status} " + \
f"({pt.TaskStatus.Name(response.header.status)})"
self.auditor.run_returning()
def _prepare_operation_request_json(self, operation):
"""
Convert the request body into a JSON string (or an empty string if
there is no request body.
"""
if operation.request is None:
request_body_json = ""
else:
request_body_json = \
json_format.MessageToJson(operation.request,
always_print_fields_with_no_presence=True,
# including_default_value_fields=True,
preserving_proto_field_name=True)
self.auditor.request_json_before_cleanup(request_body_json)
request_body_json = operation.json_messup(request_body_json)
self.auditor.request_json_after_cleanup(request_body_json)
return request_body_json
def _response_error_json_cleanup(self, json_in: str) -> str:
"""
This is a shim that will take a `command_error_type` value
from the response error json and convert it into a PT_UnknownError
if the `command_error_type` value is mis-formatted by the server
(for instance, if the server returns a symbold name or numeric string
value, as it sometimes has done in the past. (See `errata`).
"""
errors = json.loads(json_in)
error_dict = errors['errors'][0]
old_val = errors['errors'][0]['command_error_type']
if isinstance(old_val, str):
if old_val.isdigit():
error_dict['command_error_type'] = int(old_val)
elif old_val in pt.CommandErrorType.keys():
error_dict['command_error_type'] = \
pt.CommandErrorType.Value(old_val)
else:
error_dict['command_error_type'] = pt.PT_UnknownError
return json.dumps(error_dict)
def _handle_completed_response(self, operation, response):
"""
Accept the response message from the server, parse
the response body JSON if present, and hand the results
to the operation.
If the operation provides a cleanup function, this is run on
the response body JSON prior to parsing.
"""
p = operation.__class__.response_body()
if len(response.response_body_json) > 0 and p is not None:
self.auditor.response_json_before_cleanup(
response.response_body_json)
clean_json = operation.json_cleanup(response.response_body_json)
self.auditor.response_json_after_cleanup(clean_json)
resp_body = json_format.Parse(clean_json, p(),
ignore_unknown_fields=True)
operation.on_response_body(resp_body)
else:
operation.on_empty_response_body()
self.auditor.response_was_empty()
def _send_sync_request(self, command_id,
request_body_json, task_id="") -> pt.Response:
"""
Send a synchronous request to the server.
"""
request = pt.Request(
header=pt.RequestHeader(
task_id=task_id,
session_id=self.session_id,
command=command_id,
version=PTSL_VERSION
),
request_body_json=request_body_json
)
response = self.raw_client.SendGrpcRequest(request)
return response
def close(self):
"""
Closes the client.
"""
self.is_open = False
self.channel.close()
self.session_id = ""
def _primitive_check_if_ready(self) -> bool:
"""
Checks if the Pro Tools RPC server is listening. The server will
respond to this command even if the client is not yet authenticated.
"""
response = self._send_sync_request(pt.HostReadyCheck, "")
if response.header.status == pt.Failed:
print("Pro Tools Not Ready")
print(response)
return False
else:
return True
def _primitive_register_connection(self, company_name: str,
application_name: str):
"""
Registers the client's connection to the Pro Tools RPC server.
This method is called automatically by the initializer.
"""
req = pt.RegisterConnectionRequestBody(
company_name=company_name,
application_name=application_name)
req_json = json_format.MessageToJson(
req,
# including_default_value_fields=True,
preserving_proto_field_name=True)
response = self._send_sync_request(pt.RegisterConnection,
req_json)
if response.header.status == pt.Failed:
print("An error occurred")
print(response)
else:
registration_response = \
json_format.Parse(response.response_body_json,
pt.RegisterConnectionResponseBody())
self.session_id = registration_response.session_id
def _primitive_authorize_connection(self, api_key_path) -> Optional[str]:
"""
(Deprecated) Authorizes the client's connection to the Pro Tools RPC
server.
This method is called automatically by the initializer.
"""
print("WARNING: Certificate authorization method is deprecated",
file=sys.stderr)
KEY_FILE_ENCODING = 'ascii'
with io.FileIO(api_key_path) as f:
api_token = f.readall().decode(encoding=KEY_FILE_ENCODING)
req = pt.AuthorizeConnectionRequestBody(auth_string=api_token)
req_json = json_format.MessageToJson(req,
preserving_proto_field_name=True)
response = self._send_sync_request(pt.AuthorizeConnection,
req_json)
if response.header.status == pt.Failed:
print("An error occurred")
print(response)
else:
authorization_response = \
json_format.Parse(response.response_body_json,
pt.AuthorizeConnectionResponseBody())
if authorization_response.is_authorized:
self.session_id = authorization_response.session_id
else:
print("Connection did not authorize, message: " +
authorization_response.message)