Exemplo n.º 1
0
def generate_autosummary_docs(
        sources, output_dir=None, suffix='.rst', warn=_simple_warn,
        info=_simple_info, base_path=None, builder=None, template_dir=None,
        app=None):
    showed_sources = list(sorted(sources))
    if len(showed_sources) > 20:
        showed_sources = showed_sources[:10] + ['...'] + showed_sources[-10:]
    info('[autosummary] generating autosummary for: %s' %
         ', '.join(showed_sources))

    if output_dir:
        info('[autosummary] writing to %s' % output_dir)

    if base_path is not None:
        sources = [osp.join(base_path, filename) for filename in sources]

    # create our own templating environment
    template_dirs = [osp.join(package_dir, 'ext', 'autosummary', 'templates')]
    if builder is not None:
        # allow the user to override the templates
        template_loader = BuiltinTemplateLoader()
        template_loader.init(builder, dirs=template_dirs)
    else:
        if template_dir:
            template_dirs.insert(0, template_dir)
        template_loader = FileSystemLoader(template_dirs)
    template_env = SandboxedEnvironment(loader=template_loader)

    # read
    items = find_autosummary_in_files(sources)

    # remove possible duplicates
    items = list(dict([(item, True) for item in items]).keys())

    # keep track of new files
    new_files = []

    # write
    # noinspection PyTypeChecker
    for name, path, template_name in sorted(items, key=str):
        if path is None:
            # The corresponding autosummary:: directive did not have
            # a :toctree: option
            continue

        path = output_dir or osp.abspath(path)
        ensuredir(path)

        try:
            name, obj, parent, mod_name = import_by_name(name)
        except ImportError as e:
            warn('[autosummary] failed to import %r: %s' % (name, e))
            continue

        fn = osp.join(path, name + suffix)

        # skip it if it exists
        if osp.isfile(fn):
            continue

        new_files.append(fn)

        f = open(fn, 'w')

        try:
            try:
                doc = get_documenter(app, obj, parent)
            except TypeError:
                doc = get_documenter(obj, parent)

            if template_name is not None:
                template = template_env.get_template(template_name)
            else:
                try:
                    template = template_env.get_template('autosummary/%s.rst'
                                                         % doc.objtype)
                except TemplateNotFound:
                    template = template_env.get_template('autosummary/base.rst')

            ns = {}

            if doc.objtype == 'module':
                ns['members'] = dir(obj)
                ns['functions'], ns['all_functions'] = \
                    get_members(app, obj, 'function')
                ns['classes'], ns['all_classes'] = \
                    get_members(app, obj, 'class')
                ns['exceptions'], ns['all_exceptions'] = \
                    get_members(app, obj, 'exception')
                ns['data'], ns['all_data'] = \
                    get_members(app, obj, 'data', imported=True)

                ns['data'] = ', '.join(ns['data'])
                ns['all_data'] = ', '.join(ns['all_data'])

                ns['dispatchers'], ns['all_dispatchers'] = \
                    get_members(app, obj, 'dispatcher', imported=True)
            elif doc.objtype == 'class':
                ns['members'] = dir(obj)
                ns['methods'], ns['all_methods'] = \
                    get_members(app, obj, 'method', ['__init__'], True)
                ns['attributes'], ns['all_attributes'] = \
                    get_members(app, obj, 'attribute')

            parts = name.split('.')
            if doc.objtype in ('method', 'attribute'):
                mod_name = '.'.join(parts[:-2])
                cls_name = parts[-2]
                obj_name = '.'.join(parts[-2:])
                ns['class'] = cls_name
            else:
                mod_name, obj_name = '.'.join(parts[:-1]), parts[-1]

            ns['fullname'] = name
            ns['module'] = mod_name
            ns['objname'] = obj_name
            ns['name'] = parts[-1]

            ns['objtype'] = doc.objtype
            ns['underline'] = len(name) * '='

            rendered = template.render(**ns)
            f.write(rendered)
        finally:
            f.close()

    # descend recursively to new files
    if new_files:
        generate_autosummary_docs(new_files, output_dir=output_dir,
                                  suffix=suffix, warn=warn, info=info,
                                  base_path=base_path, builder=builder,
                                  template_dir=template_dir, app=app)
