在(I)Python中为XMLRPC-Client自动完成函数参数

时间:2014-05-15 15:08:11

标签: python autocomplete xmlrpclib

我正在使用xmlrpclib通过IPython以交互方式远程控制某些实验室设备。我喜欢IPython的自动完成功能,我也希望通过xmlrpclib获得它。到目前为止,我设法通过以下方法完成方法名称完成和方法帮助。

一个模拟电动载物台的小型测试服务器(如果你想测试我的客户端代码,这只是有用的):

import time  # needed for StageSimulation
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler

class StageSimulation:
    """ A very simple simulation of a motorized linear stage """
    v = 5 # mm/s -- speed of stage
    goalPos = 0 # mm -- goal Position in mm
    goalTime = 0 # sec -- time when goal position should be reached


    def getPos(self):
        """ Return actual Position of stage """
        delta_t = self.goalTime - time.time()  # remaining moving time
        if delta_t <= 0 :  # stage is not moving
            return self.goalPos
        else:  # stage is moving
            return self.goalPos - self.v*delta_t

    def move(self, goalPos, v=5):
        """ Move stage to position ``goalPos`` with speed ``v`` """
        p0 = self.getPos()
        delta_p = goalPos - p0

        if v*delta_p < 0: # sign of v wrong
            v *= -1
        self.goalTime = time.time() + delta_p/v
        self.goalPos, self.v = goalPos, v


# Restrict to a particular path (see python Docs)
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)


if __name__=='__main__':
    """ Instaniate Server """   
    host, hport = "localhost", 8787
    LogXMLRPCRequests = False
    server = SimpleXMLRPCServer((host, hport), allow_none=True,
                                requestHandler=RequestHandler)
    server.register_introspection_functions()

    StS = StageSimulation()
    server.register_instance(StS)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("Terminated server.")

我的客户端实例化一个已注册所有已知方法的对象:

import xmlrpclib


class XMLRPCClientObject(object):
    """XMLRPC Client which allows Tab completion on server instances

    This is achieved by reading all methods names from the server and 
    using them to generate local wrappers to them.
    """

    def __init__(self, url):
        """ Connect to server with ``url`` and generate methods """
        self.SP = xmlrpclib.ServerProxy(url)
        self.generateMethods()


    def generateMethods(self):
       """ Read names of server methods and use them for local wrappers """
       SP = self.SP
       for n in SP.system.listMethods():
           f = getattr(SP, n)
           f.__doc__  = SP.system.methodHelp(n)  # add doc string
           f.__name__ = n  # needed to make help() work
           setattr(self, n, f)  # register as local method 

if __name__ == "__main__":
    """ main function connects to Test Server """
    S = XMLRPCClientObject("http://localhost:8787")

除了方法名称完成之外,我还希望在S.move(goal<TAB>中完成参数名称。一种方法是使用xmlrpc.ServerProxy.system.methodSignature(),但system_methodSignature()不支持SimpleXMLRPCServer。有没有人知道如何检索服务器方法的签名?

1 个答案:

答案 0 :(得分:1)

我倾向于认为python inspect module可以提供帮助,它提供了可用于所需功能的基础知识