forked from shotgunsoftware/rv-integration-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshotgun_xmlrpc.mu
503 lines (415 loc) · 12.9 KB
/
shotgun_xmlrpc.mu
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
documentation: """
The xmlrpc module implements XML-RPC (remote procedure call via a web
server using XML to hold parameter data structres). The main entry
point is the call() function. You will need to know how to create a
xmlrpc.Value tree in order to create parameter values.
""";
module: shotgun_xmlrpc {
use io;
use encoding;
require math;
require commands;
documentation: """
The Value union is a tree data structure which can hold an arbitrary
XML-RPC value. Each parameter to the call() function is a Value true. The
RPC will result in a single Value.
""";
union: Value
{
Nil // appears to be common
| ErrorValue // internal error
| Int int
| String string
| Bool bool
| Double float
| DateTime (int,int,int,int,int,int)
| Binary byte[]
| Struct [(string, Value)]
| EmptyArray
| Array [Value]
}
ReturnFunc := (void; Value);
class: ResponseValue
{
bool fault;
Value value;
}
function: reverse ([Value]; [Value] a)
{
[Value] n;
for_each (p; a) n = p : n;
n;
}
function: reverse ([(string, Value)]; [(string, Value)] a)
{
[(string, Value)] n;
for_each (p; a) n = p : n;
n;
}
function: posixRegex (regex; string pattern)
{
if (runtime.build_os() == "WINDOWS") return regex(pattern, 16);
else return regex(pattern);
}
function: outputValue (void; ostream o, Value v)
{
print(o, "<value>");
case (v)
{
Nil -> { print(o, "</nil>"); }
Int i -> { print(o, "<int>%d</int>" % i); }
String s -> { print(o, "<string>%s</string>" % s); }
Bool b -> { print(o, "<boolean>%d</boolean>" % (if b then 1 else 0)); }
Double f -> { print(o, "<double>%g</double>" % f); }
EmptyArray -> { print(o, "<array><data></data></array>"); }
DateTime d -> { print(o, "<dateTime.iso8601>%04d-%02d-%02dT%02d:%02d:%02d</dateTime.iso8601>" % d); }
Struct s ->
{
print(o, "<struct>");
for_each (p; s)
{
print(o, "<member><name>%s</name>" % p._0);
outputValue(o, p._1);
print(o, "</member>");
}
print(o, "</struct>");
}
Array a ->
{
print(o, "<array><data>");
for_each (e; a) outputValue(o, e);
print(o, "</data></array>");
}
Binary b ->
{
print(o, "<base64>");
print(o, utf8_to_string(to_base64(b)));
print(o, "</base64>");
}
}
print(o, "</value>");
}
function: outputParamList (void; ostream o, [Value] vlist)
{
print(o, "<params>");
for_each (v; vlist)
{
print(o, "<param>");
outputValue(o, v);
print(o, "</param>");
}
print(o, "</params>");
}
function: outputMethod (void; ostream o, string methodName, [Value] paramList)
{
print(o, "<?xml version=\"1.0\"?>");
print(o, "<methodCall><methodName>%s</methodName>" % methodName);
outputParamList(o, paramList);
print(o, "</methodCall>");
}
function: elementSplit (string[]; string instring)
{
use encoding;
let barray = string_to_utf8(instring);
string[] array;
byte[] temp;
for (int i = 0, is = barray.size(), i0 = 0, i1 = 0; i < is; i = i1)
{
for (i0 = i; i0 < is; i0++)
{
temp.push_back(barray[i0]);
if (barray[i0] == byte('>')) break;
}
for (i1 = i0 + 1; i1 < is; i1++)
{
let b = barray[i1];
if (b != byte(' ') && b != byte('\n') && b != byte('\r')) break;
}
if (i1 < is && barray[i1] == byte('<'))
{
let str = utf8_to_string(temp),
same = false;
if (!array.empty())
{
let last = array.back(),
len = math.min(last.size(), str.size());
if (len > 2 && str[1] == '/')
{
same = true;
for (int j = 2; j < len && same == true; ++j)
{
if (last[j-1] != str[j]) same = false;
}
}
}
if (same) array.back() = array.back()+str;
else array.push_back(str);
temp.clear();
}
else
{
for (int q = i0 + 1; q < i1 && q < is; q++)
{
temp.push_back(barray[q]);
}
}
}
if (!temp.empty()) array.push_back(utf8_to_string(temp));
array;
}
function: parseResult (Value; string xml)
{
use Value;
class: ParseState
{
int index;
string[] lines;
method: next (void;) { index++; }
method: line (string;) { lines[index]; }
method: ensure (void; string value)
{
if (line() != value)
{
string msg = "XML parsing error: saw %s but expected %s\n" % (line(), value);
print("ERROR: %s\n" % msg);
throw exception(msg);
}
}
method: nextIf (void; string value) { ensure(value); next(); }
}
/*
let re = regex(">[\\t\\r\\n ]*<"),
lines = re.replace(xml, ">\b<").split("\b"); // funky
*/
let lines = elementSplit (xml);
\: parseStruct (Value; ParseState state)
{
state.nextIf("<struct>");
[(string, Value)] members;
while (state.line() != "</struct>")
{
state.nextIf("<member>");
let re = posixRegex("<name>([^<]+)"),
name = re.smatch(state.line())[1];
state.next();
members = (name, parseValue(state)) : members;
state.nextIf("</member>");
}
state.nextIf("</struct>");
return Struct(reverse(members));
}
\: parseArray (Value; ParseState state)
{
state.nextIf("<array>");
[Value] list = nil;
//
// Allow for mal-formed xml from shotgun.
//
if (state.line() == "<data/>") state.next();
else
{
state.nextIf("<data>");
while (state.line() != "</data>")
{
list = parseValue(state) : list;
}
state.nextIf("</data>");
}
state.nextIf("</array>");
if (list eq nil) return EmptyArray;
return Array(reverse(list));
}
\: parseValue (Value; ParseState state)
{
let skipClosingValue = posixRegex("^<double>").match(state.line());
if (!skipClosingValue) state.nextIf("<value>");
string line = state.line();
Value value = ErrorValue;
case (line)
{
"<struct>" -> { value = parseStruct(state); }
"<array>" -> { value = parseArray(state); }
"<nil/>" -> { value = Nil; state.next(); }
_ ->
{
//
// Scalar tags
//
let re = posixRegex("(<[^>]+>).*(</[^>]+>)"),
parts = re.smatch(line),
start = parts[1],
end = parts[2];
let re2 = posixRegex("<[^>]+>([^<]*)</[^>]+>"),
parts2 = re2.smatch(line),
body = parts2[1];
case (start)
{
"<i4>" -> { value = Int(int(body)); }
"<int>" -> { value = Int(int(body)); }
"<string>" -> { value = String(body); }
"<boolean>" -> { value = Bool(int(body) == 1); }
"<double>" -> { value = Double(float(body)); }
"<dateTime.iso8601>" ->
{
let dre = posixRegex("([0-9]{4})-?([0-9]{2})-?([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})"),
d = dre.smatch(body);
assert(d.size() == 7);
value = DateTime((int(d[1]), int(d[2]), int(d[3]),
int(d[4]), int(d[5]), int(d[6])));
}
"<base64>" ->
{
value = Binary(from_base64(string_to_utf8(body)));
}
_ ->
{
throw exception("XML parser failure in shotgun_xmlrpc: unknown tag %s" % start);
}
}
state.next();
}
}
if (!skipClosingValue) state.nextIf("</value>");
return value;
}
\: parseParam (Value; ParseState state)
{
state.nextIf("<param>");
let v = parseValue(state);
state.nextIf("</param>");
return v;
}
//
// Top level
//
for_index (i; lines)
{
let line = lines[i];
if (line == "<fault>")
{
let Struct [ (_, Int code), (_, String why) ] = parseValue(ParseState(i+1, lines));
print("ERROR: XML-RPC: faultCode=%d -- %s\n" % (code, why));
break;
}
if (line == "<params>") return parseParam(ParseState(i+1, lines));
}
return Nil;
}
global (int,float)[] inFlight;
function: addInFlight(int hash)
{
inFlight.push_back((hash, commands.theTime()));
}
function: removeInFlight(int hash)
{
let newList = (int,float)[]();
for_index (i; inFlight)
{
if (inFlight[i]._0 != hash) newList.push_back(inFlight[i]);
}
inFlight = newList;
/*
XXX
should be able to do the below. the fact that we can't indicates a bug in mu, i think.
let index = -1;
for_index(i; inFlight)
{
print ("****************** i %s\n" % i);
print ("****************** tuple %s\n" % string(inFlight[i]));
if (inFlight[i]._0 == hash) index = i;
}
if (index != -1) inFlight.erase(index,1);
*/
}
function: inFlightStats((int, float); )
{
if (inFlight.size() != 0) return (inFlight.size(), inFlight.back()._1);
else return (0, 0.0);
}
documentation: """
call() takes the url of the server, the name of the method to call, a list
of Value trees for the parameters, and a function to call with the result
when it is available.
call() will return immediately. The rvalFunc will be called at some later
point.
""";
function: call (void;
string url,
string name,
[Value] params,
ReturnFunc rvalFunc)
{
use commands;
use app_utils;
let str = osstream();
outputMethod(str, name, params);
let xml = string(str),
hash = string.hash(xml + url + name + string(theTime())),
revent = "shotgun_xmlrpc-%s-return" % hash,
aevent = "shotgun_xmlrpc-%s-authenticate" % hash,
eevent = "shotgun_xmlrpc-%s-error" % hash;
\: returnFunc (void; Event event)
{
//print("DEBUG: XML-RCP: Returned\n");
for_each (event; [revent, aevent, eevent]) unbind(event);
use Value;
removeInFlight(hash);
Value v = ErrorValue;
try
{
v = parseResult(event.contents());
}
catch (exception exc)
{
print("ERROR: XML-RPC: Parsing Failed: %s\n" % exc);
print("DEBUG: Value = %s\n" % v);
print("DEBUG: XML = %s\n" % event.contents());
}
try
{
rvalFunc(v);
}
catch (...) { ; }
}
\: errorFunc (void; Event event)
{
for_each (event; [revent, aevent, eevent]) unbind(event);
removeInFlight(hash);
throw exception("ERROR: XMP-RPC: method \"%s\"" % name);
}
\: authenticateFunc (void; Event event)
{
for_each (event; [revent, aevent, eevent]) unbind(event);
throw exception("ERROR: XMP-RPC: method \"%s\" authentication not implementated" % name);
}
bind(revent, returnFunc);
bind(aevent, authenticateFunc);
bind(eevent, errorFunc);
addInFlight(hash);
//print("DEBUG: XML-RCP: Sending\n");
httpPost(url,
[("Content-Type", "text/xml")],
xml,
revent, aevent, eevent,
true /* ignoreSslErrors */);
}
\: test (void;)
{
use Value;
let p1 = Struct( [ ("script_name", String("rv") ),
("script_key", String("4b1676497a208c845b12f5c6734daf9d6e7c6274")) ] );
let p2 = Struct( [ ("paging",
Struct( [("current_page", Int(1)),
("entities_per_page", Int(500))] )),
("filters",
Struct( [("conditions", EmptyArray),
("logical_operator", String("and"))] )),
("type", String("Shot"))
] );
\: F (void; Value v)
{
print("F = %s\n" % v);
}
call("https://tweak.shotgunstudio.com/api3_preview/", "read", [p1, p2], F);
}
} // module shotgun_xmlrpc