Beispiel #1
0
 def def_members(obj, typ, include_public=[]):
     items = []
     try:
         obj_dict = safe_getattr(obj, '__dict__')
     except AttributeError:
         return []
     defined = obj_dict.keys()
     defined.sort()
     for name in defined:
         if sys.skip_member(name, obj): continue
         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
Beispiel #2
0
            def get_members(obj, typ, include_public=[]):
                items = []
                for name in dir(obj):

                    if sys.skip_member(name, obj):
                        continue
                    if typ in ["class", "function"]:
                        c = getattr(obj, name)
                        if inspect.isclass(c) or inspect.isfunction(c):
                            if c.__module__ != obj.__name__ + ".base" and c.__module__ != obj.__name__:
                                continue
                    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
Beispiel #3
0
            def exclude_member(obj, name):
                if sys.skip_member(name, obj):
                    return True

                live = getattr(obj, name)

                if inspect.isbuiltin(live):
                    return True

                real_module = inspect.getmodule(live)
                if real_module is not None:
                    if real_module.__name__ in ["ctypes", "unittest"]:
                        return True

                c = getattr(obj, name)
                if inspect.isclass(c) or inspect.isfunction(c):
                    if (c.__module__ != obj.__name__ + ".base"
                            and c.__module__ != obj.__name__):
                        return True
                return False
Beispiel #4
0
            def exclude_member(obj, name):
                if sys.skip_member(name, obj): 
                    return True
                
                live = getattr(obj, name)

                if inspect.isbuiltin(live): 
                    return True

                real_module = inspect.getmodule(live)
                if real_module is not None:
                    if real_module.__name__ in ["ctypes", 
                                                "unittest"]: 
                        return True
                    
                c = getattr(obj, name)
                if inspect.isclass(c) or inspect.isfunction(c):
                    if (c.__module__!=obj.__name__+".base" and
                        c.__module__!=obj.__name__):
                        return True
                return False
Beispiel #5
0
 def def_members(obj, typ, include_public=[]):
     items = []
     try:
         obj_dict = safe_getattr(obj, '__dict__')
     except AttributeError:
         return []
     defined = obj_dict.keys()
     defined.sort()
     for name in defined:
         if sys.skip_member(name, obj): continue
         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
Beispiel #6
0
            def get_members(obj, typ, include_public=[]):
                items = []
                for name in dir(obj):

                    if sys.skip_member(name, obj): continue
                    if typ in ['class', 'function']:
                        c = getattr(obj, name)
                        if inspect.isclass(c) or inspect.isfunction(c):
                            if (c.__module__ != obj.__name__ + ".base"
                                    and c.__module__ != obj.__name__):
                                continue
                    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
Beispiel #7
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):
        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:
    	    name, obj, parent = import_by_name(name)
        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):

                    if sys.skip_member(name, obj): continue                    
                    if typ in ['class', 'function']:
                        c = getattr(obj, name)
                        if inspect.isclass(c) or inspect.isfunction(c):
                            if (c.__module__!=obj.__name__+".base" and
                                c.__module__!=obj.__name__):
                                    continue       
                    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


            def def_members(obj, typ, include_public=[]):
                items = []
                try:
                    obj_dict = safe_getattr(obj, '__dict__')
                except AttributeError:
                    return []
                defined = obj_dict.keys()
                defined.sort()
                for name in defined:
                    if sys.skip_member(name, obj): continue
                    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


            ns = {}

            if doc.objtype == 'module':
                ns['all_members'] = dir(obj)

                ns['classes'], ns['all_classes'] = \
                                 get_members(obj, 'class')
                ns['functions'], ns['all_functions'] = \
                                   get_members(obj, 'function')
                ns['exceptions'], ns['all_exceptions'] = \
                                   get_members(obj, 'exception')
                documented = ns['classes']+ns['functions']+ns['exceptions']
                
                if sys.all_submodules.has_key(obj.__name__):
                    ns['submodules'] = sys.all_submodules[obj.__name__]
                    documented += ns['submodules']

                ns['members'] = ns['all_members']
                try:
                    obj_dict = safe_getattr(obj, '__dict__')
                except AttributeError:
                    obj_dict = []

                public = [x for x in obj_dict if not x.startswith('_')]
                for item in documented:
                    if item in public:
                        public.remove(item)

                public.sort()
                ns['members'] = public
                ns['constants'] = [x for x in public
                                   if not sys.skip_member(x, obj)]
                
            elif doc.objtype == 'class':
                ns['members'] = dir(obj)
                ns['events'], ns['all_events'] = \
                                 get_members(obj, 'event')
                ns['methods'], ns['all_methods'] = \
                                 get_members(obj, 'method', ['__init__'])
                ns['attributes'], ns['all_attributes'] = \
                                 get_members(obj, 'attribute')

                ns['def_events'] = def_members(obj, 'event')
                ns['def_methods'] = def_members(obj, 'method', ['__init__'])
                ns['def_attributes'] = def_members(obj, 'attribute')

                ns['inherited'] = []
                for t in ['events', 'methods', 'attributes']:
                    key = 'inh_' + t
                    ns[key]=[]
                    for item in ns[t]:
                        if not item in ns['def_' + t]:
                            ns['inherited'].append(item)
                            ns[key].append(item)


            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()
