| 1 |
"""Self documenting XML-RPC Server. |
| 2 |
|
| 3 |
This module can be used to create XML-RPC servers that |
| 4 |
serve pydoc-style documentation in response to HTTP |
| 5 |
GET requests. This documentation is dynamically generated |
| 6 |
based on the functions and methods registered with the |
| 7 |
server. |
| 8 |
|
| 9 |
This module is built upon the pydoc and SimpleXMLRPCServer |
| 10 |
modules. |
| 11 |
""" |
| 12 |
|
| 13 |
import pydoc |
| 14 |
import inspect |
| 15 |
import re |
| 16 |
import sys |
| 17 |
|
| 18 |
from SimpleXMLRPCServer import (SimpleXMLRPCServer, |
| 19 |
SimpleXMLRPCRequestHandler, |
| 20 |
CGIXMLRPCRequestHandler, |
| 21 |
resolve_dotted_attribute) |
| 22 |
|
| 23 |
|
| 24 |
def _html_escape_quote(s): |
| 25 |
s = s.replace("&", "&") |
| 26 |
s = s.replace("<", "<") |
| 27 |
s = s.replace(">", ">") |
| 28 |
s = s.replace('"', """) |
| 29 |
s = s.replace('\'', "'") |
| 30 |
return s |
| 31 |
|
| 32 |
|
| 33 |
class ServerHTMLDoc(pydoc.HTMLDoc): |
| 34 |
"""Class used to generate pydoc HTML document for a server""" |
| 35 |
|
| 36 |
def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 37 |
"""Mark up some plain text, given a context of symbols to look for. |
| 38 |
Each context dictionary maps object names to anchor names.""" |
| 39 |
escape = escape or self.escape |
| 40 |
results = [] |
| 41 |
here = 0 |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|' |
| 48 |
r'RFC[- ]?(\d+)|' |
| 49 |
r'PEP[- ]?(\d+)|' |
| 50 |
r'(self\.)?((?:\w|\.)+))\b') |
| 51 |
while 1: |
| 52 |
match = pattern.search(text, here) |
| 53 |
if not match: break |
| 54 |
start, end = match.span() |
| 55 |
results.append(escape(text[here:start])) |
| 56 |
|
| 57 |
all, scheme, rfc, pep, selfdot, name = match.groups() |
| 58 |
if scheme: |
| 59 |
url = escape(all).replace('"', '"') |
| 60 |
results.append('<a href="%s">%s</a>' % (url, url)) |
| 61 |
elif rfc: |
| 62 |
url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 63 |
results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 64 |
elif pep: |
| 65 |
url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep) |
| 66 |
results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 67 |
elif text[end:end+1] == '(': |
| 68 |
results.append(self.namelink(name, methods, funcs, classes)) |
| 69 |
elif selfdot: |
| 70 |
results.append('self.<strong>%s</strong>' % name) |
| 71 |
else: |
| 72 |
results.append(self.namelink(name, classes)) |
| 73 |
here = end |
| 74 |
results.append(escape(text[here:])) |
| 75 |
return ''.join(results) |
| 76 |
|
| 77 |
def docroutine(self, object, name, mod=None, |
| 78 |
funcs={}, classes={}, methods={}, cl=None): |
| 79 |
"""Produce HTML documentation for a function or method object.""" |
| 80 |
|
| 81 |
anchor = (cl and cl.__name__ or '') + '-' + name |
| 82 |
note = '' |
| 83 |
|
| 84 |
title = '<a name="%s"><strong>%s</strong></a>' % ( |
| 85 |
self.escape(anchor), self.escape(name)) |
| 86 |
|
| 87 |
if inspect.ismethod(object): |
| 88 |
args, varargs, varkw, defaults = inspect.getargspec(object.im_func) |
| 89 |
|
| 90 |
|
| 91 |
argspec = inspect.formatargspec ( |
| 92 |
args[1:], |
| 93 |
varargs, |
| 94 |
varkw, |
| 95 |
defaults, |
| 96 |
formatvalue=self.formatvalue |
| 97 |
) |
| 98 |
elif inspect.isfunction(object): |
| 99 |
args, varargs, varkw, defaults = inspect.getargspec(object) |
| 100 |
argspec = inspect.formatargspec( |
| 101 |
args, varargs, varkw, defaults, formatvalue=self.formatvalue) |
| 102 |
else: |
| 103 |
argspec = '(...)' |
| 104 |
|
| 105 |
if isinstance(object, tuple): |
| 106 |
argspec = object[0] or argspec |
| 107 |
docstring = object[1] or "" |
| 108 |
else: |
| 109 |
docstring = pydoc.getdoc(object) |
| 110 |
|
| 111 |
decl = title + argspec + (note and self.grey( |
| 112 |
'<font face="helvetica, arial">%s</font>' % note)) |
| 113 |
|
| 114 |
doc = self.markup( |
| 115 |
docstring, self.preformat, funcs, classes, methods) |
| 116 |
doc = doc and '<dd><tt>%s</tt></dd>' % doc |
| 117 |
return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc) |
| 118 |
|
| 119 |
def docserver(self, server_name, package_documentation, methods): |
| 120 |
"""Produce HTML documentation for an XML-RPC server.""" |
| 121 |
|
| 122 |
fdict = {} |
| 123 |
for key, value in methods.items(): |
| 124 |
fdict[key] = '#-' + key |
| 125 |
fdict[value] = fdict[key] |
| 126 |
|
| 127 |
server_name = self.escape(server_name) |
| 128 |
head = '<big><big><strong>%s</strong></big></big>' % server_name |
| 129 |
result = self.heading(head, '#ffffff', '#7799ee') |
| 130 |
|
| 131 |
doc = self.markup(package_documentation, self.preformat, fdict) |
| 132 |
doc = doc and '<tt>%s</tt>' % doc |
| 133 |
result = result + '<p>%s</p>\n' % doc |
| 134 |
|
| 135 |
contents = [] |
| 136 |
method_items = sorted(methods.items()) |
| 137 |
for key, value in method_items: |
| 138 |
contents.append(self.docroutine(value, key, funcs=fdict)) |
| 139 |
result = result + self.bigsection( |
| 140 |
'Methods', '#ffffff', '#eeaa77', pydoc.join(contents)) |
| 141 |
|
| 142 |
return result |
| 143 |
|
| 144 |
class XMLRPCDocGenerator: |
| 145 |
"""Generates documentation for an XML-RPC server. |
| 146 |
|
| 147 |
This class is designed as mix-in and should not |
| 148 |
be constructed directly. |
| 149 |
""" |
| 150 |
|
| 151 |
def __init__(self): |
| 152 |
|
| 153 |
self.server_name = 'XML-RPC Server Documentation' |
| 154 |
self.server_documentation = \ |
| 155 |
"This server exports the following methods through the XML-RPC "\ |
| 156 |
"protocol." |
| 157 |
self.server_title = 'XML-RPC Server Documentation' |
| 158 |
|
| 159 |
def set_server_title(self, server_title): |
| 160 |
"""Set the HTML title of the generated server documentation""" |
| 161 |
|
| 162 |
self.server_title = server_title |
| 163 |
|
| 164 |
def set_server_name(self, server_name): |
| 165 |
"""Set the name of the generated HTML server documentation""" |
| 166 |
|
| 167 |
self.server_name = server_name |
| 168 |
|
| 169 |
def set_server_documentation(self, server_documentation): |
| 170 |
"""Set the documentation string for the entire server.""" |
| 171 |
|
| 172 |
self.server_documentation = server_documentation |
| 173 |
|
| 174 |
def generate_html_documentation(self): |
| 175 |
"""generate_html_documentation() => html documentation for the server |
| 176 |
|
| 177 |
Generates HTML documentation for the server using introspection for |
| 178 |
installed functions and instances that do not implement the |
| 179 |
_dispatch method. Alternatively, instances can choose to implement |
| 180 |
the _get_method_argstring(method_name) method to provide the |
| 181 |
argument string used in the documentation and the |
| 182 |
_methodHelp(method_name) method to provide the help text used |
| 183 |
in the documentation.""" |
| 184 |
|
| 185 |
methods = {} |
| 186 |
|
| 187 |
for method_name in self.system_listMethods(): |
| 188 |
if method_name in self.funcs: |
| 189 |
method = self.funcs[method_name] |
| 190 |
elif self.instance is not None: |
| 191 |
method_info = [None, None] |
| 192 |
if hasattr(self.instance, '_get_method_argstring'): |
| 193 |
method_info[0] = self.instance._get_method_argstring(method_name) |
| 194 |
if hasattr(self.instance, '_methodHelp'): |
| 195 |
method_info[1] = self.instance._methodHelp(method_name) |
| 196 |
|
| 197 |
method_info = tuple(method_info) |
| 198 |
if method_info != (None, None): |
| 199 |
method = method_info |
| 200 |
elif not hasattr(self.instance, '_dispatch'): |
| 201 |
try: |
| 202 |
method = resolve_dotted_attribute( |
| 203 |
self.instance, |
| 204 |
method_name |
| 205 |
) |
| 206 |
except AttributeError: |
| 207 |
method = method_info |
| 208 |
else: |
| 209 |
method = method_info |
| 210 |
else: |
| 211 |
assert 0, "Could not find method in self.functions and no "\ |
| 212 |
"instance installed" |
| 213 |
|
| 214 |
methods[method_name] = method |
| 215 |
|
| 216 |
documenter = ServerHTMLDoc() |
| 217 |
documentation = documenter.docserver( |
| 218 |
self.server_name, |
| 219 |
self.server_documentation, |
| 220 |
methods |
| 221 |
) |
| 222 |
|
| 223 |
title = _html_escape_quote(self.server_title) |
| 224 |
return documenter.page(title, documentation) |
| 225 |
|
| 226 |
class DocXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): |
| 227 |
"""XML-RPC and documentation request handler class. |
| 228 |
|
| 229 |
Handles all HTTP POST requests and attempts to decode them as |
| 230 |
XML-RPC requests. |
| 231 |
|
| 232 |
Handles all HTTP GET requests and interprets them as requests |
| 233 |
for documentation. |
| 234 |
""" |
| 235 |
|
| 236 |
def do_GET(self): |
| 237 |
"""Handles the HTTP GET request. |
| 238 |
|
| 239 |
Interpret all HTTP GET requests as requests for server |
| 240 |
documentation. |
| 241 |
""" |
| 242 |
|
| 243 |
if not self.is_rpc_path_valid(): |
| 244 |
self.report_404() |
| 245 |
return |
| 246 |
|
| 247 |
response = self.server.generate_html_documentation() |
| 248 |
self.send_response(200) |
| 249 |
self.send_header("Content-type", "text/html") |
| 250 |
self.send_header("Content-length", str(len(response))) |
| 251 |
self.end_headers() |
| 252 |
self.wfile.write(response) |
| 253 |
|
| 254 |
class DocXMLRPCServer( SimpleXMLRPCServer, |
| 255 |
XMLRPCDocGenerator): |
| 256 |
"""XML-RPC and HTML documentation server. |
| 257 |
|
| 258 |
Adds the ability to serve server documentation to the capabilities |
| 259 |
of SimpleXMLRPCServer. |
| 260 |
""" |
| 261 |
|
| 262 |
def __init__(self, addr, requestHandler=DocXMLRPCRequestHandler, |
| 263 |
logRequests=1, allow_none=False, encoding=None, |
| 264 |
bind_and_activate=True): |
| 265 |
SimpleXMLRPCServer.__init__(self, addr, requestHandler, logRequests, |
| 266 |
allow_none, encoding, bind_and_activate) |
| 267 |
XMLRPCDocGenerator.__init__(self) |
| 268 |
|
| 269 |
class DocCGIXMLRPCRequestHandler( CGIXMLRPCRequestHandler, |
| 270 |
XMLRPCDocGenerator): |
| 271 |
"""Handler for XML-RPC data and documentation requests passed through |
| 272 |
CGI""" |
| 273 |
|
| 274 |
def handle_get(self): |
| 275 |
"""Handles the HTTP GET request. |
| 276 |
|
| 277 |
Interpret all HTTP GET requests as requests for server |
| 278 |
documentation. |
| 279 |
""" |
| 280 |
|
| 281 |
response = self.generate_html_documentation() |
| 282 |
|
| 283 |
print 'Content-Type: text/html' |
| 284 |
print 'Content-Length: %d' % len(response) |
| 285 |
print |
| 286 |
sys.stdout.write(response) |
| 287 |
|
| 288 |
def __init__(self): |
| 289 |
CGIXMLRPCRequestHandler.__init__(self) |
| 290 |
XMLRPCDocGenerator.__init__(self) |