Skip to content

Commit 00106b0

Browse files
committed
Documentation comments added everywhere.
1 parent 01d3db5 commit 00106b0

File tree

8 files changed

+4595
-12
lines changed

8 files changed

+4595
-12
lines changed

nodejs-moera-api/nodejsmoeraapi.py

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,51 @@
66
from typing import Any, TextIO
77

88
import yaml
9-
from camel_converter import to_pascal
109

1110

1211
def ind(n: int) -> str:
1312
return n * 4 * ' '
1413

1514

15+
def doc_wrap(s: str, indent: int) -> str:
16+
s = s.strip()
17+
if '\n' in s:
18+
return f'\n{ind(indent)}'.join(doc_wrap(c, indent) for c in s.split('\n'))
19+
20+
max_length = 120 - indent * 4 - 3
21+
result = ''
22+
while True:
23+
if len(s) < max_length:
24+
result += ' * ' + s
25+
break
26+
pos = 0
27+
while True:
28+
next = s.find(' ', pos + 1)
29+
if next < 0 or next >= max_length:
30+
break
31+
pos = next
32+
result += ' * ' + s[:pos] + '\n' + ind(indent)
33+
s = s[pos + 1:]
34+
return result
35+
36+
37+
def html_to_doc(s: str) -> str:
38+
s = (s.replace('<code>', '``')
39+
.replace('</code>', '``')
40+
.replace('<br>', '\\\n')
41+
.replace('<i>', '*')
42+
.replace('</i>', '*')
43+
.replace('<b>', '**')
44+
.replace('</b>', '**')
45+
.replace('<ul>', '\n')
46+
.replace('<li>', '* ')
47+
.replace('</li>', '')
48+
.replace('</ul>', ''))
49+
s = re.sub(r'<a [^>]*>', '', s)
50+
s = s.replace('</a>', '')
51+
return s
52+
53+
1654
def read_api(ifname: str) -> Any:
1755
with open(ifname, 'r') as ifile:
1856
return yaml.safe_load(ifile)
@@ -114,7 +152,9 @@ def generate_operations(operations: Any, tfile: TextIO, sfile: TextIO) -> None:
114152
tfile.write('\n')
115153
tfile.write(f'export interface {operations["name"]} {{\n')
116154
for field in operations['fields']:
117-
tfile.write(f' {field["name"]}?: PrincipalValue | null;\n')
155+
if 'description' in field:
156+
tfile.write(f'{ind(1)}/**\n{ind(1)} * {field["description"]}\n{ind(1)} */\n')
157+
tfile.write(f'{ind(1)}{field["name"]}?: PrincipalValue | null;\n')
118158
tfile.write('}\n')
119159