Exemplo n.º 2
0
def generate_autosummary_docs(sources,
                              app,
                              suffix='.rst',
                              output_dir=None,
                              base_path=None,
                              builder=None,
                              template_dir=None):
    showed_sources = list(sorted(sources))
    if len(showed_sources) > 20:
        showed_sources = showed_sources[:10] + ['...'] + showed_sources[-10:]
    logger.info('[autosummary] generating autosummary for: %s' %
                ', '.join(showed_sources))

    if output_dir:
        logger.info('[autosummary] writing to %s' % output_dir)

    if base_path is not None:
        sources = [os.path.join(base_path, filename) for filename in sources]

    # create our own templating environment
    template_dirs = [
        os.path.join(package_dir, 'ext', 'autosummary', 'templates')
    ]
    if builder is not None:
        # allow the user to override the templates
        template_loader = BuiltinTemplateLoader()
        template_loader.init(builder, dirs=template_dirs)
    else:
        if template_dir:
            template_dirs.insert(0, template_dir)
        template_loader = FileSystemLoader(template_dirs)
    template_env = SandboxedEnvironment(loader=template_loader)

    # read
    items = find_autosummary_in_files(sources)
    # keep track of new files
    new_files = []
    # write
    for name, path, template_name in sorted(set(items), key=str):
        if path is None:
            continue  # The corresponding autosummary:: directive did not have a :toctree: option

        path = output_dir or os.path.abspath(path)
        ensuredir(path)

        try:
            name, obj, parent, mod_name = import_by_name(name)
        except ImportError as e:
            logger.warning('[autosummary] failed to import %r: %s' % (name, e))
            continue

        fn = os.path.join(path, name + suffix)
        # skip it if it exists
        if os.path.isfile(fn):
            continue

        new_files.append(fn)
        with open(fn, 'w') as f:
            doc = get_documenter(app, obj, parent)

            if template_name is not None:
                template = template_env.get_template(template_name)
            else:
                try:
                    template = template_env.get_template('autosummary/%s.rst' %
                                                         doc.objtype)
                except TemplateNotFound:
                    template = template_env.get_template(
                        'autosummary/base.rst')

            ns = {}
            if doc.objtype == 'module':
                ns['members'] = dir(obj)
                ns['functions'], ns['all_functions'] = get_members(
                    app, obj, 'function')
                ns['classes'], ns['all_classes'] = get_members(
                    app, obj, 'class')
                ns['exceptions'], ns['all_exceptions'] = get_members(
                    app, obj, 'exception')
            elif doc.objtype == 'class':
                ns['members'] = dir(obj)
                include_public = app.config.autodoc_allowed_special_members
                ns['methods'], ns['all_methods'] = get_members(
                    app, obj, 'method', include_public)
                ns['attributes'], ns['all_attributes'] = get_members(
                    app, obj, 'attribute')

            parts = name.split('.')
            if doc.objtype in ('method', 'attribute'):
                mod_name = '.'.join(parts[:-2])
                cls_name = parts[-2]
                obj_name = '.'.join(parts[-2:])
                ns['class'] = cls_name
            else:
                mod_name, obj_name = '.'.join(parts[:-1]), parts[-1]

            ns['fullname'] = name
            ns['module'] = mod_name
            ns['objname'] = obj_name
            ns['name'] = parts[-1]
            ns['objtype'] = doc.objtype
            ns['underline'] = len(name) * '='

            rendered = template.render(**ns)
            f.write(rendered)

    # descend recursively to new files
    if new_files:
        generate_autosummary_docs(new_files,
                                  app,
                                  suffix=suffix,
                                  output_dir=output_dir,
                                  base_path=base_path,
                                  builder=builder,
                                  template_dir=template_dir)
