Skip to content

Commit 5eac447

Browse files
MarkDaoustcopybara-github
authored andcommitted
Simplify in generate_lib with the ApiTree and Toc classes.
- config.py: Attach the api_tree to the parser_config. - doc_generator_visitor.py: Use the api_tree to generate the toc. - toc.py: move root's submodules from children to peers. PiperOrigin-RevId: 439439106
1 parent 02f808a commit 5eac447

File tree

6 files changed

+141
-411
lines changed

6 files changed

+141
-411
lines changed

tools/tensorflow_docs/api_generator/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
class ParserConfig(object):
1818
"""Stores all indexes required to parse the docs."""
1919

20-
def __init__(self, reference_resolver, duplicates, duplicate_of, tree, index,
21-
reverse_index, path_tree, base_dir, code_url_prefix):
20+
def __init__(self, *, reference_resolver, duplicates, duplicate_of, tree,
21+
index, reverse_index, path_tree, api_tree, base_dir,
22+
code_url_prefix):
2223
"""Object with the common config for docs_for_object() calls.
2324
2425
Args:
@@ -32,6 +33,8 @@ def __init__(self, reference_resolver, duplicates, duplicate_of, tree, index,
3233
members. Used to populate the members section of a class or module page.
3334
index: A `dict` mapping full names to objects.
3435
reverse_index: A `dict` mapping object ids to full names.
36+
path_tree: A PathTree datastructure to manage all the API paths.
37+
api_tree: A PathTree datastructure to manage all the API objects.
3538
base_dir: A base path that is stripped from file locations written to the
3639
docs.
3740
code_url_prefix: A Url to pre-pend to the links to file locations.
@@ -43,6 +46,7 @@ def __init__(self, reference_resolver, duplicates, duplicate_of, tree, index,
4346
self.reverse_index = reverse_index
4447
self.index = index
4548
self.path_tree = path_tree
49+
self.api_tree = api_tree
4650
self.base_dir = base_dir
4751
self.code_url_prefix = code_url_prefix
4852

tools/tensorflow_docs/api_generator/doc_generator_visitor.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import collections
1818
import dataclasses
19-
import functools
19+
import enum
2020
import inspect
2121

2222
from typing import Any, Dict, List, Optional, NamedTuple, Tuple
@@ -182,6 +182,7 @@ def __init__(self):
182182
self._duplicate_of: Dict[str, str] = None
183183

184184
self.path_tree = PathTree()
185+
self.api_tree = None
185186

186187
@property
187188
def index(self):
@@ -390,6 +391,8 @@ def build(self):
390391
if self._reverse_index is not None:
391392
return
392393

394+
self.api_tree = ApiTree.from_path_tree(self.path_tree, self._score_name)
395+
393396
# Maps the id of a symbol to its fully qualified name. For symbols that have
394397
# several aliases, this map contains the first one found.
395398
# We use id(py_object) to get a hashable value for py_object. Note all
@@ -445,12 +448,32 @@ def build(self):
445448

446449
@dataclasses.dataclass(repr=False)
447450
class ApiTreeNode(PathTreeNode):
451+
"""A node in the ApiTree."""
448452
aliases: List[ApiPath] = dataclasses.field(default_factory=list)
449453

450454
@property
451455
def obj_type(self) -> obj_type_lib.ObjType:
452456
return obj_type_lib.ObjType.get(self.py_object)
453457

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+
454477

455478
class ApiTree(Dict[ApiPath, ApiTreeNode]):
456479
"""Public API index.

0 commit comments

Comments
 (0)