|
33 | 33 | "from fastcore.script import *\n",
|
34 | 34 | "from fastcore.utils import *\n",
|
35 | 35 | "from fastcore.meta import delegates\n",
|
| 36 | + "from fastcore.net import urlread\n", |
36 | 37 | "\n",
|
37 |
| - "import ast,contextlib\n", |
| 38 | + "import ast,builtins,contextlib\n", |
38 | 39 | "import pkg_resources,importlib\n",
|
39 |
| - "from astunparse import unparse\n", |
40 | 40 | "\n",
|
| 41 | + "from astunparse import unparse\n", |
| 42 | + "from io import BytesIO\n", |
| 43 | + "from collections import defaultdict\n", |
41 | 44 | "from pprint import pformat\n",
|
42 | 45 | "from urllib.parse import urljoin\n",
|
43 | 46 | "from functools import lru_cache\n",
|
|
355 | 358 | "outputs": [],
|
356 | 359 | "source": [
|
357 | 360 | "#|export\n",
|
358 |
| - "\n", |
359 | 361 | "@call_parse\n",
|
360 | 362 | "@delegates(nbglob_cli)\n",
|
361 | 363 | "def nbdev_export(\n",
|
|
383 | 385 | "N.B.: the `black_format` processor is passed in by default. But it is a no-op, unless `black_formatting=True` is set in your `settings.ini` configuration. You can omit it from `nbdev_export` on the command line by passing in `--procs`."
|
384 | 386 | ]
|
385 | 387 | },
|
| 388 | + { |
| 389 | + "cell_type": "markdown", |
| 390 | + "metadata": {}, |
| 391 | + "source": [ |
| 392 | + "## Construct Index" |
| 393 | + ] |
| 394 | + }, |
| 395 | + { |
| 396 | + "cell_type": "code", |
| 397 | + "execution_count": null, |
| 398 | + "metadata": {}, |
| 399 | + "outputs": [], |
| 400 | + "source": [ |
| 401 | + "#|export\n", |
| 402 | + "typs = 'module','class','method','function'\n", |
| 403 | + "bset = set(dir(builtins))" |
| 404 | + ] |
| 405 | + }, |
| 406 | + { |
| 407 | + "cell_type": "code", |
| 408 | + "execution_count": null, |
| 409 | + "metadata": {}, |
| 410 | + "outputs": [], |
| 411 | + "source": [ |
| 412 | + "#|export\n", |
| 413 | + "def create_index(url, pre=None):\n", |
| 414 | + " \"Create a documentation index from a sphinx inventory file at `url`, with optional prefix `pre`\"\n", |
| 415 | + " try: from sphinx.util.inventory import InventoryFile\n", |
| 416 | + " except ImportError: raise ImportError('`sphinx` is a dependency for building indexes. Run `pip install sphinx` to use `create_index`.')\n", |
| 417 | + " pre = ifnone(pre, f\"{url}/\")\n", |
| 418 | + " invs = urlread(f'{url}/objects.inv', decode=False)\n", |
| 419 | + " idx = InventoryFile.load(stream=BytesIO(invs), uri=pre, joinfunc=urljoin)\n", |
| 420 | + " _get = lambda o: {k:v[2] for k,v in idx[f'py:{o}'].items() if k[0]!='_'}\n", |
| 421 | + " d = {o:_get(o) for o in typs}\n", |
| 422 | + " syms = defaultdict(dict)\n", |
| 423 | + " for o in typs:\n", |
| 424 | + " for k,v in d[o].items():\n", |
| 425 | + " if k.split('.')[0] in bset: k = 'builtins.' + k\n", |
| 426 | + " modparts = k.split(\".\")[:-2 if o=='method' else -1]\n", |
| 427 | + " if modparts: syms['.'.join(modparts)][k] = v\n", |
| 428 | + " return syms" |
| 429 | + ] |
| 430 | + }, |
| 431 | + { |
| 432 | + "cell_type": "code", |
| 433 | + "execution_count": null, |
| 434 | + "metadata": {}, |
| 435 | + "outputs": [], |
| 436 | + "source": [ |
| 437 | + "url = 'https://docs.python.org/3'\n", |
| 438 | + "syms = create_index(url)" |
| 439 | + ] |
| 440 | + }, |
| 441 | + { |
| 442 | + "cell_type": "code", |
| 443 | + "execution_count": null, |
| 444 | + "metadata": {}, |
| 445 | + "outputs": [ |
| 446 | + { |
| 447 | + "data": { |
| 448 | + "text/plain": [ |
| 449 | + "{'builtins.bool': 'https://docs.python.org/3/library/functions.html#bool',\n", |
| 450 | + " 'builtins.bytearray': 'https://docs.python.org/3/library/stdtypes.html#bytearray',\n", |
| 451 | + " 'builtins.bytes': 'https://docs.python.org/3/library/stdtypes.html#bytes',\n", |
| 452 | + " 'builtins.complex': 'https://docs.python.org/3/library/functions.html#complex',\n", |
| 453 | + " 'builtins.dict': 'https://docs.python.org/3/library/stdtypes.html#dict',\n", |
| 454 | + " 'builtins.float': 'https://docs.python.org/3/library/functions.html#float',\n", |
| 455 | + " 'builtins.frozenset': 'https://docs.python.org/3/library/stdtypes.html#frozenset',\n", |
| 456 | + " 'builtins.int': 'https://docs.python.org/3/library/functions.html#int',\n", |
| 457 | + " 'builtins.list': 'https://docs.python.org/3/library/stdtypes.html#list',\n", |
| 458 | + " 'builtins.memoryview': 'https://docs.python.org/3/library/stdtypes.html#memoryview'}" |
| 459 | + ] |
| 460 | + }, |
| 461 | + "execution_count": null, |
| 462 | + "metadata": {}, |
| 463 | + "output_type": "execute_result" |
| 464 | + } |
| 465 | + ], |
| 466 | + "source": [ |
| 467 | + "dict(list(syms['builtins'].items())[:10])" |
| 468 | + ] |
| 469 | + }, |
| 470 | + { |
| 471 | + "cell_type": "code", |
| 472 | + "execution_count": null, |
| 473 | + "metadata": {}, |
| 474 | + "outputs": [], |
| 475 | + "source": [ |
| 476 | + "for b in syms['builtins']:\n", |
| 477 | + " b = b.split('.')\n", |
| 478 | + " if len(b) != 2: continue\n", |
| 479 | + " b = b[1]\n", |
| 480 | + " assert b in bset" |
| 481 | + ] |
| 482 | + }, |
386 | 483 | {
|
387 | 484 | "cell_type": "markdown",
|
388 | 485 | "metadata": {},
|
|
721 | 818 | "text/markdown": [
|
722 | 819 | "---\n",
|
723 | 820 | "\n",
|
724 |
| - "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L241){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 821 | + "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L266){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
725 | 822 | "\n",
|
726 | 823 | "### NbdevLookup.doc\n",
|
727 | 824 | "\n",
|
|
732 | 829 | "text/plain": [
|
733 | 830 | "---\n",
|
734 | 831 | "\n",
|
735 |
| - "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L241){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 832 | + "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L266){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
736 | 833 | "\n",
|
737 | 834 | "### NbdevLookup.doc\n",
|
738 | 835 | "\n",
|
|
823 | 920 | "text/markdown": [
|
824 | 921 | "---\n",
|
825 | 922 | "\n",
|
826 |
| - "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L246){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 923 | + "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L271){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
827 | 924 | "\n",
|
828 | 925 | "### NbdevLookup.code\n",
|
829 | 926 | "\n",
|
|
834 | 931 | "text/plain": [
|
835 | 932 | "---\n",
|
836 | 933 | "\n",
|
837 |
| - "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L246){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 934 | + "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L271){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
838 | 935 | "\n",
|
839 | 936 | "### NbdevLookup.code\n",
|
840 | 937 | "\n",
|
|
882 | 979 | "text/markdown": [
|
883 | 980 | "---\n",
|
884 | 981 | "\n",
|
885 |
| - "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L264){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 982 | + "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L289){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
886 | 983 | "\n",
|
887 | 984 | "### NbdevLookup.linkify\n",
|
888 | 985 | "\n",
|
|
891 | 988 | "text/plain": [
|
892 | 989 | "---\n",
|
893 | 990 | "\n",
|
894 |
| - "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L264){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 991 | + "[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L289){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
895 | 992 | "\n",
|
896 | 993 | "### NbdevLookup.linkify\n",
|
897 | 994 | "\n",
|
|
962 | 1059 | "assert NbdevLookup().linkify(md) == md"
|
963 | 1060 | ]
|
964 | 1061 | },
|
| 1062 | + { |
| 1063 | + "cell_type": "code", |
| 1064 | + "execution_count": null, |
| 1065 | + "metadata": {}, |
| 1066 | + "outputs": [ |
| 1067 | + { |
| 1068 | + "data": { |
| 1069 | + "text/plain": [ |
| 1070 | + "'[`builtins.str.split`](https://docs.python.org/3/library/stdtypes.html#str.split)'" |
| 1071 | + ] |
| 1072 | + }, |
| 1073 | + "execution_count": null, |
| 1074 | + "metadata": {}, |
| 1075 | + "output_type": "execute_result" |
| 1076 | + } |
| 1077 | + ], |
| 1078 | + "source": [ |
| 1079 | + "# Test builtins\n", |
| 1080 | + "md = \"`builtins.str.split`\"\n", |
| 1081 | + "NbdevLookup().linkify(md)" |
| 1082 | + ] |
| 1083 | + }, |
| 1084 | + { |
| 1085 | + "cell_type": "code", |
| 1086 | + "execution_count": null, |
| 1087 | + "metadata": {}, |
| 1088 | + "outputs": [ |
| 1089 | + { |
| 1090 | + "data": { |
| 1091 | + "text/plain": [ |
| 1092 | + "'[`str.split`](https://docs.python.org/3/library/stdtypes.html#str.split) and [`str`](https://docs.python.org/3/library/locale.html#locale.str)'" |
| 1093 | + ] |
| 1094 | + }, |
| 1095 | + "execution_count": null, |
| 1096 | + "metadata": {}, |
| 1097 | + "output_type": "execute_result" |
| 1098 | + } |
| 1099 | + ], |
| 1100 | + "source": [ |
| 1101 | + "# ... now with stripping\n", |
| 1102 | + "md = \"`str.split` and `str`\"\n", |
| 1103 | + "NbdevLookup('nbdev_stdlib').linkify(md)" |
| 1104 | + ] |
| 1105 | + }, |
965 | 1106 | {
|
966 | 1107 | "cell_type": "markdown",
|
967 | 1108 | "metadata": {},
|
|
0 commit comments