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