-
Notifications
You must be signed in to change notification settings - Fork 0
/
minidoc.py
724 lines (530 loc) · 18.8 KB
/
minidoc.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
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
"""Render module documentation to markdown
To use `minidoc`, follow these steps:
1. mark a section as documentation using HTML comments:
```markdown
# My Documentation
<!-- minidoc "module": "my_module" -->
<!-- minidoc -->
```
2. Run minidoc as in `python -m chmp.minidoc MY_DOC.md`
`minidoc` will replace the content between the comments with the documentation
of the module. It preserves the comments itself. Therefore, it is safe to run
`minidoc` repeatedly on the same document.
Per default minidoc will render a header for the module. To disable this
behavior add `"header": false` to the initial comment, as in:
```markdown
<!-- minidoc "module": "my_module", "header": false -->
```
To execute doc tests using pytest, load the plugin. E.g., via a `conftest.py`
file:
```python
pytest_plugins = "chmp.minidoc"
```
And include a test function with the `minidoc` marker:
```python
: import pytest
:
@pytest.mark.minidoc("chmp.minidoc")
def test_docs(test):
test()
```
The `test` function will execute a single code block in a test. To include setup
in tests, prefix lines with `: `. They are executed in tests, but not included
in the rendered documentation.
"""
#
# Copyright: Christopher Prohm, 2022
# Copied from https://github.com/chmp/libchmp/blob/main/src/chmp/minidoc.py
# License: MIT License
#
import collections
import dataclasses
import enum
import importlib
import inspect
import json
import pathlib
import re
import typing
from inspect import Parameter
from typing import Any, Iterable, List, Optional, Tuple, Union, cast
try:
from typing import get_overloads
except ImportError:
def get_overloads(f):
return []
__all__ = ["update_docs", "update_docs_lines", "update_docs_str"]
def update_docs(path: Union[str, pathlib.Path]):
"""Update the documentation section inside a file
The file is updated in-place.
Usage:
```python ignore
update_docs("Readme.md")
```
"""
with open(path, "rt") as fobj:
lines = list(fobj)
lines = update_docs_lines(lines)
with open(path, "wt") as fobj:
for line in lines:
fobj.write(f"{line.rstrip()}\n")
def update_docs_lines(lines: List[str]) -> List[str]:
"""Inject the documentation in a list of lines
Usage:
```python
: from chmp.minidoc import update_docs_lines
:
lines = ["# My documentation", "..."]
lines = update_docs_lines(lines)
assert isinstance(lines, list)
```
"""
return list(_inject_docs(lines))
def update_docs_str(lines: str) -> str:
"""Inject the documentation in a string
Usage:
```python
: from chmp.minidoc import update_docs_str
:
doc = update_docs_str('''# My documentation
...
''')
assert isinstance(doc, str)
```
"""
return "\n".join(_inject_docs(lines.splitlines()))
_minidoc_start = re.compile(r"^<!--\s+minidoc\s+(?P<desc>.*)-->$")
_minidoc_end = re.compile(r"^<!--\s+minidoc\s+-->$")
def parse_line(line):
if _minidoc_end.match(line) is not None:
return MinidocEnd()
m = _minidoc_start.match(line)
if m is not None:
desc = json.loads("{" + m.group("desc") + "}")
if "module" in desc:
object = desc.pop("module")
object_type = ObjectType.MODULE
elif "class" in desc:
object = desc.pop("class")
object_type = ObjectType.CLASS
elif "function" in desc:
object = desc.pop("function")
object_type = ObjectType.FUNCTION
else:
raise ValueError()
return MinidocStart(type=object_type, object=object, **desc)
if line.startswith("###### "):
return Header(depth=6)
if line.startswith("##### "):
return Header(depth=5)
if line.startswith("#### "):
return Header(depth=4)
if line.startswith("### "):
return Header(depth=3)
if line.startswith("## "):
return Header(depth=2)
if line.startswith("# "):
return Header(depth=1)
if line.startswith("```"):
return BlockCode()
return None
@dataclasses.dataclass
class MinidocStart:
type: "ObjectType"
object: str
header: bool = True
rename: Optional[str] = None
class ObjectType(str, enum.Enum):
MODULE = "module"
CLASS = "class"
FUNCTION = "function"
@dataclasses.dataclass
class Header:
depth: int
@dataclasses.dataclass
class BlockCode:
pass
@dataclasses.dataclass
class MinidocEnd:
pass
def _inject_docs(lines: Iterable[str]) -> Iterable[str]:
current = None
header_depth = 0
in_code = False
for idx, line in enumerate(lines):
line = line.rstrip()
ty = parse_line(line)
if isinstance(ty, BlockCode):
if current is None:
yield line
in_code = not in_code
elif in_code:
if current is None:
yield line
elif isinstance(ty, MinidocStart):
if current is not None:
raise ValueError(
"minidoc: detected documentation start while inside a"
f" documentation block (line: {idx + 1})"
)
else:
yield line
current = ty
elif isinstance(ty, MinidocEnd):
if current is not None:
yield from render_docs(
current.type,
current.object,
header_depth,
include_header=current.header,
rename=current.rename,
)
yield line
current = None
else:
raise ValueError(
"minidoc: detected documentation end without start (line"
f" {idx + 1})"
)
elif isinstance(ty, Header) and current is None:
header_depth = ty.depth
yield line
elif current is None:
yield line
def render_docs(
type: ObjectType,
object_name: str,
header_depth: int,
*,
include_header: bool = True,
rename: Optional[str] = None,
) -> Iterable[str]:
if type is ObjectType.MODULE:
module = importlib.import_module(object_name)
render_name = object_name if rename is None else rename
yield from render_module(
render_name,
module,
header_depth=header_depth + 1,
include_header=include_header,
)
elif type in {ObjectType.CLASS, ObjectType.FUNCTION}:
assert rename is None
module_name, _, item_name = object_name.rpartition(".")
module = importlib.import_module(module_name)
item = getattr(module, item_name)
yield from render_item(
module_name,
module,
item_name,
item,
header_depth=header_depth + 1,
include_header=include_header,
)
else:
raise RuntimeError(f"Unknown object type ({type})")
def render_module(
module_name: str, module: Any, *, header_depth: int, include_header: bool = True
):
if include_header:
yield _render_header(f"`{module_name}`", header_depth)
yield ""
yield from process_doc(get_doc(module))
yield ""
for name, item in get_module_contents(module):
yield from render_item(
module_name,
module,
name,
item,
header_depth=header_depth + (1 if include_header else 0),
)
def render_item(
module_name: str,
module: Any,
item_name: str,
item: Any,
header_depth: int,
*,
include_header: bool = True,
) -> Iterable[str]:
module_doc_name = "__" + str(item_name).replace(".", "_") + "_doc__"
doc = getattr(module, module_doc_name, get_doc(item))
overloads = get_overloads(item) if inspect.isfunction(item) else []
if not overloads:
overloads = [item]
if include_header:
header = f"`{module_name}.{item_name}`"
yield _render_header(header, header_depth)
yield ""
yield f"[{module_name}.{item_name}]: #{header_to_link(header)}"
yield ""
for overload in overloads:
yield f"`{module_name}.{item_name}{format_signature(overload)}`"
yield ""
yield from process_doc(doc)
yield ""
yield from render_members(module_name, module, item_name, item, header_depth)
def process_doc(doc: str) -> Iterable[str]:
"""Strip out any ignored lines from code examples"""
in_code_block = False
filter_lines = False
for line in doc.splitlines():
assert not filter_lines or (filter_lines and in_code_block)
if line.startswith("```python") and "ignore" not in line:
assert not in_code_block
in_code_block = True
filter_lines = True
yield line
elif line.startswith("```"):
in_code_block = not in_code_block
filter_lines = False
yield line
elif filter_lines and line.startswith("::"):
yield line[1:]
elif filter_lines and line.startswith(":"):
continue
else:
yield line
def render_members(
module_name: str, module: Any, item_name: str, item: Any, header_depth: int
) -> Iterable[str]:
for member_name, member in getattr(item, "__dict__", {}).items():
is_documented = (
not member_name.startswith("_") and has_own_doc(member)
) or hasattr(module, f"__{item_name}_{member_name}_doc__")
if not is_documented:
continue
yield from render_item(
module_name,
module,
f"{item_name}.{member_name}",
member,
header_depth=header_depth + 1,
)
def _render_header(header: Any, depth: int) -> str:
return "#" * min(6, depth) + " " + str(header)
def get_module_contents(module: Any) -> Iterable[Tuple[str, Any]]:
if hasattr(module, "__all__"):
for name in module.__all__:
yield name, getattr(module, name)
else:
for key, item in module.__dict__.items():
if key.startswith("_"):
continue
if hasattr(item, "__module__") and item.__module__ != module.__name__:
continue
if callable(item) or hasattr(module, f"__{key}_doc__"):
yield key, item
def header_to_link(header: str) -> str:
for c in "`.()=*,<>[]:'\"":
header = header.replace(c, "")
header = header.replace(" ", "-")
header = header.lower()
return header
def splice_docs(readme: List[str], docs: List[str]) -> Iterable[str]:
in_reference = False
for line in readme:
is_reference_start = line.startswith("## Reference")
is_h2_header = line.startswith("## ")
if not in_reference and not is_reference_start:
yield line
elif not in_reference and is_reference_start:
yield line
yield from docs
in_reference = True
elif in_reference and not is_h2_header:
# ignore the line
pass
elif in_reference and is_h2_header:
yield line
in_reference = False
def has_own_doc(obj: Any) -> bool:
doc = getattr(obj, "__doc__", None)
class_doc = getattr(type(obj), "__doc__", None)
# filters out objects like ints, which have a doc attribute due to their class
return doc is not None and doc != class_doc
def get_doc(obj: Any) -> str:
doc = inspect.getdoc(obj)
return str(doc) if doc is not None else ""
def format_signature(func: Any) -> str:
"""Format the signature of a callable"""
if inspect.isclass(func):
func = func.__init__
skip_first_arg = True
else:
skip_first_arg = False
if not inspect.isfunction(func):
return ""
sig = inspect.signature(func)
parts = []
parameters: List[Union[Parameter, str]] = list(sig.parameters.values())
if skip_first_arg:
parameters = parameters[1:]
# insert pseud-parameters to handle pos-only and kw-only parameters
idx = 0
while idx < len(parameters):
current = cast(Parameter, parameters[idx])
assert isinstance(current, Parameter)
if idx == 0 and current.kind is Parameter.KEYWORD_ONLY:
parameters.insert(0, "*")
idx += 2
continue
prev = cast(Parameter, parameters[idx - 1])
assert isinstance(prev, Parameter)
if (
idx != 0
and current.kind is Parameter.KEYWORD_ONLY
and prev.kind not in {Parameter.KEYWORD_ONLY, Parameter.VAR_POSITIONAL}
):
parameters.insert(idx, "*")
idx += 2
elif (
idx != 0
and current.kind is not Parameter.POSITIONAL_ONLY
and prev.kind is Parameter.POSITIONAL_ONLY
):
parameters.insert(idx, "/")
idx += 2
else:
idx += 1
for param in parameters:
if isinstance(param, str):
parts.append(param)
elif param.kind in {
Parameter.POSITIONAL_ONLY,
Parameter.POSITIONAL_OR_KEYWORD,
Parameter.KEYWORD_ONLY,
}:
if param.default is Parameter.empty and param.annotation is Parameter.empty:
parts.append(f"{param.name}")
elif (
param.default is not Parameter.empty
and param.annotation is Parameter.empty
):
parts.append(f"{param.name}={param.default!r}")
elif (
param.default is Parameter.empty
and param.annotation is not Parameter.empty
):
parts.append(f"{param.name}: {format_annotation(param.annotation)}")
elif (
param.default is not Parameter.empty
and param.annotation is not Parameter.empty
):
parts.append(
f"{param.name}: {format_annotation(param.annotation)} "
f"= {param.default!r}"
)
elif param.kind in {Parameter.VAR_POSITIONAL}:
parts.append(f"*{param.name}")
elif param.kind in {Parameter.VAR_KEYWORD}:
parts.append(f"**{param.name}")
args = "(" + ", ".join(parts) + ")"
if sig.return_annotation is Parameter.empty:
return args
else:
return f"{args} -> {format_annotation(sig.return_annotation)}"
def format_annotation(obj: Any) -> str:
origin = typing.get_origin(obj)
args = typing.get_args(obj)
# special formatting for typing modules
if origin is typing.Union and len(args) == 2 and args[1] is type(None):
return f"Optional[{format_annotation(args[0])}]"
elif origin in _known_typing_origins:
origin_name = _known_typing_origins[origin]
formatted_args = ", ".join(format_annotation(arg) for arg in args)
return f"{origin_name}[{formatted_args}]"
elif isinstance(obj, typing.TypeVar):
return str(obj)
elif isinstance(obj, collections.abc.Hashable) and obj in _known_objects:
return _known_objects[obj]
elif isinstance(obj, list):
return "[{}]".format(", ".join(format_annotation(item) for item in obj))
elif (
hasattr(obj, "__module__")
and hasattr(obj, "__name__")
and obj.__module__ != "builtins"
):
return f"{obj.__module__}.{obj.__name__}"
elif hasattr(obj, "__name__"):
return str(obj.__name__)
else:
return str(obj)
_known_typing_origins = {
collections.abc.Callable: "Callable",
collections.abc.Iterable: "Iterable",
collections.abc.Mapping: "Mapping",
collections.abc.Sequence: "Sequence",
typing.Union: "Union",
tuple: "Tuple",
list: "List",
dict: "Dict",
}
_known_objects = {
typing.Any: "Any",
callable: "callable",
}
# pytest plugin
def pytest_configure(config):
config.addinivalue_line(
"markers", "minidoc(object): mark a test function running minicoc tests."
)
def pytest_generate_tests(metafunc):
if (mark := metafunc.definition.keywords.get("minidoc")) is not None:
if "test" not in metafunc.fixturenames:
raise RuntimeError("minidoc tests must take `test` as an argument")
if len(mark.args) != 1 or len(mark.kwargs):
raise RuntimeError(
"The minidoc mark must be called with a single positional argument"
)
module_name = mark.args[0]
module = importlib.import_module(module_name)
tests = [(module_name, test) for test in get_doc_tests(module)]
for child_name, child in get_module_contents(module):
tests.extend(
(f"{module_name}.{child_name}", block) for block in get_doc_tests(child)
)
metafunc.parametrize(
"test",
[build_doc_test_function(code) for _, code in tests],
ids=[test_id for test_id, _ in tests],
)
def get_doc_tests(obj):
doc = inspect.getdoc(obj)
if doc is None:
return
current = None
in_code_block = False
for line in doc.splitlines():
if line.startswith("```python") and "ignore" not in line:
assert not in_code_block
current = []
in_code_block = True
elif line.startswith("```"):
if in_code_block:
if current:
yield "\n".join(current)
in_code_block = False
current = None
else:
assert current is None
in_code_block = None
elif in_code_block:
assert current is not None
if line.startswith(":"):
line = line[2:]
current.append(line)
if current:
raise RuntimeError("Unclosed code block")
def build_doc_test_function(code):
def test():
exec(code, {}, {})
return test
if __name__ == "__main__":
import argparse
_parser = argparse.ArgumentParser()
_parser.add_argument("files", type=pathlib.Path, nargs="+")
_args = _parser.parse_args()
for path in _args.files:
update_docs(path)