Exemplo n.º 3
0
def generate_autosummary_docs(sources,
                              output_dir=None,
                              suffix='.rst',
                              warn=_simple_warn,
                              info=_simple_info,
                              base_path=None,
                              builder=None,
                              template_dir=None,
                              app=None):
    showed_sources = list(sorted(sources))
    if len(showed_sources) > 20:
        showed_sources = showed_sources[:10] + ['...'] + showed_sources[-10:]
    info('[autosummary] generating autosummary for: %s' %
         ', '.join(showed_sources))

    if output_dir:
        info('[autosummary] writing to %s' % output_dir)

    if base_path is not None:
        sources = [osp.join(base_path, filename) for filename in sources]

    # create our own templating environment
    template_dirs = [osp.join(package_dir, 'ext', 'autosummary', 'templates')]
    if builder is not None:
        # allow the user to override the templates
        template_loader = BuiltinTemplateLoader()
        template_loader.init(builder, dirs=template_dirs)
    else:
        if template_dir:
            template_dirs.insert(0, template_dir)
        template_loader = FileSystemLoader(template_dirs)
    template_env = SandboxedEnvironment(loader=template_loader)

    # read
    items = find_autosummary_in_files(sources)

    # remove possible duplicates
    items = list(dict([(item, True) for item in items]).keys())

    # keep track of new files
    new_files = []

    # write
    # noinspection PyTypeChecker
    for name, path, template_name in sorted(items, key=str):
        if path is None:
            # The corresponding autosummary:: directive did not have
            # a :toctree: option
            continue

        path = output_dir or osp.abspath(path)
        ensuredir(path)

        try:
            name, obj, parent, mod_name = import_by_name(name)
        except ImportError as e:
            warn('[autosummary] failed to import %r: %s' % (name, e))
            continue

        fn = osp.join(path, name + suffix)

        # skip it if it exists
        if osp.isfile(fn):
            continue

        new_files.append(fn)

        f = open(fn, 'w')

        try:
            try:
                doc = get_documenter(app, obj, parent)
            except TypeError:
                doc = get_documenter(obj, parent)

            if template_name is not None:
                template = template_env.get_template(template_name)
            else:
                try:
                    template = template_env.get_template('autosummary/%s.rst' %
                                                         doc.objtype)
                except TemplateNotFound:
                    template = template_env.get_template(
                        'autosummary/base.rst')

            ns = {}

            if doc.objtype == 'module':
                ns['members'] = dir(obj)
                ns['functions'], ns['all_functions'] = \
                    get_members(app, obj, 'function')
                ns['classes'], ns['all_classes'] = \
                    get_members(app, obj, 'class')
                ns['exceptions'], ns['all_exceptions'] = \
                    get_members(app, obj, 'exception')
                ns['data'], ns['all_data'] = \
                    get_members(app, obj, 'data', imported=True)

                ns['data'] = ', '.join(ns['data'])
                ns['all_data'] = ', '.join(ns['all_data'])

                ns['dispatchers'], ns['all_dispatchers'] = \
                    get_members(app, obj, 'dispatcher', imported=True)
            elif doc.objtype == 'class':
                ns['members'] = dir(obj)
                ns['methods'], ns['all_methods'] = \
                    get_members(app, obj, 'method', ['__init__'], True)
                ns['attributes'], ns['all_attributes'] = \
                    get_members(app, obj, 'attribute')

            parts = name.split('.')
            if doc.objtype in ('method', 'attribute'):
                mod_name = '.'.join(parts[:-2])
                cls_name = parts[-2]
                obj_name = '.'.join(parts[-2:])
                ns['class'] = cls_name
            else:
                mod_name, obj_name = '.'.join(parts[:-1]), parts[-1]

            ns['fullname'] = name
            ns['module'] = mod_name
            ns['objname'] = obj_name
            ns['name'] = parts[-1]

            ns['objtype'] = doc.objtype
            ns['underline'] = len(name) * '='

            rendered = template.render(**ns)
            f.write(rendered)
        finally:
            f.close()

    # descend recursively to new files
    if new_files:
        generate_autosummary_docs(new_files,
                                  output_dir=output_dir,
                                  suffix=suffix,
                                  warn=warn,
                                  info=info,
                                  base_path=base_path,
                                  builder=builder,
                                  template_dir=template_dir,
                                  app=app)
