def _encode_pycls(self,pycls): """ Calls the object's .encode() method and returns a python string version """ log_debug( pycls) ptr = pycls.encode() if not ptr: return plen = yobotproto.yobot_protoclient_getsegsize(ptr) if not plen or plen > yobotproto.YOBOT_MAX_COMMSIZE: return None #error return yobotproto.cdata(ptr,plen)
def __init__(self, msg): """takes a raw string and converts it to a python YobotSegment.""" for a in ("cmd", "msg", "evt", "acct", "base"): setattr(self, a, None) #something will 'stamp' our segment with the CID on which it was received self.cid = 0 self.raw = msg decoded_segment = self._decode_segment(msg) self.commflags = decoded_segment.comm.flags self.reference = decoded_segment.comm.reference self.struct_type = decoded_segment.struct_type if self.struct_type in (yobotproto.YOBOT_PROTOCLIENT_YCMDI, yobotproto.YOBOT_PROTOCLIENT_YMSGI, yobotproto.YOBOT_PROTOCLIENT_YMKACCTI): self.cmd = yobotclass.YobotCommand(decoded_segment.cmdi) if self.struct_type == yobotproto.YOBOT_PROTOCLIENT_YMKACCTI: self.acct = yobotclass.YobotAccount(decoded_segment.mkaccti) self.base = self.acct elif self.struct_type == yobotproto.YOBOT_PROTOCLIENT_YMSGI: self.msg = yobotclass.YobotMessage(decoded_segment.msgi) self.msg.acctid = self.cmd.acctid self.base = self.msg elif self.struct_type == yobotproto.YOBOT_PROTOCLIENT_YEVTI: self.evt = yobotclass.YobotEvent(decoded_segment.evi) self.base = self.evt #some extra hack... if self.commflags & yobotproto.YOBOT_DATA_IS_BINARY: result = yobotproto.cdata(decoded_segment.rawdata,decoded_segment.evi.evt.len) self.evt.txt = result elif self.struct_type == yobotproto.YOBOT_PROTOCLIENT_YCMDI: self.base = self.cmd else: raise UnknownSegment, "Segment type %s is not supported" % self.struct_type self.base.commflags = self.commflags self.base.reference = self.reference