2014-05-15 7 views
0

나는 실험실 장비를 IPython과 대화식으로 원격 제어하기 위해 을 사용하고 있습니다. IPython의 자동 완성 기능이 마음에 들었고 xmlrpclib을 통해 보내고 싶습니다. 지금까지 나는 메소드 이름 완성과 메소드 도움말을 다음과 같은 접근 방식으로 수행했다.(I) Python for XMLRPC-Client의 함수 매개 변수 자동 완성

전동 무대를 시뮬레이션하는 작은 테스트 서버 (당신이 내 클라이언트 코드를 테스트하려는 경우이 만 유용) :

:

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에서 지원되지 않습니다. 아무도 서버 메소드의 서명을 검색하는 방법을 알고 있습니까? 내가 python inspect module 도울 수 있다고 생각하는 경향이

답변

1

, 그것은 당신이 원하는 기능

+0

감사에 사용할 수있는 기초를 제공하고, 내 서버를 확장하기에 충분 보인다. – Dietrich