Repository navigation
Expand file tree
/
Copy pathgenerate_api.js
More file actions
1151 lines (1069 loc) · 38.8 KB
/
Copy pathgenerate_api.js
File metadata and controls
1151 lines (1069 loc) · 38.8 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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-check
// Generates the public Python API (playwright/{async,sync}_api/_generated.py)
// and its TypedDicts (playwright/_impl/_api_structures.py) from the upstream
// Playwright docs (docs/src/api in microsoft/playwright).
//
// Usage:
// node scripts/codegen/generate_api.js --flavor async|sync|structs [--out <file>]
//
// The docs come from .upstream/, the microsoft/playwright checkout at the commit
// in DRIVER_VERSION (scripts/upstream.py).
//
// The docs are parsed with the upstream parser (utils/doclint/api_parser.js) and
// filtered for Python. Facts that the docs do not contain come from
// python_rules.js. The output is not formatted; run isort and black on it.
'use strict';
const fs = require('fs');
const path = require('path');
const rules = require('./python_rules');
/**
* @typedef {{
* kind: 'name' | 'class' | 'struct' | 'union' | 'list' | 'dict' | 'callable' | 'literal' | 'pattern' | 'awaitable' | 'ecm' | 'cm',
* name?: string,
* wrapper?: boolean,
* members?: PyType[],
* item?: PyType,
* key?: PyType,
* value?: PyType,
* args?: PyType[] | null,
* ret?: PyType,
* values?: string[],
* inner?: PyType | null,
* docType?: any,
* place?: string,
* dir?: 'in' | 'out',
* }} PyType
*/
/**
* @typedef {{
* name: string,
* type: PyType,
* optional: boolean,
* comment: string,
* deprecated: string | undefined,
* documented: boolean,
* }} PyParam
*/
/**
* @typedef {{
* kind: 'property' | 'method',
* name: string,
* isAsync: boolean,
* positional: PyParam[],
* keywordOnly: PyParam[],
* returnType: PyType | null,
* comment: string | null,
* documented: boolean,
* }} PyMember
*/
/**
* @typedef {{
* name: string,
* base: string,
* events: any[],
* members: PyMember[],
* }} PyClass
*/
// ---------------------------------------------------------------------------
// Command line and upstream modules.
function parseArgs(argv) {
const result = { flavor: 'async', out: '' };
for (let i = 0; i < argv.length; i++) {
if (argv[i] === '--flavor')
result.flavor = argv[++i];
else if (argv[i] === '--out')
result.out = argv[++i];
else
fail(`Unknown argument: ${argv[i]}`);
}
if (!['async', 'sync', 'structs'].includes(result.flavor))
fail(`--flavor must be "async", "sync" or "structs", got "${result.flavor}"`);
return result;
}
/** @param {string} message */
function fail(message) {
console.error(message);
process.exit(1);
}
const options = parseArgs(process.argv.slice(2));
const isAsync = options.flavor === 'async';
const UPSTREAM_DIR = path.join(__dirname, '..', '..', '.upstream');
if (!fs.existsSync(path.join(UPSTREAM_DIR, 'docs', 'src', 'api')))
fail('.upstream/ is missing. Run: python scripts/upstream.py checkout');
const { parseApi } = require(path.join(UPSTREAM_DIR, 'utils', 'doclint', 'api_parser'));
const { Type, parseCodeLang } = require(path.join(UPSTREAM_DIR, 'utils', 'doclint', 'documentation'));
const md = require(path.join(UPSTREAM_DIR, 'utils', 'markdown'));
const BASE_CLASS = isAsync ? 'AsyncBase' : 'SyncBase';
const CONTEXT_MANAGER_CLASS = isAsync ? 'AsyncContextManager' : 'SyncContextManager';
const EVENT_CONTEXT_MANAGER_CLASS = isAsync ? 'AsyncEventContextManager' : 'EventContextManager';
/** @type {string[]} */
const errors = [];
/** @type {Set<string>} */
const usedRules = new Set();
/** @type {Set<string>} */
const usedStructs = new Set();
/** @type {Map<string, PyType[]>} every use of each struct name */
const structUses = new Map();
// ---------------------------------------------------------------------------
// Names.
/**
* Compares by name, in code point order (as Python's sorted()). The generated
* files list classes, members, keyword-only parameters, events and
* TypedDicts in this order.
* @param {string} a
* @param {string} b
*/
function byName(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
/** @param {string} name */
function toSnakeCase(name) {
// Same as to_snake_case() in playwright/_impl/isomorphic/string_utils.py.
return name.replace(/((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))/g, '_$1').toLowerCase();
}
/**
* Looks up a rule and records that it was used.
* @param {string} table
* @param {string} key
*/
function lookupRule(table, key) {
const entries = /** @type {any} */ (rules)[table];
if (Object.prototype.hasOwnProperty.call(entries, key)) {
usedRules.add(`${table}:${key}`);
return entries[key];
}
for (const pattern of Object.keys(entries)) {
if (!pattern.includes('*'))
continue;
const regex = new RegExp('^' + pattern.split('*').map(escapeRegExp).join('[^.]+') + '$');
if (regex.test(key)) {
usedRules.add(`${table}:${pattern}`);
return entries[pattern];
}
}
return undefined;
}
/** @param {string} text */
function escapeRegExp(text) {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// ---------------------------------------------------------------------------
// Docs.
function loadDocumentation() {
const documentation = parseApi(path.join(UPSTREAM_DIR, 'docs', 'src', 'api'));
documentation.filterForLanguage('python');
return documentation;
}
const documentation = loadDocumentation();
/** @type {Set<string>} */
const generatedClassNames = new Set(documentation.classesArray
.map(c => c.name)
.filter(name => !rules.handWrittenClasses.includes(name)));
/** @param {any} member */
function isPythonProperty(member) {
if (member.kind === 'property')
return true;
if (member.kind !== 'method' || member.async || member.argsArray.length)
return false;
const name = toSnakeCase(member.alias);
return !name.startsWith('is_') && !name.startsWith('as_') && name !== 'connect_to_server';
}
/** @param {any} member */
function memberLink(member) {
const variable = toSnakeCase(member.clazz.varName);
if (member.kind === 'event')
return '`' + `${variable}.on('${member.alias.toLowerCase()}')` + '`';
if (isPythonProperty(member))
return '`' + `${variable}.${toSnakeCase(member.alias)}` + '`';
return '`' + `${variable}.${toSnakeCase(member.alias)}()` + '`';
}
documentation.setLinkRenderer(({ clazz, member, param, option }) => {
if (param)
return '`' + toSnakeCase(param.alias) + '`';
if (option)
return '`' + option.alias.split('.').map(toSnakeCase).join('.') + '`';
if (member)
return memberLink(member);
if (clazz)
return '`' + clazz.name + '`';
return undefined;
});
// ---------------------------------------------------------------------------
// Docstrings.
const KEPT_CODE_LANGUAGES = new Set(['html', 'yml', 'sh', 'py', 'python']);
/** @param {any[]} nodes */
function filterCodeBlocks(nodes) {
const result = [];
for (const node of nodes) {
if (node.type === 'code' && node.codeLang) {
if (node.codeLang === 'python async' || node.codeLang === 'python sync') {
if (node.codeLang !== `python ${options.flavor}`)
continue;
} else if (!KEPT_CODE_LANGUAGES.has(parseCodeLang(node.codeLang).highlighter)) {
continue;
}
}
if (node.children)
node.children = filterCodeBlocks(node.children);
result.push(node);
}
return result;
}
/**
* Renders a member or argument description as plain text. Same as the
* upstream generateSourceCodeComment(), but keeps only the code examples for
* Python and for this flavor.
* @param {any[] | undefined} spec
*/
function renderSpec(spec) {
const nodes = filterCodeBlocks((spec || [])
.filter(n => !n.type.startsWith('h') && (n.type !== 'li' || n.liType !== 'default'))
.map(n => md.clone(n)));
md.visitAll(nodes, node => {
if (node.type === 'li' && node.liType === 'bullet')
node.liType = 'default';
if (node.type === 'code' && node.codeLang)
node.codeLang = parseCodeLang(node.codeLang).highlighter;
});
return md.render(nodes, { maxColumns: 120 - 5, omitLastCR: true, flattenText: true, noteMode: 'compact' });
}
/** @param {string} text */
function escapeDocstring(text) {
return escapeBackslashes(text).replace(/"/g, '\\"');
}
/**
* Parameter descriptions keep their quotes: they never end the docstring.
* @param {string} text
*/
function escapeBackslashes(text) {
return text.replace(/\\/g, '\\\\');
}
/**
* @param {string} text
* @param {string} indent
*/
function indentParagraph(text, indent) {
const lines = text.split('\n');
return [lines[0], ...lines.slice(1).map(line => indent + line)].join('\n');
}
/** @param {string} text */
function resolveRelativeLinks(text) {
return text.replace(/\[([^\]]+)\]\((\.[^)]+)\)/g, (match, linkText, href) => {
const resolved = new URL(href.replace(/\.md/g, ''), 'https://playwright.dev/python/docs/api/').href;
return `[${linkText}](${resolved})`;
});
}
/**
* Member description: one blank line between paragraphs, indented.
* @param {string} text
* @param {string} indent
*/
function formatComment(text, indent) {
const result = [];
let lastWasBlank = true;
for (const line of escapeDocstring(text).split('\n')) {
if (!line.trim()) {
lastWasBlank = true;
continue;
}
if (lastWasBlank) {
lastWasBlank = false;
result.push('');
}
result.push(line);
}
return resolveRelativeLinks(indentParagraph(result.join('\n'), indent));
}
// ---------------------------------------------------------------------------
// Types.
/** @param {string} name */
const NAME = name => /** @type {PyType} */ ({ kind: 'name', name });
const ANY = NAME('Any');
const NONE = NAME('None');
/** @param {PyType} t */
const isNone = t => t.kind === 'name' && t.name === 'None';
/** @param {string} name */
function isStringLiteral(name) {
return name.startsWith('"') && name.endsWith('"');
}
/**
* @param {PyType[]} members
* @returns {PyType}
*/
function makeUnion(members) {
/** @type {PyType[]} */
const flat = [];
for (const member of members) {
if (member.kind === 'union')
flat.push(...(member.members || []));
else
flat.push(member);
}
/** @type {PyType[]} */
const result = [];
/** @type {PyType | null} */
let literal = null;
const seen = new Set();
for (const member of flat) {
if (member.kind === 'literal') {
if (!literal) {
literal = { kind: 'literal', values: [] };
result.push(literal);
}
for (const value of member.values || []) {
if (!(literal.values || []).includes(value))
literal.values?.push(value);
}
continue;
}
const key = annotation(member, false);
if (seen.has(key))
continue;
seen.add(key);
result.push(member);
}
if (literal)
literal.values?.sort();
return result.length === 1 ? result[0] : { kind: 'union', members: result };
}
/**
* @param {any} type docs Type
* @param {{ dir: 'in' | 'out', place: string, fnReturn?: boolean }} ctx
* @returns {PyType}
*/
function toPy(type, ctx) {
if (type.union)
return makeUnion(type.union.map(t => toPy(t, ctx)));
const name = type.name;
if (isStringLiteral(name))
return { kind: 'literal', values: [name.substring(1, name.length - 1)] };
switch (name) {
case 'string':
case 'URL':
return NAME('str');
case 'boolean':
return NAME('bool');
case 'int':
case 'long':
return NAME('int');
case 'float':
return NAME('float');
case 'null':
case 'void':
case 'undefined':
return NONE;
case 'any':
case 'unknown':
case 'Serializable':
case 'EvaluationArgument':
return ANY;
case 'Buffer':
case 'ReadStream':
return NAME('bytes');
case 'Date':
return NAME('datetime.datetime');
case 'RegExp':
return { kind: 'pattern' };
case 'path':
return ctx.dir === 'in' ? makeUnion([NAME('str'), NAME('pathlib.Path')]) : NAME('pathlib.Path');
case 'function': {
if (!type.args)
return { kind: 'callable', args: null, ret: ANY };
const args = type.args.map(t => toPy(t, { ...ctx, fnReturn: false }));
const ret = type.returnType ? toPy(type.returnType, { ...ctx, fnReturn: true }) : ANY;
return { kind: 'callable', args, ret };
}
case 'Promise': {
const inner = type.templates ? toPy(type.templates[0], { ...ctx, fnReturn: false }) : ANY;
// A callback may return a promise: in Python, the async API accepts an
// awaitable there; the sync API does not.
if (ctx.fnReturn && isAsync && !(inner.kind === 'name' && inner.name === 'Any'))
return { kind: 'awaitable', inner };
return inner;
}
case 'Array':
return { kind: 'list', item: toPy(type.templates[0], ctx), dir: ctx.dir };
case 'Object':
case 'Map':
if (type.templates)
return { kind: 'dict', key: toPy(type.templates[0], ctx), value: toPy(type.templates[1], ctx) };
if (type.properties && type.properties.length)
return { kind: 'struct', docType: type, place: ctx.place, dir: ctx.dir };
return { kind: 'dict', key: undefined, value: undefined };
case 'EventContextManager':
return { kind: 'ecm', inner: type.templates ? toPy(type.templates[0], ctx) : null };
case 'Disposable':
return { kind: 'cm' };
case 'Error':
case 'FormData':
return { kind: 'class', name, wrapper: false };
}
if (generatedClassNames.has(name))
return { kind: 'class', name, wrapper: true };
throw new Error(`Unknown docs type "${name}" at ${ctx.place}`);
}
/** @param {PyType} t */
function structName(t) {
const place = /** @type {string} */ (t.place);
const name = t.docType.langAliases?.python || t.docType.langAliases?.default;
if (!name) {
errors.push(`No Python name for the struct at ${place}: {${t.docType.properties.map(p => p.name).join(', ')}}`);
return 'typing.Dict';
}
usedStructs.add(name);
const uses = structUses.get(name) || [];
if (!uses.some(use => use.place === t.place && use.dir === t.dir))
uses.push(t);
structUses.set(name, uses);
return name;
}
/**
* Python annotation text.
* @param {PyType} t
* @param {boolean=} record whether to record used struct names
* @returns {string}
*/
function annotation(t, record = true) {
switch (t.kind) {
case 'name':
return t.name === 'Any' ? 'typing.Any' : /** @type {string} */ (t.name);
case 'class':
return `"${t.name}"`;
case 'struct':
return record ? structName(t) : `struct:${t.place}`;
case 'union': {
const members = t.members || [];
const others = members.filter(m => !isNone(m)).map(m => annotation(m, record));
const inner = others.length === 1 ? others[0] : `typing.Union[${others.join(', ')}]`;
return members.some(isNone) ? `typing.Optional[${inner}]` : inner;
}
case 'list':
return `typing.${t.dir === 'in' ? 'Sequence' : 'List'}[${annotation(/** @type {PyType} */ (t.item), record)}]`;
case 'dict':
if (!t.key)
return 'typing.Dict';
return `typing.Dict[${annotation(t.key, record)}, ${annotation(/** @type {PyType} */ (t.value), record)}]`;
case 'callable':
if (!t.args)
return 'typing.Callable';
return `typing.Callable[[${t.args.map(a => annotation(a, record)).join(', ')}], ${annotation(/** @type {PyType} */ (t.ret), record)}]`;
case 'literal':
return `Literal[${(t.values || []).map(v => JSON.stringify(v)).join(', ')}]`;
case 'pattern':
return 'typing.Pattern[str]';
case 'awaitable':
return `typing.Awaitable[${annotation(/** @type {PyType} */ (t.inner), record)}]`;
case 'ecm':
return t.inner ? `${EVENT_CONTEXT_MANAGER_CLASS}[${annotation(t.inner, record)}]` : EVENT_CONTEXT_MANAGER_CLASS;
case 'cm':
return `"${CONTEXT_MANAGER_CLASS}"`;
}
throw new Error(`Unknown type kind ${t.kind}`);
}
/** @param {string} text */
function makeOptional(text) {
if (text.startsWith('Union[')) {
if (text.endsWith('None]'))
return text;
return text.substring(0, text.length - 1) + ', None]';
}
return `Union[${text}, None]`;
}
/** @param {string[]} items */
function sortDocUnion(items) {
const key = item => item === 'None' ? '}' : item;
return [...items].sort((a, b) => key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0);
}
/**
* Type text for docstrings. Same format as the old generator: structs are
* expanded, unions are sorted with None last.
* @param {PyType} t
* @returns {string}
*/
function docType(t) {
switch (t.kind) {
case 'name':
return /** @type {string} */ (t.name);
case 'class':
return /** @type {string} */ (t.name);
case 'struct': {
const fields = t.docType.properties.map(p => {
const field = docType(toPy(p.type, { dir: t.dir || 'out', place: `${t.place}.${p.name}` }));
return `${p.name}: ${p.required ? field : makeOptional(field)}`;
});
return `{${fields.join(', ')}}`;
}
case 'union': {
const members = t.members || [];
if (members.length === 2 && members.some(isNone))
return makeOptional(docType(/** @type {PyType} */ (members.find(m => !isNone(m)))));
const items = [];
for (const member of members) {
if (member.kind === 'literal')
items.push(...(member.values || []).map(v => `"${v}"`));
else
items.push(docType(member));
}
return `Union[${sortDocUnion(items).join(', ')}]`;
}
case 'list':
return `${t.dir === 'in' ? 'Sequence' : 'List'}[${docType(/** @type {PyType} */ (t.item))}]`;
case 'dict':
if (!t.key)
return 'Dict';
return `Dict[${docType(t.key)}, ${docType(/** @type {PyType} */ (t.value))}]`;
case 'callable':
if (!t.args)
return 'Callable';
return `Callable[[${t.args.map(docType).join(', ')}], ${docType(/** @type {PyType} */ (t.ret))}]`;
case 'literal': {
const values = (t.values || []).map(v => `"${v}"`);
return values.length === 1 ? values[0] : `Union[${values.join(', ')}]`;
}
case 'pattern':
return 'Pattern[str]';
case 'awaitable':
return `typing.Awaitable[${docType(/** @type {PyType} */ (t.inner))}]`;
case 'ecm':
return t.inner ? `EventContextManager[${docType(t.inner)}]` : 'EventContextManager';
case 'cm':
return CONTEXT_MANAGER_CLASS;
}
throw new Error(`Unknown type kind ${t.kind}`);
}
/**
* Visits the type, but not the fields of structs: a struct crosses into the
* implementation as a plain dict, whatever its fields are.
* @param {PyType} t
* @param {(t: PyType) => boolean} predicate
* @returns {boolean}
*/
function someType(t, predicate) {
if (predicate(t))
return true;
const children = [
...(t.members || []),
...(t.args || []),
t.item, t.key, t.value, t.ret, t.inner,
].filter(Boolean);
return children.some(c => someType(/** @type {PyType} */ (c), predicate));
}
/**
* The Python expression that passes a parameter to the implementation.
* @param {PyParam} param
*/
function forwardExpression(param) {
const t = param.type;
const name = param.name;
if (someType(t, c => c.kind === 'callable'))
return `self._wrap_handler(${name})`;
if (param.name === 'timeout' && someType(t, c => c.kind === 'name' && c.name === 'float'))
return `to_milliseconds(${name})`;
if (someType(t, c =>
(c.kind === 'name' && c.name === 'Any') ||
c.kind === 'dict' ||
(c.kind === 'list' && c.dir === 'in') ||
(c.kind === 'class' && !!c.name && c.name.includes('Handle'))))
return `mapping.to_impl(${name})`;
if (t.kind === 'class' && t.wrapper)
return param.optional ? `${name}._impl_obj if ${name} else None` : `${name}._impl_obj`;
if (t.kind === 'union' && t.members?.length === 2 && t.members.some(isNone)) {
const other = /** @type {PyType} */ (t.members.find(m => !isNone(m)));
if (other.kind === 'class' && other.wrapper)
return `${name}._impl_obj if ${name} else None`;
}
// A union such as `str | Locator`: unwrap whichever value was passed.
if (someType(t, c => c.kind === 'class' && !!c.wrapper))
return `mapping.to_impl(${name})`;
return name;
}
/**
* The function that converts a return value from the implementation.
* @param {PyType | null} t
*/
function returnWrapper(t) {
if (!t || !someType(t, c => c.kind === 'class' || c.kind === 'struct' || c.kind === 'cm'))
return 'mapping.from_maybe_impl';
if (t.kind === 'union' && t.members?.length === 2 && t.members.some(isNone))
return 'mapping.from_impl_nullable';
if (t.kind === 'list')
return 'mapping.from_impl_list';
if (t.kind === 'dict')
return 'mapping.from_impl_dict';
return 'mapping.from_impl';
}
// ---------------------------------------------------------------------------
// Python model.
/**
* @param {any} clazz
* @param {any} member
* @returns {PyMember}
*/
function buildMember(clazz, member) {
const memberPlace = `${clazz.name}.${member.name}`;
const returnType = member.type.name === 'void' && !member.type.union ? null : toPy(member.type, { dir: 'out', place: memberPlace });
const isEventContextManager = returnType?.kind === 'ecm';
/** @type {{ arg: any, isOption: boolean }[]} */
const args = [];
for (const arg of member.argsArray) {
if (arg.name === 'options') {
for (const option of arg.type.properties)
args.push({ arg: option, isOption: true });
} else {
args.push({ arg, isOption: false });
}
}
/** @param {{ arg: any, isOption: boolean }} entry */
const toParam = ({ arg, isOption }) => {
const place = `${memberPlace}.${arg.name}`;
const type = toPy(arg.type, { dir: 'in', place });
const nullable = type.kind === 'union' && !!type.members?.some(isNone);
return /** @type {PyParam} */ ({
name: toSnakeCase(arg.alias),
type,
optional: isOption || !arg.required || nullable,
comment: renderSpec(arg.spec),
deprecated: arg.deprecated,
documented: true,
});
};
/** @param {{ arg: any, isOption: boolean }} entry */
const isPositional = ({ arg, isOption }) => !isOption || rules.positionalOptions.includes(arg.name);
/** @type {PyParam[]} */
const positional = args.filter(isPositional).map(toParam);
/** @type {PyParam[]} */
const keywordOnly = args.filter(a => !isPositional(a)).map(toParam);
for (const extra of lookupRule('extraOptions', memberPlace) || []) {
keywordOnly.push({
name: toSnakeCase(extra.name),
type: toPy(Type.parse(extra.type), { dir: 'in', place: `${memberPlace}.${extra.name}` }),
optional: true,
comment: '',
deprecated: undefined,
documented: false,
});
}
keywordOnly.sort((a, b) => byName(a.name, b.name));
let sawOptional = false;
for (const param of positional) {
if (sawOptional && !param.optional)
errors.push(`${memberPlace}: required parameter ${param.name} follows an optional one; fix the docs`);
sawOptional = sawOptional || param.optional;
}
const kind = isPythonProperty(member) ? 'property' : 'method';
return {
kind,
name: toSnakeCase(member.alias),
isAsync: !!member.async && !isEventContextManager,
positional,
keywordOnly,
returnType,
comment: renderSpec(member.spec),
documented: true,
};
}
/**
* @param {string} className
* @param {any} extra
* @returns {PyMember}
*/
function buildExtraMember(className, extra) {
const place = `${className}.${extra.name}`;
const returnDocsType = Type.parse(extra.returns);
return {
kind: extra.kind,
name: extra.name,
isAsync: extra.async,
positional: extra.args.map(arg => ({
name: toSnakeCase(arg.name),
type: toPy(Type.parse(arg.type), { dir: 'in', place: `${place}.${arg.name}` }),
optional: false,
comment: '',
deprecated: undefined,
documented: false,
})),
keywordOnly: [],
returnType: returnDocsType.name === 'void' ? null : toPy(returnDocsType, { dir: 'out', place }),
comment: null,
documented: false,
};
}
/**
* @param {any} clazz
* @returns {PyClass}
*/
function buildClass(clazz) {
let base = BASE_CLASS;
if (rules.contextManagerClasses.includes(clazz.name))
base = CONTEXT_MANAGER_CLASS;
else if (clazz.extends && generatedClassNames.has(clazz.extends))
base = clazz.extends;
const members = [];
for (const member of clazz.membersArray) {
if (member.kind === 'event')
continue;
members.push(buildMember(clazz, member));
}
for (const extra of lookupRule('extraMembers', clazz.name) || [])
members.push(buildExtraMember(clazz.name, extra));
members.sort((a, b) => byName(a.name, b.name));
const events = [...clazz.eventsArray].sort((a, b) => byName(a.name.toLowerCase(), b.name.toLowerCase()));
return { name: clazz.name, base, events, members };
}
/** Classes in alphabetical order, with base classes first. */
function orderedClasses() {
const classes = documentation.classesArray.filter(c => generatedClassNames.has(c.name)).sort((a, b) => byName(a.name, b.name));
const result = [];
const done = new Set();
const visit = clazz => {
if (done.has(clazz.name))
return;
if (clazz.extends && generatedClassNames.has(clazz.extends))
visit(documentation.classes.get(clazz.extends));
done.add(clazz.name);
result.push(clazz);
};
classes.forEach(visit);
return result;
}
// ---------------------------------------------------------------------------
// Output.
const LICENSE = `# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
`;
/** @param {any} event */
function eventPayload(clazz, event) {
let payload = docType(toPy(event.type, { dir: 'out', place: `${clazz.name}.${event.name}` }));
if (payload.startsWith('{'))
payload = 'typing.Dict';
return payload.replace(/Union\[/g, 'typing.Union[');
}
/**
* on() or once(), with one typed overload per event.
* @param {PyClass} clazz
* @param {'on' | 'once'} method
* @param {string[]} out
*/
function emitEventMethod(clazz, method, out) {
const events = clazz.events;
const handlerReturn = isAsync ? 'typing.Union[typing.Awaitable[None], None]' : 'None';
for (const event of events) {
out.push(' @typing.overload');
out.push(` def ${method}(self, event: Literal['${event.name.toLowerCase()}'], f: typing.Callable[['${eventPayload(clazz, event)}'], '${handlerReturn}']) -> None:`);
out.push(` """${formatComment(renderSpec(event.spec), ' '.repeat(8))}"""`);
}
if (events.length === 1) {
out.push(' @typing.overload');
out.push(` def ${method}(self, event: str, f: typing.Callable[...,${handlerReturn}]) -> None: ...`);
out.push('');
}
out.push(` def ${method}(self, event: str, f: typing.Callable[...,${handlerReturn}]) -> None:`);
out.push(` return super().${method}(event=event,f=f)`);
}
/**
* Typed overloads of expect_event() / wait_for_event(), keyed on the event name.
* @param {PyClass} clazz
* @param {string} methodName
* @param {string[]} out
*/
function emitEventOverloads(clazz, methodName, out) {
if (!clazz.events.length)
return;
const isExpect = methodName === 'expect_event';
const asyncPrefix = !isExpect && isAsync ? 'async ' : '';
for (const event of clazz.events) {
const payload = eventPayload(clazz, event);
const returnType = isExpect ? `${EVENT_CONTEXT_MANAGER_CLASS}["${payload}"]` : `"${payload}"`;
out.push(' @typing.overload');
out.push(` ${asyncPrefix}def ${methodName}(`);
out.push(' self,');
out.push(` event: typing.Literal["${event.name.toLowerCase()}"],`);
out.push(` predicate: typing.Optional[typing.Callable[["${payload}"], bool]] = None,`);
out.push(' *,');
out.push(' timeout: typing.Optional[float] = None,');
out.push(` ) -> ${returnType}: ...`);
out.push('');
}
const catchAllReturn = isExpect ? `${EVENT_CONTEXT_MANAGER_CLASS}[typing.Any]` : 'typing.Any';
out.push(' @typing.overload');
out.push(` ${asyncPrefix}def ${methodName}(`);
out.push(' self,');
out.push(' event: str,');
out.push(' predicate: typing.Optional[typing.Callable[..., bool]] = None,');
out.push(' *,');
out.push(' timeout: typing.Optional[float] = None,');
out.push(` ) -> ${catchAllReturn}: ...`);
out.push('');
}
/** @param {PyParam} param */
function paramAnnotation(param) {
let type = param.type;
if (param.name === 'timeout' && type.kind === 'name' && type.name === 'float')
type = makeUnion([NAME('float'), NAME('datetime.timedelta')]);
if (param.optional && !(type.kind === 'union' && type.members?.some(isNone)))
type = { kind: 'union', members: [type, NONE] };
return annotation(type) + (param.optional ? ' = None' : '');
}
/**
* @param {PyClass} clazz
* @param {PyMember} member
*/
function emitDocstring(clazz, member, out) {
const indent = ' '.repeat(8);
out.push(`${indent}"""${clazz.name}.${member.name}`);
if (member.comment)
out.push(`${indent}${formatComment(member.comment, indent)}`);
const params = [...member.positional, ...member.keywordOnly].filter(p => p.documented);
if (params.length) {
out.push('');
out.push(`${indent}Parameters`);
out.push(`${indent}----------`);
for (const param of params) {
const type = param.optional ? makeOptional(docType(param.type)) : docType(param.type);
out.push(`${indent}${param.name} : ${type}`);
if (param.comment)
out.push(`${indent} ${indentParagraph(escapeBackslashes(param.comment), indent + ' ')}`);
if (param.deprecated)
out.push(`${indent} Deprecated: ${escapeBackslashes(param.deprecated)}`);
}
}
if (member.returnType) {
out.push('');
out.push(`${indent}Returns`);
out.push(`${indent}-------`);
out.push(`${indent}${docType(member.returnType)}`);
}
out.push(`${indent}"""`);
}
/**
* @param {PyClass} clazz
* @param {PyMember} member
* @param {string[]} out
*/
function emitMember(clazz, member, out) {
const returnAnnotation = member.returnType ? annotation(member.returnType) : 'None';
const wrapper = returnWrapper(member.returnType);
if (member.kind === 'property') {
out.push(' @property');
out.push(` def ${member.name}(self) -> ${returnAnnotation}:`);
if (member.documented)
emitDocstring(clazz, member, out);
out.push(` return ${wrapper}(self._impl_obj.${member.name})`);
return;
}
const params = ['self', ...member.positional.map(p => `${p.name}: ${paramAnnotation(p)}`)];
if (member.keywordOnly.length)
params.push('*', ...member.keywordOnly.map(p => `${p.name}: ${paramAnnotation(p)}`));
out.push(` ${member.isAsync && isAsync ? 'async ' : ''}def ${member.name}(${params.join(', ')}) -> ${returnAnnotation}:`);
if (member.documented)
emitDocstring(clazz, member, out);
if (rules.tracebackHideClasses.includes(clazz.name))
out.push(' __tracebackhide__ = True');
// Dunder methods take positional arguments only.
const isDunder = member.name.startsWith('__');
const callArgs = [...member.positional, ...member.keywordOnly].map(p => isDunder ? forwardExpression(p) : `${p.name}=${forwardExpression(p)}`).join(', ');
const call = `self._impl_obj.${member.name}(${callArgs})`;
if (member.returnType?.kind === 'ecm') {
if (isAsync) {
out.push('');
out.push(` return AsyncEventContextManager(${call}.future)`);
} else {
out.push(` return EventContextManager(self, ${call}.future)`);
}
return;
}
out.push('');
let expression = call;
if (member.isAsync)
expression = isAsync ? `await ${call}` : `self._sync(${call})`;
if (member.returnType)
out.push(` return ${wrapper}(${expression})`);
else
out.push(` ${expression}`);
}
/**
* @param {PyClass} clazz
* @param {string[]} out
*/
function emitClass(clazz, out) {
out.push('');
out.push(`class ${clazz.name}(${clazz.base}):`);
// Typed, so that mypy checks every call below against the implementation.
out.push(` _impl_obj: ${clazz.name}Impl`);
out.push('');
/** @type {{ name: string, emit: () => void }[]} */
const items = clazz.members.map(member => ({
name: member.name,
emit: () => {
if (member.name === 'expect_event' || member.name === 'wait_for_event')
emitEventOverloads(clazz, member.name, out);
emitMember(clazz, member, out);
},
}));
if (clazz.events.length) {