Exemplo n.º 4
0
    def generate_autosummary_docs(sources,
                                  output_dir=None,
                                  suffix='.rst',
                                  warn=_simple_warn,
                                  info=_simple_info,
                                  base_path=None,
                                  builder=None,
                                  template_dir=None):
        showed_sources = list(sorted(sources))
        if len(showed_sources) > 20:
            showed_sources = showed_sources[:10] + ['...'
                                                    ] + showed_sources[-10:]
        info('[autosummary] generating autosummary for: %s' %
             ', '.join(showed_sources))

        if output_dir:
            info('[autosummary] writing to %s' % output_dir)

        if base_path is not None:
            sources = [
                os.path.join(base_path, filename) for filename in sources
            ]

        # create our own templating environment
        template_dirs = [
            os.path.join(package_dir, 'ext', 'autosummary', 'templates')
        ]
        if builder is not None:
            # allow the user to override the templates
            template_loader = BuiltinTemplateLoader()
            template_loader.init(builder, dirs=template_dirs)
        else:
            if template_dir:
                template_dirs.insert(0, template_dir)
            template_loader = FileSystemLoader(template_dirs)
        template_env = SandboxedEnvironment(loader=template_loader)

        # read
        items = find_autosummary_in_files(sources)

        # remove possible duplicates
        items = dict([(item, True) for item in items]).keys()

        # keep track of new files
        new_files = []

        # write
        for name, path, template_name in sorted(items, key=str):
            if path is None:
                # The corresponding autosummary:: directive did not have
                # a :toctree: option
                continue

            path = output_dir or os.path.abspath(path)
            ensuredir(path)

            try:
                res = import_by_name(name)
                if len(res) == 3:
                    name, obj, parent = res
                else:
                    name, obj, parent, mod_name = res
            except ImportError, e:
                warn('[autosummary] failed to import %r: %s' % (name, e))
                continue

            fn = os.path.join(path, name + suffix)

            # skip it if it exists
            if os.path.isfile(fn):
                continue

            new_files.append(fn)

            f = open(fn, 'w')

            try:
                doc = get_documenter(obj, parent)

                if template_name is not None:
                    template = template_env.get_template(template_name)
                else:
                    try:
                        template = template_env.get_template(
                            'autosummary/%s.rst' % doc.objtype)
                    except TemplateNotFound:
                        template = template_env.get_template(
                            'autosummary/base.rst')

                def get_members(obj, typ, include_public=[]):
                    items = []
                    for name in dir(obj):
                        try:
                            documenter = get_documenter(
                                safe_getattr(obj, name), obj)
                        except AttributeError:
                            continue
                        if documenter.objtype == typ:
                            items.append(name)
                    public = [
                        x for x in items
                        if x in include_public or not x.startswith('_')
                    ]
                    return public, items

                ns = {}

                if doc.objtype == 'module':
                    ns['members'] = dir(obj)
                    ns['functions'], ns['all_functions'] = \
                                       get_members(obj, 'function')
                    ns['classes'], ns['all_classes'] = \
                                     get_members(obj, 'class')
                    ns['exceptions'], ns['all_exceptions'] = \
                                       get_members(obj, 'exception')
                    ##Added, following four lines
                    ns['data'], ns['all_data'] = \
                                       get_members(obj, 'data')
                    ns['attributes'], ns['all_attributes'] = \
                                     get_members(obj, 'attribute')
                elif doc.objtype == 'class':
                    ns['members'] = dir(obj)
                    ns['methods'], ns['all_methods'] = \
                                     get_members(obj, 'method', ['__init__'])
                    ns['attributes'], ns['all_attributes'] = \
                                     get_members(obj, 'attribute')
                    ##NEW
                    #Try to get stuff that's only in attributes
                    if hasattr(obj, '__module__'):
                        realmodule = obj.__module__
                    elif hasattr(parent, '__module__'):
                        realmodule = parent.__module__
                    else:
                        realmodule = None
                    if realmodule:
                        #Keyed by a tuple of (class name, attribute name)
                        #Result is a list of docstrings
                        docattrs = ModuleAnalyzer.for_module(
                            realmodule).find_attr_docs()
                        moreattrs = [
                            k[1] for k in docattrs.keys()
                            if k[1] not in ns['all_attributes']
                            and k[0] == obj.__name__
                        ]
                        ns['all_attributes'].extend(moreattrs)
                        ns['attributes'].extend(
                            [a for a in moreattrs if not a.startswith('_')])
                    ##END NEW

                parts = name.split('.')
                if doc.objtype in ('method', 'attribute'):
                    mod_name = '.'.join(parts[:-2])
                    cls_name = parts[-2]
                    obj_name = '.'.join(parts[-2:])
                    ns['class'] = cls_name
                else:
                    mod_name, obj_name = '.'.join(parts[:-1]), parts[-1]

                ns['fullname'] = name
                ns['module'] = mod_name
                ns['objname'] = obj_name
                ns['name'] = parts[-1]

                ns['objtype'] = doc.objtype
                ns['underline'] = len(name) * '='

                rendered = template.render(**ns)
                f.write(rendered)
            finally:
                f.close()