gen_doc.py 3.8 KB
Newer Older
Y
Yang Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import argparse
import sys
import types

L
Luo Tao 已提交
20
import paddle.fluid as fluid
Y
Yang Yu 已提交
21 22 23 24 25 26 27 28 29 30 31


def parse_arg():
    parser = argparse.ArgumentParser()
    parser.add_argument('--submodules', nargs="*")
    parser.add_argument(
        'module', type=str, help='Generate the documentation of which module')
    return parser.parse_args()


class DocGenerator(object):
Y
yuyang18 已提交
32 33 34
    def __init__(self, module_name=None, stream=sys.stdout):
        if module_name == "":
            module_name = None
Y
Yang Yu 已提交
35
        self.stream = stream
Y
yuyang18 已提交
36 37
        if module_name is None:
            self.module_name = "fluid"
Y
Yang Yu 已提交
38
        else:
Y
yuyang18 已提交
39 40 41 42 43 44 45 46
            self.module_name = "fluid." + module_name
        if module_name is None:
            self.module = fluid
        else:
            if not hasattr(fluid, module_name):
                raise ValueError("Cannot find fluid.{0}".format(module_name))
            else:
                self.module = getattr(fluid, module_name)
Y
Yang Yu 已提交
47 48 49 50 51
        self.stream.write('''..  THIS FILE IS GENERATED BY `gen_doc.{py|sh}`
    !DO NOT EDIT THIS FILE MANUALLY!

''')

Y
yuyang18 已提交
52
        self._print_header_(self.module_name, dot='=', is_title=True)
Y
Yang Yu 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    def print_submodule(self, submodule_name):
        submodule = getattr(self.module, submodule_name)
        if submodule is None:
            raise ValueError("Cannot find submodule {0}".format(submodule_name))
        self.print_section(submodule_name)

        for item in submodule.__all__:
            self.print_item(item)

    def print_current_module(self):
        for item in self.module.__all__:
            self.print_item(item)

    def print_section(self, name):
        self._print_header_(name, dot='=', is_title=False)

    def print_item(self, name):
Y
yuyang18 已提交
71 72 73
        item = getattr(self.module, name, None)
        if item is None:
            return
Y
Yang Yu 已提交
74 75 76 77 78
        if isinstance(item, types.TypeType):
            self.print_class(name)
        elif isinstance(item, types.FunctionType):
            self.print_method(name)
        else:
Y
yuyang18 已提交
79
            pass
Y
Yang Yu 已提交
80 81

    def print_class(self, name):
Y
yuyang18 已提交
82
        self._print_ref_(name)
Y
Yang Yu 已提交
83
        self._print_header_(name, dot='-', is_title=False)
Y
yuyang18 已提交
84
        self.stream.write('''..  autoclass:: paddle.{0}.{1}
Y
Yang Yu 已提交
85 86 87 88 89 90
    :members:
    :noindex:

'''.format(self.module_name, name))

    def print_method(self, name):
Y
yuyang18 已提交
91
        self._print_ref_(name)
Y
Yang Yu 已提交
92
        self._print_header_(name, dot='-', is_title=False)
Y
yuyang18 已提交
93
        self.stream.write('''..  autofunction:: paddle.{0}.{1}
Y
Yang Yu 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    :noindex:

'''.format(self.module_name, name))

    def _print_header_(self, name, dot, is_title):
        dot_line = dot * len(name)
        if is_title:
            self.stream.write(dot_line)
            self.stream.write('\n')
        self.stream.write(name)
        self.stream.write('\n')
        self.stream.write(dot_line)
        self.stream.write('\n')
        self.stream.write('\n')

Y
yuyang18 已提交
109 110 111 112
    def _print_ref_(self, name):
        self.stream.write(".. _api_{0}_{1}:\n\n".format("_".join(
            self.module_name.split(".")), name))

Y
Yang Yu 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125

def main():
    args = parse_arg()
    gen = DocGenerator(args.module)
    if args.submodules is None:
        gen.print_current_module()
    else:
        for submodule_name in args.submodules:
            gen.print_submodule(submodule_name)


if __name__ == '__main__':
    main()