Ejemplo n.º 1
0
def runExampleInterfaceTest(exampleInterface):
    required_metadata = {
        'subject': '04',
        'task': 'story',
        'suffix': 'bold',
        'datatype': 'func',
        'run': 1
    }
    other_metadata = {
        'a1': [1, 'two', 3.0],
        'a2': {
            'np': numpy.float32(3),
            'pyint': 4,
            'str': 'five'
        },
        'a3': [6.0, 'seven', numpy.int(8)]
    }
    args = (4, 'hello', required_metadata)
    kwargs = {
        'mdata': other_metadata,
        'test1': 9.0,
        'test2': numpy.float32(9),
        'test3': 'yes'
    }
    res = exampleInterface.testMethod(*args, **kwargs)
    print(f'testMethod returned {res}')
    args_py = npToPy(args)
    kwargs_py = npToPy(kwargs)
    assert tuple(res[0]) == args_py
    assert res[1] == kwargs_py
Ejemplo n.º 2
0
 def handleRPCRequest(self, channelName, cmd, timeout=60):
     """Process RPC requests using websocket RequestHandler to send the request"""
     """Caller will catch exceptions"""
     handler = self.handlers[channelName]
     if handler is None:
         raise StateError(f'RPC Handler {channelName} not registered')
     savedError = None
     incomplete = True
     # print(f'handle request {cmd}')
     if cmd.get('cmd') == 'rpc':
         # if cmd is rpc, check and encode any byte args as base64
         cmd = encodeByteTypeArgs(cmd)
         # Convert numpy arguments to native python types
         cmd['args'] = npToPy(cmd.get('args', ()))
         cmd['kwargs'] = npToPy(cmd.get('kwargs', {}))
     while incomplete:
         response = handler.doRequest(cmd, timeout)
         if response.get('status') != 200:
             errStr = 'handleDataRequest: status {}, err {}'.format(
                 response.get('status'), response.get('error'))
             self.setError(errStr)
             raise RequestError(errStr)
         try:
             data = unpackDataMessage(response)
         except Exception as err:
             errStr = 'handleDataRequest: unpackDataMessage: {}'.format(err)
             logging.error(errStr)
             if savedError is None:
                 savedError = errStr
         incomplete = response.get('incomplete', False)
         cmd['callId'] = response.get('callId', -1)
         cmd['incomplete'] = incomplete
     if savedError:
         self.setError(savedError)
         raise RequestError(savedError)
     serializationType = response.get('dataSerialization')
     if serializationType == 'json':
         if type(data) is bytes:
             data = data.decode()
         data = json.loads(data)
     elif serializationType == 'pickle':
         data = pickle.loads(data)
     return data
Ejemplo n.º 3
0
 def _sendMessageToWeb(self, msg):
     """Helper function used by the other methods to send a message to the web page"""
     if self.ioLoopInst is not None:
         msg = npToPy(msg)
         json_msg = json.dumps(msg)
         self.ioLoopInst.add_callback(sendWebSocketMessage,
                                      wsName='wsUser',
                                      msg=json_msg)
     else:
         print(f'WebDisplayMsg {msg}')
Ejemplo n.º 4
0
 def test_npToPy(self):
     data1 = {
         'subject': '04',
         'task': 'story',
         'suffix': 'bold',
         'datatype': 'func',
         'run': 1
     }
     data2 = {
         'a1': (1, 'two', 3.0),
         'a2': {
             'np': numpy.float32(3),
             'pyint': 4,
             'str': 'five'
         },
         'a3': [6.0, 'seven',
                numpy.int(8), {'a', numpy.float32(5), 'c'}]
     }
     data2_py = {
         'a1': (1, 'two', 3.0),
         'a2': {
             'np': 3.0,
             'pyint': 4,
             'str': 'five'
         },
         'a3': [6.0, 'seven', 8.0, {'a', 5.0, 'c'}]
     }
     kwargs = {
         'mdata': data2,
         'test1': 9.0,
         'test2': numpy.float32(9),
         'test3': 'yes'
     }
     kwargs_py = {
         'mdata': data2_py,
         'test1': 9.0,
         'test2': 9.0,
         'test3': 'yes'
     }
     args = (4, 'hello', data1, kwargs)
     args_py = (4, 'hello', data1, kwargs_py)
     res = putils.npToPy(args)
     assert res == args_py