120160
sfile.write('\n')
@@ -276,6 +316,10 @@ def generate_class(self, tfile: TextIO, structs: dict[str, Structure]) -> None:
276316
t = to_js_type(field['type'])
277317
if field.get('array', False):
278318
t += '[]'
319+
if 'description' in field:
320+
tfile.write(f'{ind(1)}/**\n')
321+
tfile.write(ind(1) + doc_wrap(html_to_doc(field["description"]), 1))
322+
tfile.write(f'\n{ind(1)} */\n')
279323
tfile.write(tmpl % (field['name'], t))
280324
tfile.write('}\n')
281325
if self.generic:
@@ -406,7 +450,8 @@ def generate_calls(api: Any, structs: dict[str, Structure], afile: TextIO) -> No
406450
url_params: dict[str, str] = {}
407451
flag_name: str | None = None
408452
flag_js_name: str | None = None
409-
flags: list[str] = []
453+
flags: list[Any] = []
454+
param_docs = []
410455
for param in request.get('params', []) + request.get('query', []):
411456
if 'name' not in param:
412457
print('Missing name of parameter of the request "{method} {url}"'
@@ -422,23 +467,28 @@ def generate_calls(api: Any, structs: dict[str, Structure], afile: TextIO) -> No
422467
if 'flags' in param:
423468
flag_name = name
424469
flag_js_name = js_name
425-
flags = [flag['name'] for flag in param['flags']]
470+
flags = [flag for flag in param['flags']]
426471
for flag in flags:
427-
params.append('with{name}: boolean = false'.format(name=flag.capitalize()))
472+
flag_param = f'with{flag["name"].capitalize()}'
473+
params.append(f'{flag_param}: boolean = false')
474+
param_docs += [(flag_param, 'include ' + flag.get('description', ''), 'boolean')]
428475
else:
429476
if param.get('optional', False):
430477
tail_params.append(f'{js_name}: {js_type} | null = null')
478+
param_docs += [(js_name, param.get('description', ''), f'{js_type} | null')]
431479
else:
432480
params.append(f'{js_name}: {js_type}')
481+
param_docs += [(js_name, param.get('description', ''), js_type)]
433482
body = ''
434483
if 'in' in request:
435484
if 'type' in request['in']:
436485
if request['in']['type'] != 'blob':
437486
print('Unrecognised type "{type}" of the input body of the request "{method} {url}"'
438487
.format(type=request['in']['type'], method=request['type'], url=request['url']))
439488
exit(1)
440-
body = ', body'
441-
params.append('body: Buffer')
489+
body = ', body, contentType'
490+
params += ['body: Buffer', 'contentType: string']
491+
param_docs += [('body', '', 'Buffer'), ('contentType', 'content-type of ``body``', 'string')]
442492
else:
443493
if 'name' not in request['in']:
444494
print('Missing name of body of the request "{method} {url}"'
@@ -450,6 +500,7 @@ def generate_calls(api: Any, structs: dict[str, Structure], afile: TextIO) -> No
450500
js_type += '[]'
451501
body = f', body: {name}'
452502
params.append(f'{name}: {js_type}')
503+
param_docs += [(name, request['in'].get('description', ''), js_type)]
453504
params += tail_params
454505

455506
method = request['type']
@@ -494,10 +545,24 @@ def generate_calls(api: Any, structs: dict[str, Structure], afile: TextIO) -> No
494545
result += '[]'
495546
result_schema += 'Array'
496547

548+
description = ''
549+
if 'description' in request:
550+
description = doc_wrap(html_to_doc(request["description"]), 1)
551+
description += f'\n{ind(1)} *'
552+
if len(param_docs) > 0:
553+
for pd in param_docs:
554+
param_doc = ' - ' + html_to_doc(pd[1]) if pd[1] != '' else ''
555+
doc = doc_wrap(f'@param {{{pd[2]}}} {pd[0]}{param_doc}', 1)
556+
description += f'\n{ind(1)}{doc}'
557+
description += f'\n{ind(1)}' + doc_wrap(f'@return {{Promise<{result}>}}', 1)
558+
if description != '':
559+
description = f'\n{ind(1)}/**\n{ind(1)}{description}\n{ind(1)} */'
560+
561+
afile.write(description)
497562
name = request['function']
498563
afile.write(params_wrap(f'\n{ind(1)}async {name}(%s): Promise<{result}> {{\n', ', '.join(params), 2))
499564
if flag_name is not None:
500-
items = ', '.join('"%s": with%s' % (flag, flag.capitalize()) for flag in flags)
565+
items = ', '.join('"%s": with%s' % (flag['name'], flag['name'].capitalize()) for flag in flags)
501566
afile.write(f'{ind(2)}const {flag_js_name} = commaSeparatedFlags({{{items}}});\n')
502567
afile.write(f'{ind(2)}const location = {location};\n')
503568
query_params = ''
@@ -533,8 +598,14 @@ def generate_calls(api: Any, structs: dict[str, Structure], afile: TextIO) -> No
533598
import * as API from "./types";
534599
import { commaSeparatedFlags, ut } from "../util";
535600
601+
/**
602+
* Node API interface.
603+
*/
536604
export class MoeraNode extends Caller {
537605
606+
/**
607+
* @param {string | null} nodeUrl - the node URL
608+
*/
538609
constructor(nodeUrl: string | null = null) {
539610
super();
540611
if (nodeUrl != null) {

0 commit comments

Comments
 (0)