@@ -32,6 +32,7 @@ extensions = [
32
32
"sphinx.ext.viewcode" ,
33
33
"sphinx.ext.githubpages" ,
34
34
"sphinx.ext.autodoc" ,
35
+ "sphinx.ext.autosummary" ,
35
36
"sphinx.ext.napoleon" ,
36
37
]
37
38
@@ -69,7 +70,7 @@ source_suffix = ".rst"
69
70
# This pattern also affects html_static_path and html_extra_path.
70
71
exclude_patterns = ["_build" , "Thumbs.db" , ".DS_Store" ]
71
72
72
- highlight_language = "c "
73
+ highlight_language = "Python "
73
74
74
75
# The name of the Pygments (syntax highlighting) style to use.
75
76
pygments_style = "sphinx"
@@ -140,3 +141,92 @@ if generate_multiversion == "ON":
140
141
html_context ["versions" ].append (
141
142
(version , DOC_SITE_NAME + version + "/index.html" )
142
143
)
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