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