Package empro :: Package toolkit :: Module rpc
[frames] | no frames]

Source Code for Module empro.toolkit.rpc

  1  # Copyright 1983-2012 Keysight Technologies, Inc 
  2  import empro.core 
  3  import xml.dom 
  4  import xml.dom.minidom 
  5   
  6   
7 -def _getText(node):
8 text = [] 9 for n in node.childNodes: 10 if n.nodeType in (xml.dom.Node.TEXT_NODE, xml.dom.Node.CDATA_SECTION_NODE): 11 text.append(n.data) 12 return ''.join(text)
13 14
15 -def _convertElementToPython(element):
16 if element.tagName == "null": 17 return None 18 elif element.tagName == "int": 19 return int(_getText(element)) 20 elif element.tagName == "real": 21 return float(_getText(element)) 22 elif element.tagName == "string": 23 return _getText(element) 24 elif element.tagName == "list": 25 result = [] 26 for listItem in element.childNodes: 27 if listItem.nodeType == xml.dom.Node.ELEMENT_NODE: 28 result.append(_convertElementToPython(listItem)) 29 return result 30 else: 31 raise RuntimeError("callRpcMethod: unsupported result data type <%s>" % element.tagName)
32 33
34 -def _createElementFromPython(value, doc, parentElement):
35 if value is None: 36 parentElement.appendChild(doc.createElement("null")) 37 elif isinstance(value, (int, long)): 38 parentElement.appendChild(doc.createElement("int")).appendChild(doc.createTextNode(str(long(value)))) 39 elif isinstance(value, float): 40 parentElement.appendChild(doc.createElement("real")).appendChild(doc.createTextNode(str(value))) 41 elif isinstance(value, basestring): 42 parentElement.appendChild(doc.createElement("string")).appendChild(doc.createTextNode(value)) 43 elif isinstance(value, list): 44 listElement = parentElement.appendChild(doc.createElement("list")) 45 for listItem in value: 46 _createElementFromPython(listItem, doc, listElement) 47 else: 48 raise RuntimeError("callRpcMethod: unsupported argument data type %s" % str(type(value)))
49 50
51 -def callRpcMethodBlockingOrNonBlocking(methodName, function, argsList=(), vocabulary="CmdOp", rpc=None):
52 rpc = rpc or empro.core.CommunicationWithParent.rpc() 53 doc = xml.dom.minidom.Document() 54 callElement = doc.appendChild(doc.createElement("call")) 55 callElement.appendChild(doc.createElement("method")).appendChild(doc.createTextNode(methodName)) 56 callElement.appendChild(doc.createElement("function")).appendChild(doc.createTextNode(function)) 57 callElement.appendChild(doc.createElement("vocabulary")).appendChild(doc.createTextNode(vocabulary)) 58 argsElement = callElement.appendChild(doc.createElement("args")) 59 for arg in argsList: 60 _createElementFromPython(arg, doc, argsElement) 61 reply = xml.dom.minidom.parseString(rpc.call(doc.toxml()).encode('utf-8')) 62 for replyElement in reply.childNodes: 63 if replyElement.nodeType == xml.dom.Node.ELEMENT_NODE: 64 for resultElement in replyElement.childNodes: 65 if resultElement.nodeType == xml.dom.Node.ELEMENT_NODE: 66 return _convertElementToPython(resultElement) 67 raise RuntimeError("%s: missing result in reply" % methodName)
68 69
70 -def callParent(function, argsList=(), vocabulary="CmdOp", rpc=None):
71 """ 72 callParent(function, argsList = (), vocabulary = \"CmdOp\", rpc = empro.core.CommunicationWithParent.rpc()). Calls the Skill function and returns the result. 73 function: Skill function to call 74 argsList: Python tuple or list of arguments (default empty). Can contain None (= Parent NULL), integers, floats, strings and lists. 75 vocabulary: theParent vocabulary, or \"CmdOp\" 76 rpc: the EmSubprocessRpc object executing the call (defaults to empro.core.CommunicationWithParent.rpc()) 77 """ 78 return callRpcMethodBlockingOrNonBlocking("apply", function, argsList, vocabulary, rpc)
79 80
81 -def callAEL(function, argsList=(), vocabulary="CmdOp", rpc=None):
82 """ 83 callAEL(function, argsList = (), vocabulary = \"CmdOp\", rpc = empro.core.CommunicationWithParent.rpc()). Calls the AEL function and returns the result. 84 function: AEL function to call 85 argsList: Python tuple or list of arguments (default empty). Can contain None (= AEL NULL), integers, floats, strings and lists. 86 vocabulary: theAEL vocabulary, or \"CmdOp\" 87 rpc: the EmSubprocessRpc object executing the call (defaults to empro.core.CommunicationWithParent.rpc()) 88 """ 89 return callRpcMethodBlockingOrNonBlocking("callAEL", function, argsList, vocabulary, rpc)
90 91
92 -def callAELNonBlocking(function, argsList=(), vocabulary="CmdOp", rpc=None):
93 """ 94 callAELNonBlocking(function, argsList = (), vocabulary = \"CmdOp\", rpc = empro.core.CommunicationWithParent.rpc()). Calls the AEL function without waiting for the result. 95 function: AEL function to call 96 argsList: Python tuple or list of arguments (default empty). Can contain None (= AEL NULL), integers, floats, strings and lists. 97 vocabulary: theAEL vocabulary, or \"CmdOp\" 98 rpc: the EmSubprocessRpc object executing the call (defaults to empro.core.CommunicationWithParent.rpc()) 99 """ 100 return callRpcMethodBlockingOrNonBlocking("callAELNonBlocking", function, argsList, vocabulary, rpc)
101 102
103 -def callIsShowingModalOrPopupWidget(rpc=None):
104 """ 105 callIsShowingModalOrPopupWidget(rpc = empro.core.CommunicationWithParent.rpc()). Returns whether the peer process is showing a modal or popup widget. 106 rpc: the EmSubprocessRpc object executing the call (defaults to empro.core.CommunicationWithParent.rpc()) 107 """ 108 rpc = rpc or empro.core.CommunicationWithParent.rpc() 109 reply = xml.dom.minidom.parseString(rpc.call("<call><method>isShowingModalOrPopupWidget</method></call>").encode('utf-8')) 110 for replyElement in reply.childNodes: 111 if replyElement.nodeType == xml.dom.Node.ELEMENT_NODE: 112 result = _getText(replyElement) 113 if result == "yes": 114 return True 115 elif result == "no": 116 return False 117 else: 118 raise RuntimeError("callIsShowingModalOrPopupWidget: invalid reply \"%s\"" % result) 119 raise RuntimeError("callIsShowingModalOrPopupWidget: missing result in reply")
120