Skip to content

Commit acb9d0d

Browse files
MarkDaoustcopybara-github
authored andcommitted
Split parser.py: move ParserConfig out.
PiperOrigin-RevId: 416428155
1 parent 741a39f commit acb9d0d

File tree

6 files changed

+99
-76
lines changed

6 files changed

+99
-76
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Lint as: python3
2+
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ==============================================================================
16+
"""The `ParserConfig` contains the information extracted by walking the API."""
17+
18+
class ParserConfig(object):
19+
"""Stores all indexes required to parse the docs."""
20+
21+
def __init__(self, reference_resolver, duplicates, duplicate_of, tree, index,
22+
reverse_index, base_dir, code_url_prefix):
23+
"""Object with the common config for docs_for_object() calls.
24+
25+
Args:
26+
reference_resolver: An instance of ReferenceResolver.
27+
duplicates: A `dict` mapping fully qualified names to a set of all aliases
28+
of this name. This is used to automatically generate a list of all
29+
aliases for each name.
30+
duplicate_of: A map from duplicate names to preferred names of API
31+
symbols.
32+
tree: A `dict` mapping a fully qualified name to the names of all its
33+
members. Used to populate the members section of a class or module page.
34+
index: A `dict` mapping full names to objects.
35+
reverse_index: A `dict` mapping object ids to full names.
36+
base_dir: A base path that is stripped from file locations written to the
37+
docs.
38+
code_url_prefix: A Url to pre-pend to the links to file locations.
39+
"""
40+
self.reference_resolver = reference_resolver
41+
self.duplicates = duplicates
42+
self.duplicate_of = duplicate_of
43+
self.tree = tree
44+
self.reverse_index = reverse_index
45+
self.index = index
46+
self.base_dir = base_dir
47+
self.code_url_prefix = code_url_prefix
48+
49+
def py_name_to_object(self, full_name):
50+
"""Return the Python object for a Python symbol name."""
51+
return self.index[full_name]
52+
53+

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, Union
2727

