1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
import inspect
import os
class Inspect(object):
"""An inspector for a given module"""
module_header = """Scripting API reference for `%(module)s`
===================================
.. automodule:: %(module)s
.. inheritance-diagram: GPS # DISABLED, add "::" to enable
"""
functions_header = """
Functions
---------
"""
function_stub = ".. autofunction:: %(name)s\n"
classes_header = """
Classes
-------
"""
class_stub = """
:class:`%(module)s.%(name)s`
%(underscore)s
.. autoclass:: %(name)s
%(inheritance)s
"""
method_stub = """
.. automethod:: %(name)s
"""
data_stub = """
.. autoattribute:: %(name)s
"""
exceptions_header = """
Exceptions
-------
"""
def __init__(self, module):
self.module = module
self.func = []
self.classes = []
self.excepts = []
for obj_name, obj in module.__dict__.iteritems():
if obj_name.startswith('__') \
and obj_name not in ["__init__"]:
pass
elif inspect.ismodule(obj):
pass
elif inspect.isfunction(obj) or inspect.isroutine(obj):
self.func.append(obj_name)
elif isinstance(obj, Exception):
self.excepts.append(obj_name)
elif inspect.isclass(obj):
self.classes.append((obj_name, obj))
self.func.sort()
self.excepts.sort()
self.classes.sort()
def __methods(self, cls):
"""Returns the methods and data of the class"""
methods = []
data = []
for name, kind, defined, obj in inspect.classify_class_attrs(cls):
if defined != cls:
pass # Inherited method
elif name.startswith("__") \
and name not in ["__init__"]:
pass
elif kind in ["method", "static method", "class method"]:
methods.append(name)
elif kind in ["property", "data"]:
data.append(name)
else:
print "Unknown kind (%s) for %s.%s" % (
kind, defined.__name__, name)
methods.sort()
data.sort()
return (data, methods)
def generate_rest(self):
"""Generate a REST file for the given module.
The output should be processed by sphinx.
"""
n = self.module.__name__
fd = file("%s.rst" % n, "w")
fd.write(".. This file is automatically generated, do not edit\n\n")
fd.write(Inspect.module_header % {"module": n})
if self.func:
fd.write(Inspect.functions_header)
for f in self.func:
fd.write(Inspect.function_stub %
{"name": f, "module": n})
if self.classes:
fd.write(Inspect.classes_header)
for name, c in self.classes:
# Only show inheritance diagram if base classes are other
# than just "object"
inheritance = ""
mro = inspect.getmro(c) # first member is always c
if len(mro) > 2 \
or (len(mro) == 2
and mro[1].__name__ != "object"):
inheritance = \
".. inheritance-diagram:: %s.%s" % (n, name)
fd.write(Inspect.class_stub %
{"name": name,
"inheritance": inheritance,
"underscore": "^" * (len(name) + len(n) + 10),
"module": n})
data, methods = self.__methods(c)
for d in data:
mname = "%s.%s.%s" % (n, name, d)
fd.write(Inspect.data_stub %
{"name": mname,
"base_name": d,
"underscore": "*" * (len(d) + 8)})
for m in methods:
mname = "%s.%s.%s" % (n, name, m)
fd.write(Inspect.method_stub %
{"name": mname,
"base_name": m,
"inheritance":
".. inheritance-diagram:: %s.%s" % (n, c),
"underscore": "*" * (len(m) + 8)})
if self.excepts:
fd.write(Inspect.exceptions_header)
for c in self.excepts:
fd.write(Inspect.class_stub %
{"name": c,
"inheritance":
".. inheritance-diagram:: %s.%s" % (n, c),
"underscore": "^" * (len(c) + len(n) + 10),
"module": n})
import GPS
Inspect(GPS).generate_rest()
GPS.exit()
|