Returns a description of where the passed in python object was defined. Args: py_object: The Python object. parser_config: A ParserConfig object. Returns: Either a `_PythonBuiltin`, `_PythonFile`, or a `_GeneratedFile`
(py_object, parser_config)
| 1656 | |
| 1657 | |
| 1658 | def _get_defined_in(py_object, parser_config): |
| 1659 | """Returns a description of where the passed in python object was defined. |
| 1660 | |
| 1661 | Args: |
| 1662 | py_object: The Python object. |
| 1663 | parser_config: A ParserConfig object. |
| 1664 | |
| 1665 | Returns: |
| 1666 | Either a `_PythonBuiltin`, `_PythonFile`, or a `_GeneratedFile` |
| 1667 | """ |
| 1668 | # Every page gets a note about where this object is defined |
| 1669 | # TODO(wicke): If py_object is decorated, get the decorated object instead. |
| 1670 | # TODO(wicke): Only use decorators that support this in TF. |
| 1671 | |
| 1672 | try: |
| 1673 | path = os.path.relpath(path=tf_inspect.getfile(py_object), |
| 1674 | start=parser_config.base_dir) |
| 1675 | except TypeError: # getfile throws TypeError if py_object is a builtin. |
| 1676 | return _PythonBuiltin() |
| 1677 | |
| 1678 | # TODO(wicke): If this is a generated file, link to the source instead. |
| 1679 | # TODO(wicke): Move all generated files to a generated/ directory. |
| 1680 | # TODO(wicke): And make their source file predictable from the file name. |
| 1681 | |
| 1682 | # In case this is compiled, point to the original |
| 1683 | if path.endswith('.pyc'): |
| 1684 | path = path[:-1] |
| 1685 | |
| 1686 | # Never include links outside this code base. |
| 1687 | if path.startswith('..') or re.search(r'\b_api\b', path): |
| 1688 | return None |
| 1689 | |
| 1690 | if re.match(r'.*/gen_[^/]*\.py$', path): |
| 1691 | return _GeneratedFile(path, parser_config) |
| 1692 | if 'genfiles' in path or 'tools/api/generator' in path: |
| 1693 | return _GeneratedFile(path, parser_config) |
| 1694 | elif re.match(r'.*_pb2\.py$', path): |
| 1695 | # The _pb2.py files all appear right next to their defining .proto file. |
| 1696 | return _ProtoFile(path[:-7] + '.proto', parser_config) |
| 1697 | else: |
| 1698 | return _PythonFile(path, parser_config) |
| 1699 | |
| 1700 | |
| 1701 | # TODO(markdaoust): This should just parse, pretty_docs should generate the md. |
no test coverage detected