|
| 1 | +# Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""A `traverse` visitor for processing documentation.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import inspect |
| 22 | + |
| 23 | + |
| 24 | +class DocGeneratorVisitor(object): |
| 25 | + """A visitor that generates docs for a python object when __call__ed.""" |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + self._index = {} |
| 29 | + self._tree = {} |
| 30 | + |
| 31 | + @property |
| 32 | + def index(self): |
| 33 | + """A map from fully qualified names to objects to be documented. |
| 34 | +
|
| 35 | + The index is filled when the visitor is passed to `traverse`. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + The index filled by traversal. |
| 39 | + """ |
| 40 | + return self._index |
| 41 | + |
| 42 | + @property |
| 43 | + def tree(self): |
| 44 | + """A map from fully qualified names to all its child names for traversal. |
| 45 | +
|
| 46 | + The full name to member names map is filled when the visitor is passed to |
| 47 | + `traverse`. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + The full name to member name map filled by traversal. |
| 51 | + """ |
| 52 | + return self._tree |
| 53 | + |
| 54 | + def __call__(self, parent_name, parent, children): |
| 55 | + """Visitor interface, see `tensorflow/tools/common:traverse` for details. |
| 56 | +
|
| 57 | + This method is called for each symbol found in a traversal using |
| 58 | + `tensorflow/tools/common:traverse`. It should not be called directly in |
| 59 | + user code. |
| 60 | +
|
| 61 | + Args: |
| 62 | + parent_name: The fully qualified name of a symbol found during traversal. |
| 63 | + parent: The Python object referenced by `parent_name`. |
| 64 | + children: A list of `(name, py_object)` pairs enumerating, in alphabetical |
| 65 | + order, the children (as determined by `inspect.getmembers`) of `parent`. |
| 66 | + `name` is the local name of `py_object` in `parent`. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + RuntimeError: If this visitor is called with a `parent` that is not a |
| 70 | + class or module. |
| 71 | + """ |
| 72 | + self._index[parent_name] = parent |
| 73 | + self._tree[parent_name] = [] |
| 74 | + |
| 75 | + if inspect.ismodule(parent): |
| 76 | + print('module %s: %r' % (parent_name, parent)) |
| 77 | + elif inspect.isclass(parent): |
| 78 | + print('class %s: %r' % (parent_name, parent)) |
| 79 | + else: |
| 80 | + raise RuntimeError('Unexpected type in visitor -- %s: %r' % |
| 81 | + (parent_name, parent)) |
| 82 | + |
| 83 | + for name, child in children: |
| 84 | + full_name = '.'.join([parent_name, name]) if parent_name else name |
| 85 | + self._index[full_name] = child |
| 86 | + self._tree[parent_name].append(name) |
| 87 | + |
| 88 | + def find_duplicates(self): |
| 89 | + """Compute data structures containing information about duplicates. |
| 90 | +
|
| 91 | + Find duplicates in `index` and decide on one to be the "master" name. |
| 92 | +
|
| 93 | + Returns a map `duplicate_of` from aliases to their master name (the master |
| 94 | + name itself has no entry in this map), and a map `duplicates` from master |
| 95 | + names to a lexicographically sorted list of all aliases for that name (incl. |
| 96 | + the master name). |
| 97 | +
|
| 98 | + Returns: |
| 99 | + A tuple `(duplicate_of, duplicates)` as described above. |
| 100 | + """ |
| 101 | + # Maps the id of a symbol to its fully qualified name. For symbols that have |
| 102 | + # several aliases, this map contains the first one found. |
| 103 | + # We use id(py_object) to get a hashable value for py_object. Note all |
| 104 | + # objects in _index are in memory at the same time so this is safe. |
| 105 | + reverse_index = {} |
| 106 | + |
| 107 | + # Make a preliminary duplicates map. For all sets of duplicate names, it |
| 108 | + # maps the first name found to a list of all duplicate names. |
| 109 | + raw_duplicates = {} |
| 110 | + for full_name, py_object in self._index.iteritems(): |
| 111 | + # We cannot use the duplicate mechanism for constants, since e.g., |
| 112 | + # id(c1) == id(c2) with c1=1, c2=1. This is unproblematic since constants |
| 113 | + # have no usable docstring and won't be documented automatically. |
| 114 | + if (inspect.ismodule(py_object) or |
| 115 | + inspect.isclass(py_object) or |
| 116 | + inspect.isfunction(py_object) or |
| 117 | + inspect.isroutine(py_object) or |
| 118 | + inspect.ismethod(py_object) or |
| 119 | + isinstance(py_object, property)): |
| 120 | + object_id = id(py_object) |
| 121 | + if object_id in reverse_index: |
| 122 | + master_name = reverse_index[object_id] |
| 123 | + if master_name in raw_duplicates: |
| 124 | + raw_duplicates[master_name].append(full_name) |
| 125 | + else: |
| 126 | + raw_duplicates[master_name] = [master_name, full_name] |
| 127 | + else: |
| 128 | + reverse_index[object_id] = full_name |
| 129 | + |
| 130 | + # Decide on master names, rewire duplicates and make a duplicate_of map |
| 131 | + # mapping all non-master duplicates to the master name. The master symbol |
| 132 | + # does not have an entry in this map. |
| 133 | + duplicate_of = {} |
| 134 | + # Duplicates maps the main symbols to the set of all duplicates of that |
| 135 | + # symbol (incl. itself). |
| 136 | + duplicates = {} |
| 137 | + for names in raw_duplicates.values(): |
| 138 | + names = sorted(names) |
| 139 | + |
| 140 | + # Choose the lexicographically first name with the minimum number of |
| 141 | + # submodules. This will prefer highest level namespace for any symbol. |
| 142 | + master_name = min(names, key=lambda name: name.count('.')) |
| 143 | + |
| 144 | + duplicates[master_name] = names |
| 145 | + for name in names: |
| 146 | + if name != master_name: |
| 147 | + duplicate_of[name] = master_name |
| 148 | + |
| 149 | + return duplicate_of, duplicates |
0 commit comments