28+
from tensorflow_docs.api_generator import config
2829
from tensorflow_docs.api_generator import doc_controls
2930
from tensorflow_docs.api_generator import doc_generator_visitor
3031
from tensorflow_docs.api_generator import parser
@@ -457,7 +458,7 @@ def _get_headers(page_info: parser.PageInfo, search_hints: bool) -> List[str]:
457458
def write_docs(
458459
*,
459460
output_dir: Union[str, pathlib.Path],
460-
parser_config: parser.ParserConfig,
461+
parser_config: config.ParserConfig,
461462
yaml_toc: bool,
462463
root_module_name: str,
463464
root_title: str = 'TensorFlow',
@@ -478,7 +479,7 @@ def write_docs(
478479
Args:
479480
output_dir: Directory to write documentation markdown files to. Will be
480481
created if it doesn't exist.
481-
parser_config: A `parser.ParserConfig` object, containing all the necessary
482+
parser_config: A `config.ParserConfig` object, containing all the necessary
482483
indices.
483484
yaml_toc: Set to `True` to generate a "_toc.yaml" file.
484485
root_module_name: (str) the name of the root module (`tf` for tensorflow).
@@ -809,7 +810,7 @@ def make_reference_resolver(self, visitor):
809810
visitor, py_module_names=[self._short_name])
810811

811812
def make_parser_config(self, visitor, reference_resolver):
812-
return parser.ParserConfig(
813+
return config.ParserConfig(
813814
reference_resolver=reference_resolver,
814815
duplicates=visitor.duplicates,
815816
duplicate_of=visitor.duplicate_of,

tools/tensorflow_docs/api_generator/generate_lib_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from absl import flags
2525
from absl.testing import absltest
2626

27+
from tensorflow_docs.api_generator import config
2728
from tensorflow_docs.api_generator import doc_controls
2829
from tensorflow_docs.api_generator import generate_lib
2930
from tensorflow_docs.api_generator import parser
@@ -117,7 +118,7 @@ def get_test_objects(self):
117118
reference_resolver = parser.ReferenceResolver.from_visitor(
118119
visitor=visitor, py_module_names=['tf'], link_prefix='api_docs/python')
119120

120-
parser_config = parser.ParserConfig(
121+
parser_config = config.ParserConfig(
121122
reference_resolver=reference_resolver,
122123
duplicates=duplicates,
123124
duplicate_of=duplicate_of,

tools/tensorflow_docs/api_generator/parser.py

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import astor
3737

38+
from tensorflow_docs.api_generator import config
3839
from tensorflow_docs.api_generator import doc_controls
3940
from tensorflow_docs.api_generator import doc_generator_visitor
4041
from tensorflow_docs.api_generator import public_api
@@ -71,42 +72,6 @@ def get_obj_type(py_obj: Any) -> ObjType:
7172
return ObjType.OTHER
7273

7374

74-
class ParserConfig(object):
75-
"""Stores all indexes required to parse the docs."""
76-
77-
def __init__(self, reference_resolver, duplicates, duplicate_of, tree, index,
78-
reverse_index, base_dir, code_url_prefix):
79-
"""Object with the common config for docs_for_object() calls.
80-
81-
Args:
82-
reference_resolver: An instance of ReferenceResolver.
83-
duplicates: A `dict` mapping fully qualified names to a set of all aliases
84-
of this name. This is used to automatically generate a list of all
85-
aliases for each name.
86-
duplicate_of: A map from duplicate names to preferred names of API
87-
symbols.
88-
tree: A `dict` mapping a fully qualified name to the names of all its
89-
members. Used to populate the members section of a class or module page.
90-
index: A `dict` mapping full names to objects.
91-
reverse_index: A `dict` mapping object ids to full names.
92-
base_dir: A base path that is stripped from file locations written to the
93-
docs.
94-
code_url_prefix: A Url to pre-pend to the links to file locations.
95-
"""
96-
self.reference_resolver = reference_resolver
97-
self.duplicates = duplicates
98-
self.duplicate_of = duplicate_of
99-
self.tree = tree
100-
self.reverse_index = reverse_index
101-
self.index = index
102-
self.base_dir = base_dir
103-
self.code_url_prefix = code_url_prefix
104-
105-
def py_name_to_object(self, full_name):
106-
"""Return the Python object for a Python symbol name."""
107-
return self.index[full_name]
108-
109-
11075
@dataclasses.dataclass
11176
class _FileLocation(object):
11277
"""This class indicates that the object is defined in a regular file.
@@ -918,7 +883,7 @@ class _DocstringInfo(typing.NamedTuple):
918883

919884
def _get_other_member_doc(
920885
obj: Any,
921-
parser_config: ParserConfig,
886+
parser_config: config.ParserConfig,
922887
extra_docs: Optional[Dict[int, str]],
923888
) -> str:
924889
"""Returns the docs for other members of a module."""
@@ -981,7 +946,7 @@ def _get_other_member_doc(
981946
def _parse_md_docstring(
982947
py_object: Any,
983948
full_name: str,
984-
parser_config: ParserConfig,
949+
parser_config: config.ParserConfig,
985950
extra_docs: Optional[Dict[int, str]] = None,
986951
) -> _DocstringInfo:
987952
"""Parse the object's docstring and return a `_DocstringInfo`.
@@ -994,7 +959,7 @@ def _parse_md_docstring(
994959
or module).
995960
full_name: (optional) The api path to the current object, so replacements
996961
can depend on context.
997-
parser_config: An instance of `ParserConfig`.
962+
parser_config: An instance of `config.ParserConfig`.
998963
extra_docs: Extra docs for symbols like public constants(list, tuple, etc)
999964
that need to be added to the markdown pages created.
1000965
@@ -1156,7 +1121,7 @@ class FormatArguments(object):
11561121
def __init__(
11571122
self,
11581123
type_annotations: Dict[str, str],
1159-
parser_config: ParserConfig,
1124+
parser_config: config.ParserConfig,
11601125
func_full_name: str,
11611126
) -> None:
11621127
self._type_annotations = type_annotations
@@ -1403,7 +1368,7 @@ class FuncType(enum.Enum):
14031368

14041369
def generate_signature(
14051370
func: Any,
1406-
parser_config: ParserConfig,
1371+
parser_config: config.ParserConfig,
14071372
func_full_name: str,
14081373
func_type: FuncType,
14091374
) -> _SignatureComponents:
@@ -1417,7 +1382,7 @@ def generate_signature(
14171382
14181383
Args:
14191384
func: A function, method, or functools.partial to extract the signature for.
1420-
parser_config: `ParserConfig` for the method/function whose signature is
1385+
parser_config: `config.ParserConfig` for the method/function whose signature is
14211386
generated.
14221387
func_full_name: The full name of a function whose signature is generated.
14231388
func_type: Type of the current `func`. This is required because there isn't
@@ -1740,7 +1705,7 @@ def collect_docs(self, parser_config):
17401705
Mainly this is details about the function signature.
17411706
17421707
Args:
1743-
parser_config: The ParserConfig for the module being documented.
1708+
parser_config: The config.ParserConfig for the module being documented.
17441709
"""
17451710

17461711
assert self.signature is None
@@ -1855,7 +1820,7 @@ def collect_docs(self, parser_config) -> None:
18551820
```
18561821
18571822
Args:
1858-
parser_config: The ParserConfig for the module being documented.
1823+
parser_config: The config.ParserConfig for the module being documented.
18591824
"""
18601825
assert self.signature is None
18611826

@@ -1958,7 +1923,7 @@ def _set_bases(self, parser_config):
19581923
doc pages for the class' parents.
19591924
19601925
Args:
1961-
parser_config: An instance of `ParserConfig`.
1926+
parser_config: An instance of `config.ParserConfig`.
19621927
"""
19631928
bases = []
19641929
for base in self.py_object.__mro__[1:]:
@@ -2016,13 +1981,13 @@ def _add_method(
20161981
self,
20171982
member_info: MemberInfo,
20181983
defining_class: Optional[type], # pylint: disable=g-bare-generic
2019-
parser_config: ParserConfig) -> None:
1984+
parser_config: config.ParserConfig) -> None:
20201985
"""Adds a `MethodInfo` entry to the `methods` list.
20211986
20221987
Args:
20231988
member_info: a `MemberInfo` describing the method.
20241989
defining_class: The `type` object where this method is defined.
2025-
parser_config: A `ParserConfig`.
1990+
parser_config: A `config.ParserConfig`.
20261991
"""
20271992
if defining_class is None:
20281993
return
@@ -2119,7 +2084,7 @@ def _add_member(
21192084
self,
21202085
member_info: MemberInfo,
21212086
defining_class: Optional[type], # pylint: disable=g-bare-generic
2122-
parser_config: ParserConfig,
2087+
parser_config: config.ParserConfig,
21232088
) -> None:
21242089
"""Adds a member to the class page."""
21252090
obj_type = get_obj_type(member_info.py_object)
@@ -2147,7 +2112,7 @@ def collect_docs(self, parser_config):
21472112
Mainly, this is details about the class's members.
21482113
21492114
Args:
2150-
parser_config: An instance of ParserConfig.
2115+
parser_config: An instance of config.ParserConfig.
21512116
"""
21522117
py_class = self.py_object
21532118

@@ -2359,7 +2324,7 @@ def collect_docs(self, parser_config):
23592324
Mainly this is information about the members of the module.
23602325
23612326
Args:
2362-
parser_config: An instance of ParserConfig.
2327+
parser_config: An instance of config.ParserConfig.
23632328
"""
23642329

23652330
member_names = parser_config.tree.get(self.full_name, [])
@@ -2392,7 +2357,7 @@ def collect_docs(self, parser_config):
23922357
def docs_for_object(
23932358
full_name: str,
23942359
py_object: Any,
2395-
parser_config: ParserConfig,
2360+
parser_config: config.ParserConfig,
23962361
extra_docs: Optional[Dict[int, str]] = None,
23972362
) -> PageInfo:
23982363
"""Return a PageInfo object describing a given object from the TF API.
@@ -2412,7 +2377,7 @@ def docs_for_object(
24122377
full_name: The fully qualified name of the symbol to be documented.
24132378
py_object: The Python object to be documented. Its documentation is sourced
24142379
from `py_object`'s docstring.
2415-
parser_config: A ParserConfig object.
2380+
parser_config: A config.ParserConfig object.
24162381
extra_docs: Extra docs for symbols like public constants(list, tuple, etc)
24172382
that need to be added to the markdown pages created.
24182383
@@ -2477,13 +2442,14 @@ def _unwrap_obj(obj):
24772442
return obj
24782443

24792444

2480-
def _get_defined_in(py_object: Any,
2481-
parser_config: ParserConfig) -> Optional[_FileLocation]:
2445+
def _get_defined_in(
2446+
py_object: Any,
2447+
parser_config: config.ParserConfig) -> Optional[_FileLocation]:
24822448
"""Returns a description of where the passed in python object was defined.
24832449
24842450
Args:
24852451
py_object: The Python object.
2486-
parser_config: A ParserConfig object.
2452+
parser_config: A config.ParserConfig object.
24872453
24882454
Returns:
24892455
A `_FileLocation`

0 commit comments

Comments
 (0)