Skip to content

Commit

Permalink
support multiple + after module/class name to include members recursi…
Browse files Browse the repository at this point in the history
…vely
  • Loading branch information
NiklasRosenstein committed Jan 29, 2017
1 parent 0bc0e78 commit 3db01cb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ Highly insipired by the [Keras] Documentation.

__Todo__

- [ ] Support `+` suffix to include documented members of a module/class
- [x] Support `+` suffix to include documented members of a module/class
- [ ] Expand and link cross-references (eg. `#SomeClass`)
- [ ] Parse, format and link types listed in parameter/member/raise/return type
docstrings (eg. `someattr (int): This is...`)
Expand Down Expand Up @@ -50,6 +50,9 @@ generate:
# Indenting the following items to give them a smaller header size
- foobar.baz.CoolClass+ # Class docstring (+ to include members)
- foobar.baz.some_function # Function docstring
- baz/more-stuff.md:
- foobar.more++ # foobar.more module, plus 2 more levels (eg.
# classes and their members)

# MkDocs pages configuration, with some sugar.
pages:
Expand Down
18 changes: 4 additions & 14 deletions docs/pydocmd.yml
Expand Up @@ -2,23 +2,13 @@ site_name: pyodoc-markdown Documentation
repo_url: https://github.com/NiklasRosenstein/pydoc-markdown
generate:
- document.md:
- pydocmd.document:
- pydocmd.document.Section
- pydocmd.document.Document
- pydocmd.document.Index
- pydocmd.document++
- imp.md:
- pydocmd.imp:
- pydocmd.imp.import_module
- pydocmd.imp.import_object
- pydocmd.imp.import_object_with_scope
- pydocmd.imp+
- extensions/loader.md:
- pydocmd.loader:
- pydocmd.loader.PythonLoader:
- pydocmd.loader.PythonLoader.load_section
- pydocmd.loader++
- extensions/preprocessor.md:
- pydocmd.preprocessor:
- pydocmd.preprocessor.Preprocessor:
- pydocmd.preprocessor.Preprocessor.preprocess_section
- pydocmd.preprocessor++
pages:
- Home: index.md << ../README.md
- API Documentation:
Expand Down
26 changes: 16 additions & 10 deletions pydocmd/__main__.py
Expand Up @@ -148,16 +148,22 @@ def add_sections(doc, object_names, depth=1):
add_sections(doc, key, depth)
add_sections(doc, subsections, depth + 1)
elif isinstance(object_names, str):
if object_names.endswith('+'):
object_names = object_names[:-1]
index.new_section(doc, object_names, depth=depth)

for sub in dir_object(object_names):
index.new_section(
doc, '.'.join((object_names, sub)), depth=depth + 1)
else:
index.new_section(doc, object_names, depth=depth)
else: raise RuntimeError(object_names)
# Check how many levels of recursion we should be going.
expand_depth = len(object_names)
object_names = object_names.rstrip('+')
expand_depth -= len(object_names)

def create_sections(name, level):
if level > expand_depth: return
index.new_section(doc, name, depth=depth + level)
for sub in dir_object(name):
sub = name + '.' + sub
create_sections(sub, level + 1)

create_sections(object_names, 0)
else:
raise RuntimeError(object_names)

for pages in config.get('generate') or []:
for fname, object_names in pages.items():
doc = index.new_document(fname)
Expand Down
25 changes: 17 additions & 8 deletions pydocmd/imp.py
Expand Up @@ -21,6 +21,8 @@
This module provides utilities for importing Python objects by name.
"""

import types


def import_module(name):
"""
Expand Down Expand Up @@ -76,11 +78,18 @@ def import_object_with_scope(name):


def dir_object(name):
obj, scope = import_object_with_scope(name)
if hasattr(obj, '__dict__'):
return [key
for key, value in obj.__dict__.items()
if not key.startswith('_') and
getattr(value, '__doc__', '')]
return []
return []
prefix = None
obj = import_object(name)
if isinstance(obj, types.ModuleType):
prefix = obj.__name__
all = getattr(obj, '__all__', None)

result = []
for key, value in getattr(obj, '__dict__', {}).items():
if key.startswith('_'): continue
if not getattr(value, '__doc__'): continue
if all is not None and key not in all: continue
if prefix is not None and getattr(value, '__module__', None) != prefix:
continue
result.append(key)
return result

0 comments on commit 3db01cb

Please sign in to comment.