|
| 1 | +# Copyright (c) 2018 PaddlePaddle 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 | +Print all signature of a python module in alphabet order. |
| 16 | +
|
| 17 | +Usage: |
| 18 | + ./print_signature "paddle.fluid" > signature.txt |
| 19 | +""" |
| 20 | +import importlib |
| 21 | +import inspect |
| 22 | +import collections |
| 23 | +import sys |
| 24 | +import pydoc |
| 25 | + |
| 26 | +member_dict = collections.OrderedDict() |
| 27 | + |
| 28 | + |
| 29 | +def visit_member(parent_name, member): |
| 30 | + cur_name = ".".join([parent_name, member.__name__]) |
| 31 | + if inspect.isclass(member): |
| 32 | + for name, value in inspect.getmembers(member): |
| 33 | + if hasattr(value, '__name__') and (not name.startswith("_") or |
| 34 | + name == "__init__"): |
| 35 | + visit_member(cur_name, value) |
| 36 | + elif callable(member): |
| 37 | + try: |
| 38 | + member_dict[cur_name] = inspect.getargspec(member) |
| 39 | + except TypeError: # special for PyBind method |
| 40 | + member_dict[cur_name] = " ".join([ |
| 41 | + line.strip() for line in pydoc.render_doc(member).split('\n') |
| 42 | + if "->" in line |
| 43 | + ]) |
| 44 | + |
| 45 | + else: |
| 46 | + raise RuntimeError("Unsupported generate signature of member, type {0}". |
| 47 | + format(str(type(member)))) |
| 48 | + |
| 49 | + |
| 50 | +def visit_all_module(mod): |
| 51 | + for member_name in ( |
| 52 | + name |
| 53 | + for name in (mod.__all__ if hasattr(mod, "__all__") else dir(mod)) |
| 54 | + if not name.startswith("_")): |
| 55 | + instance = getattr(mod, member_name, None) |
| 56 | + if instance is None: |
| 57 | + continue |
| 58 | + if inspect.ismodule(instance): |
| 59 | + visit_all_module(instance) |
| 60 | + else: |
| 61 | + visit_member(mod.__name__, instance) |
| 62 | + |
| 63 | + |
| 64 | +visit_all_module(importlib.import_module(sys.argv[1])) |
| 65 | + |
| 66 | +for name in member_dict: |
| 67 | + print name, member_dict[name] |
0 commit comments