Beispiel #8
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):
        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:
            name, obj, parent = import_by_name(name)
        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):

                    if sys.skip_member(name, obj): continue
                    if typ in ['class', 'function']:
                        c = getattr(obj, name)
                        if inspect.isclass(c) or inspect.isfunction(c):
                            if (c.__module__ != obj.__name__ + ".base"
                                    and c.__module__ != obj.__name__):
                                continue
                    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

            def def_members(obj, typ, include_public=[]):
                items = []
                try:
                    obj_dict = safe_getattr(obj, '__dict__')
                except AttributeError:
                    return []
                defined = obj_dict.keys()
                defined.sort()
                for name in defined:
                    if sys.skip_member(name, obj): continue
                    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

            ns = {}

            if doc.objtype == 'module':
                ns['all_members'] = dir(obj)

                ns['classes'], ns['all_classes'] = \
                                 get_members(obj, 'class')
                ns['functions'], ns['all_functions'] = \
                                   get_members(obj, 'function')
                ns['exceptions'], ns['all_exceptions'] = \
                                   get_members(obj, 'exception')
                if sys.all_submodules.has_key(obj.__name__):
                    ns['submodules'] = sys.all_submodules[obj.__name__]

                ns['members'] = ns['all_members']

                try:
                    obj_dict = safe_getattr(obj, '__dict__')
                except AttributeError:
                    obj_dict = []

                public = [x for x in obj_dict if not x.startswith('_')]
                for item in ns['classes'] + ns['functions'] + ns['exceptions']:
                    if item in public:
                        public.remove(item)

                public.sort()
                ns['members'] = public
                ns['constants'] = [
                    x for x in public if not sys.skip_member(x, obj)
                ]

            elif doc.objtype == 'class':
                ns['members'] = dir(obj)
                ns['events'], ns['all_events'] = \
                                 get_members(obj, 'event')
                ns['methods'], ns['all_methods'] = \
                                 get_members(obj, 'method', ['__init__'])
                ns['attributes'], ns['all_attributes'] = \
                                 get_members(obj, 'attribute')

                ns['def_events'] = def_members(obj, 'event')
                ns['def_methods'] = def_members(obj, 'method', ['__init__'])
                ns['def_attributes'] = def_members(obj, 'attribute')

                ns['inherited'] = []
                for t in ['events', 'methods', 'attributes']:
                    key = 'inh_' + t
                    ns[key] = []
                    for item in ns[t]:
                        if not item in ns['def_' + t]:
                            ns['inherited'].append(item)
                            ns[key].append(item)

            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()
Beispiel #9
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):
        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:
            name, obj, parent = import_by_name(name)
        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):

                    if sys.skip_member(name, obj):
                        continue
                    if typ in ["class", "function"]:
                        c = getattr(obj, name)
                        if inspect.isclass(c) or inspect.isfunction(c):
                            if c.__module__ != obj.__name__ + ".base" and c.__module__ != obj.__name__:
                                continue
                    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

            def def_members(obj, typ, include_public=[]):
                items = []
                try:
                    obj_dict = safe_getattr(obj, "__dict__")
                except AttributeError:
                    return []
                defined = obj_dict.keys()
                defined.sort()
                for name in defined:
                    if sys.skip_member(name, obj):
                        continue
                    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

            ns = {}

            if doc.objtype == "module":
                ns["all_members"] = dir(obj)

                ns["classes"], ns["all_classes"] = get_members(obj, "class")
                ns["functions"], ns["all_functions"] = get_members(obj, "function")
                ns["exceptions"], ns["all_exceptions"] = get_members(obj, "exception")
                if sys.all_submodules.has_key(obj.__name__):
                    ns["submodules"] = sys.all_submodules[obj.__name__]

                ns["members"] = ns["all_members"]

                try:
                    obj_dict = safe_getattr(obj, "__dict__")
                except AttributeError:
                    obj_dict = []

                public = [x for x in obj_dict if not x.startswith("_")]
                for item in ns["classes"] + ns["functions"] + ns["exceptions"]:
                    if item in public:
                        public.remove(item)

                public.sort()
                ns["members"] = public
                ns["constants"] = [x for x in public if not sys.skip_member(x, obj)]

            elif doc.objtype == "class":
                ns["members"] = dir(obj)
                ns["events"], ns["all_events"] = get_members(obj, "event")
                ns["methods"], ns["all_methods"] = get_members(obj, "method", ["__init__"])
                ns["attributes"], ns["all_attributes"] = get_members(obj, "attribute")

                ns["def_events"] = def_members(obj, "event")
                ns["def_methods"] = def_members(obj, "method", ["__init__"])
                ns["def_attributes"] = def_members(obj, "attribute")

                ns["inherited"] = []
                for t in ["events", "methods", "attributes"]:
                    key = "inh_" + t
                    ns[key] = []
                    for item in ns[t]:
                        if not item in ns["def_" + t]:
                            ns["inherited"].append(item)
                            ns[key].append(item)

            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()