@@ -32,6 +32,7 @@ extensions = [
3232 "sphinx.ext.viewcode" ,
3333 "sphinx.ext.githubpages" ,
3434 "sphinx.ext.autodoc" ,
35+ "sphinx.ext.autosummary" ,
3536 "sphinx.ext.napoleon" ,
3637]
3738
@@ -69,7 +70,7 @@ source_suffix = ".rst"
6970# This pattern also affects html_static_path and html_extra_path.
7071exclude_patterns = ["_build" , "Thumbs.db" , ".DS_Store" ]
7172
72- highlight_language = "c "
73+ highlight_language = "Python "
7374
7475# The name of the Pygments (syntax highlighting) style to use.
7576pygments_style = "sphinx"
@@ -140,3 +141,92 @@ if generate_multiversion == "ON":
140141 html_context ["versions" ].append (
141142 (version , DOC_SITE_NAME + version + "/index.html" )
142143 )
144+
145+ # Add an "autoclassmembers" directive that acts the same way as
146+ # "autoclass", but does not print out the class doc.
147+
148+ from sphinx .ext .autodoc import ClassDocumenter , ObjectMembers
149+ from sphinx .ext .autodoc .importer import get_class_members
150+
151+
152+ class ClassMembersDocumenter (ClassDocumenter ):
153+ """
154+ Documenter for only class members and skips the class and __init__
155+ docstrings.
156+ """
157+
158+ objtype = "classmembers"
159+
160+ def add_directive_header (self , sig ):
161+ pass
162+
163+ def get_doc (self , encoding = None , ignore = None ):
164+ return None
165+
166+
167+ # Add an "autoautosummary" directive to add a summary table of class
168+ # members and attributes.
169+ # See https://stackoverflow.com/questions/20569011/python-sphinx-autosummary-automated-listing-of-member-functions
170+
171+ from sphinx .ext .autosummary import Autosummary
172+ from sphinx .ext .autosummary import get_documenter
173+ from docutils .parsers .rst import directives
174+ from sphinx .util .inspect import safe_getattr
175+
176+
177+ class AutoAutoSummary (Autosummary ):
178+ """Create a summary for methods and attributes (autosummary).
179+ See https://stackoverflow.com/questions/20569011/python-sphinx-autosummary-automated-listing-of-member-functions
180+ """
181+
182+ option_spec = {
183+ "methods" : directives .unchanged ,
184+ "private_methods" : directives .unchanged ,
185+ "attributes" : directives .unchanged ,
186+ }
187+
188+ required_arguments = 1
189+
190+ @staticmethod
191+ def get_members (app , obj , typ , include_public = None ):
192+ if not include_public :
193+ include_public = []
194+ items = []
195+ for name in sorted (obj .__dict__ .keys ()):
196+ try :
197+ documenter = get_documenter (app , safe_getattr (obj , name ), obj )
198+ except AttributeError :
199+ continue
200+ if documenter .objtype in typ :
201+ items .append (name )
202+ return items
203+
204+ def run (self ):
205+ clazz = str (self .arguments [0 ])
206+ (module_name , class_name ) = clazz .rsplit ("." , 1 )
207+ m = __import__ (module_name , globals (), locals (), [class_name ])
208+ c = getattr (m , class_name )
209+ app = self .state .document .settings .env .app
210+ if "methods" in self .options :
211+ methods = self .get_members (app , c , ["method" ], ["__init__" ])
212+ self .content = [
213+ "%s" % method for method in methods if not method .startswith ("_" )
214+ ]
215+ if "private_methods" in self .options :
216+ private_methods = self .get_members (app , c , ["method" ], ["__init__" ])
217+ self .content = [
218+ "%s" % method
219+ for method in private_methods
220+ if method .startswith ("_" ) and not method .startswith ("__" )
221+ ]
222+ if "attributes" in self .options :
223+ attribs = self .get_members (app , c , ["attribute" , "property" ])
224+ self .content = [
225+ "%s" % attrib for attrib in attribs if not attrib .startswith ("_" )
226+ ]
227+ return super ().run ()
228+
229+
230+ def setup (app ):
231+ app .add_directive ("autoautosummary" , AutoAutoSummary )
232+ app .add_autodocumenter (ClassMembersDocumenter )
0 commit comments