|
16 | 16 |
|
17 | 17 | import collections
|
18 | 18 | import dataclasses
|
19 |
| -import functools |
| 19 | +import enum |
20 | 20 | import inspect
|
21 | 21 |
|
22 | 22 | from typing import Any, Dict, List, Optional, NamedTuple, Tuple
|
@@ -182,6 +182,7 @@ def __init__(self):
|
182 | 182 | self._duplicate_of: Dict[str, str] = None
|
183 | 183 |
|
184 | 184 | self.path_tree = PathTree()
|
| 185 | + self.api_tree = None |
185 | 186 |
|
186 | 187 | @property
|
187 | 188 | def index(self):
|
@@ -390,6 +391,8 @@ def build(self):
|
390 | 391 | if self._reverse_index is not None:
|
391 | 392 | return
|
392 | 393 |
|
| 394 | + self.api_tree = ApiTree.from_path_tree(self.path_tree, self._score_name) |
| 395 | + |
393 | 396 | # Maps the id of a symbol to its fully qualified name. For symbols that have
|
394 | 397 | # several aliases, this map contains the first one found.
|
395 | 398 | # We use id(py_object) to get a hashable value for py_object. Note all
|
@@ -445,12 +448,32 @@ def build(self):
|
445 | 448 |
|
446 | 449 | @dataclasses.dataclass(repr=False)
|
447 | 450 | class ApiTreeNode(PathTreeNode):
|
| 451 | + """A node in the ApiTree.""" |
448 | 452 | aliases: List[ApiPath] = dataclasses.field(default_factory=list)
|
449 | 453 |
|
450 | 454 | @property
|
451 | 455 | def obj_type(self) -> obj_type_lib.ObjType:
|
452 | 456 | return obj_type_lib.ObjType.get(self.py_object)
|
453 | 457 |
|
| 458 | + class OutputType(enum.Enum): |
| 459 | + PAGE = 'page' |
| 460 | + FRAGMENT = 'fragment' |
| 461 | + |
| 462 | + def output_type(self) -> OutputType: |
| 463 | + obj_type = obj_type_lib.ObjType.get(self.py_object) |
| 464 | + |
| 465 | + if obj_type in (obj_type_lib.ObjType.CLASS, obj_type_lib.ObjType.MODULE): |
| 466 | + return self.OutputType.PAGE |
| 467 | + elif obj_type in (obj_type_lib.ObjType.CALLABLE, |
| 468 | + obj_type_lib.ObjType.TYPE_ALIAS): |
| 469 | + parent_type = obj_type_lib.ObjType.get(self.parent.py_object) |
| 470 | + if parent_type is obj_type_lib.ObjType.CLASS: |
| 471 | + return self.OutputType.FRAGMENT |
| 472 | + else: |
| 473 | + return self.OutputType.PAGE |
| 474 | + else: |
| 475 | + return self.OutputType.FRAGMENT |
| 476 | + |
454 | 477 |
|
455 | 478 | class ApiTree(Dict[ApiPath, ApiTreeNode]):
|
456 | 479 | """Public API index.
|
|
0 commit comments