def handle_call(self, call): if (call.type != 'procedure_call'): raise SCSCPProtocolError('Bad message from client: %s.' % call.type, om=call.om()) try: head = call.data.elem.name self.log.debug('Requested head: %s...' % head) if call.data.elem.cd == 'scscp2' and head in CD_SCSCP2: res = getattr(self, head)(call.data) elif call.data.elem.cd == 'arith1' and head in CD_ARITH1: args = [conv.to_python(a) for a in call.data.arguments] res = conv.to_openmath(CD_ARITH1[head](*args)) else: self.log.debug('...head unknown.') return self.scscp.terminated(call.id, om.OMError( om.OMSymbol('unhandled_symbol', cd='error'), [call.data.elem])) strlog = str(res) self.log.debug('...sending result: %s' % (strlog[:20] + (len(strlog) > 20 and '...'))) return self.scscp.completed(call.id, res) except (AttributeError, IndexError, TypeError): self.log.debug('...client protocol error.') return self.scscp.terminated(call.id, om.OMError( om.OMSymbol('unexpected_symbol', cd='error'), [call.data])) except Exception as e: self.log.exception('Unhandled exception:') return self.scscp.terminated(call.id, 'system_specific', 'Unhandled exception %s.' % str(e))
def handle_call(self, call): if (call.type != 'procedure_call'): raise SCSCPProtocolError('Bad message from client: %s.' % call.type, om=call.om()) try: head = call.data.elem.name self.log.debug('Requested head: %s...' % head) if call.data.elem.cd == 'scscp2' and head in CD_SCSCP2: res = getattr(self, head)(call.data) elif call.data.elem.cd == 'singular' and head in CD_SINGULAR: #args = [conv.to_python(a) for a in call.data.arguments] args = call.data.arguments handler = get_handler(head) name = makename() handler(name, args) res = retrieve(name) else: self.log.debug('...head unknown.') return self.scscp.terminated(call.id, om.OMError( om.OMSymbol('unhandled_symbol', cd='error'), [call.data.elem])) strlog = str(res) print(colored(strlog, "green")) self.log.debug('...sending result: %s' % (strlog[:20] + (len(strlog) > 20 and '...'))) return self.scscp.completed(call.id, res) except (AttributeError, IndexError, TypeError) as e: traceback.print_exc() self.log.debug('...client protocol error.') return self.scscp.terminated(call.id, om.OMError( om.OMSymbol('unexpected_symbol', cd='error'), [call.data])) except Exception as e: self.log.exception('Unhandled exception:') return self.scscp.terminated(call.id, 'system_specific', 'Unhandled exception %s.' % str(e))
def __handle_call(self, call): """ Safely handles a call """ if (call.type != 'procedure_call'): raise SCSCPProtocolError('Bad message from client: %s.' % call.type, om=call.om()) try: head = call.data.elem.name self.log.debug('Requested head: %s...' % head) # for the methods in scscp2, use the class methods if call.data.elem.cd == 'scscp2': if not head in CD_SCSCP2: raise SCSCPUnknownHead res = getattr(self, head)(call.data) # else, handle the call internally else: res = self.handle_call(call, head) strlog = str(res) self.log.debug('...sending result: %s' % (strlog[:20] + ('...' if len(strlog) > 20 else ''))) # if we already constructed a procedure message # else just return it as is if isinstance(res, SCSCPProcedureMessage): return res # else, else: return self.scscp.completed(call.id, res) # User-thrown execption: I don't know this head except SCSCPUnknownHead: self.log.debug('...head unknown.') return self.scscp.terminated( call.id, om.OMError(om.OMSymbol('unhandled_symbol', cd='error'), [call.data.elem])) # we tried to look up something, but it wasn't given by the client except (AttributeError, IndexError, TypeError): self.log.debug('...client protocol error.') return self.scscp.terminated( call.id, om.OMError(om.OMSymbol('unexpected_symbol', cd='error'), [call.data])) # anything else except Exception as e: self.log.exception('Unhandled exception:') return self.scscp.terminated(call.id, 'system_specific', 'Unhandled exception %s.' % str(e))
def terminated(cls, id, error, msg=None, **info): if not isinstance(error, om.OMError): if cls.errors[error] and msg is None: raise RuntimeError('Must give an error message') error = om.OMError(om.OMSymbol('error_' + error, cd='scscp1'), [om.OMString(msg)]) return cls._w_info('procedure_terminated', id, error, **info)
def no_such_transient_cd(cd): return om.OMError(om.OMSymbol('no_such_transient_cd', cd='scscp2'), [om.OMString(cd)])