Skip to content

Commit 03b59e4

Browse files
committed
make it possible to override the default filter list
1 parent d3877f7 commit 03b59e4

File tree

1 file changed

+30
-80
lines changed

1 file changed

+30
-80
lines changed

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 30 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -210,75 +210,6 @@ def add_dict_to_dict(add_from, add_to):
210210
add_to[key] = add_from[key]
211211

212212

213-
def extract(
214-
py_modules,
215-
base_dir,
216-
private_map: Dict[str, Any],
217-
visitor_cls: Type[
218-
doc_generator_visitor.DocGeneratorVisitor] = doc_generator_visitor
219-
.DocGeneratorVisitor,
220-
callbacks: Optional[public_api.ApiFilter] = None,
221-
include_default_callbacks=True):
222-
"""Walks the module contents, returns an index of all visited objects.
223-
224-
The return value is an instance of `self._visitor_cls`, usually:
225-
`doc_generator_visitor.DocGeneratorVisitor`
226-
227-
Args:
228-
py_modules: A list containing a single (short_name, module_object) pair.
229-
like `[('tf',tf)]`.
230-
base_dir: The package root directory. Nothing defined outside of this
231-
directory is documented.
232-
private_map: A {'path':["name"]} dictionary listing particular object
233-
locations that should be ignored in the doc generator.
234-
visitor_cls: A class, typically a subclass of
235-
`doc_generator_visitor.DocGeneratorVisitor` that acumulates the indexes of
236-
objects to document.
237-
callbacks: Additional callbacks passed to `traverse`. Executed between the
238-
`PublicApiFilter` and the accumulator (`DocGeneratorVisitor`). The
239-
primary use case for these is to filter the list of children (see:
240-
`public_api.local_definitions_filter`)
241-
include_default_callbacks: When true the long list of standard
242-
visitor-callbacks are included. When false, only the `callbacks` argument
243-
is used.
244-
245-
Returns:
246-
The accumulator (`DocGeneratorVisitor`)
247-
"""
248-
if callbacks is None:
249-
callbacks = []
250-
251-
if len(py_modules) != 1:
252-
raise ValueError("only pass one [('name',module)] pair in py_modules")
253-
short_name, py_module = py_modules[0]
254-
255-
# The objects found during traversal, and their children are passed to each
256-
# of these filters in sequence. Each visitor returns the list of children
257-
# to be passed to the next visitor.
258-
if include_default_callbacks:
259-
filters = [
260-
# filter the api.
261-
public_api.FailIfNestedTooDeep(10),
262-
public_api.filter_module_all,
263-
public_api.add_proto_fields,
264-
public_api.filter_builtin_modules,
265-
public_api.filter_private_symbols,
266-
public_api.FilterBaseDirs(base_dir),
267-
public_api.FilterPrivateMap(private_map),
268-
public_api.filter_doc_controls_skip,
269-
public_api.ignore_typing
270-
]
271-
else:
272-
filters = []
273-
274-
accumulator = visitor_cls()
275-
traverse.traverse(
276-
py_module, filters + callbacks, accumulator, root_name=short_name)
277-
278-
accumulator.build()
279-
return accumulator
280-
281-
282213
EXCLUDED = set(['__init__.py', 'OWNERS', 'README.txt'])
283214

284215

@@ -413,23 +344,42 @@ def make_parser_config(self,
413344
self_link_base=self._self_link_base,
414345
)
415346

347+
def make_default_filters(self):
348+
# The objects found during traversal, and their children are passed to each
349+
# of these filters in sequence. Each visitor returns the list of children
350+
# to be passed to the next visitor.
351+
return [
352+
# filter the api.
353+
public_api.FailIfNestedTooDeep(10),
354+
public_api.filter_module_all,
355+
public_api.add_proto_fields,
356+
public_api.filter_builtin_modules,
357+
public_api.filter_private_symbols,
358+
public_api.FilterBaseDirs(self._base_dir),
359+
public_api.FilterPrivateMap(self._private_map),
360+
public_api.filter_doc_controls_skip,
361+
public_api.ignore_typing
362+
]
363+
416364
def run_extraction(self):
417365
"""Walks the module contents, returns an index of all visited objects.
418366
419-
The return value is an instance of `self._visitor_cls`, usually:
420-
`doc_generator_visitor.DocGeneratorVisitor`
421-
422367
Returns:
368+
An instance of `parser_config.ParserConfig`.
423369
"""
424-
visitor = extract(
425-
py_modules=self._py_modules,
426-
base_dir=self._base_dir,
427-
private_map=self._private_map,
428-
visitor_cls=self._visitor_cls,
429-
callbacks=self._callbacks)
370+
if len(self._py_modules) != 1:
371+
raise ValueError("only pass one [('name',module)] pair in py_modules")
372+
short_name, py_module = self._py_modules[0]
373+
374+
filters = self.make_default_filters()
375+
376+
accumulator = self._visitor_cls()
377+
traverse.traverse(
378+
py_module, filters + self._callbacks, accumulator, root_name=short_name)
379+
380+
accumulator.build()
430381

431-
# Write the api docs.
432-
parser_config = self.make_parser_config(visitor)
382+
parser_config = self.make_parser_config(accumulator)
433383
return parser_config
434384

435385
def build(self, output_dir):

0 commit comments

Comments
 (0)