Created
November 10, 2017 03:50
-
-
Save quietcricket/ea2ad3b27b430094dfbbf00a81963ff8 to your computer and use it in GitHub Desktop.
Customize sphinx for projects using mongoengine ORM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
If you try to generate documentations using sphinx's autodoc with models extending mongoengine's Document You are going to have a bad time | |
1. The code will call Document.objects and list all the entries in your doc!!! | |
2. Your model's fields will inherit the default docstr from Field class if you don't have any comment. The documentation generated is very long and difficult to read | |
This snippet helps to solve these 2 problems. Insert them into config.py file. | |
""" | |
import re | |
from mongoengine.base.fields import ObjectIdField | |
from mongoengine.queryset.queryset import QuerySet | |
# | |
default_docstr_patterns = ['A(n?) .+ field', '32-bit integer field', | |
'Datetime field', 'A reference to a document'] | |
ignore_pattern = '^' + '|'.join(['(%s)' % p for p in default_docstr_patterns]) | |
def autodoc_process_docstring(app, what, name, obj, options, lines): | |
""" | |
Remove default docstr for IntField, DatetimeField, ReferenceField etc. | |
""" | |
if lines: | |
if re.match(ignore_pattern, lines[0]): | |
for i in range(0, len(lines)): | |
lines[i] = '' | |
def autodoc_skip_member(app, what, name, obj, skip, options): | |
""" | |
Exclude objects and id from Mongoengine models | |
""" | |
exclude = isinstance(obj, QuerySet) or (name == 'id' and isinstance(obj, ObjectIdField)) | |
return skip or exclude | |
def setup(app): | |
app.connect('autodoc-skip-member', autodoc_skip_member) | |
app.connect('autodoc-process-docstring', autodoc_process_docstring) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment