def _append_message(self, msg, latched): ''' Adds a label to the dialog's layout and shows the given text. @param msg: the text to add to the dialog @type msg: message object ''' current_time = time.time() self._count_messages(current_time) # skip messages, if they are received often then MESSAGE_HZ_LIMIT if self._last_received_ts != 0 and self.receiving_hz != 0: if not latched and current_time - self._last_received_ts < 1.0 / self.receiving_hz: self._scrapped_msgs += 1 self._scrapped_msgs_sl += 1 return self._last_received_ts = current_time if not self.show_only_rate: # convert message to string and reduce line width to current limit msg = message.strify_message(msg, field_filter=self.field_filter_fn) if isinstance(msg, tuple): msg = msg[0] msg = self._trim_width(msg) # create a notification about scrapped messages if self._scrapped_msgs_sl > 0: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">scrapped %s message because of Hz-settings</pre>'%self._scrapped_msgs_sl self.display.append(txt) self._scrapped_msgs_sl = 0 txt = '<pre style="background-color:#FFFCCC; font-family:Fixedsys,Courier; padding:10px;">---------- %s --------------------\n%s</pre>'%(datetime.now().strftime("%d.%m.%Y %H:%M:%S.%f"), msg) # set the count of the displayed messages on receiving the first message self._update_max_msg_count(txt) self.display.append(txt) self._print_status()
def _append_message(self, msg, latched): ''' Adds a label to the dialog's layout and shows the given text. @param msg: the text to add to the dialog @type msg: message object ''' current_time = time.time() self._count_messages(current_time) # skip messages, if they are received often then MESSAGE_HZ_LIMIT if self._last_received_ts != 0 and self.receiving_hz != 0: if not latched and current_time - self._last_received_ts < 1.0 / self.receiving_hz: self._scrapped_msgs += 1 self._scrapped_msgs_sl += 1 return self._last_received_ts = current_time if not self.show_only_rate: # convert message to string and reduce line width to current limit msg = message.strify_message(msg, field_filter=self.field_filter_fn) if isinstance(msg, tuple): msg = msg[0] msg = self._trim_width(msg) msg = msg.replace('<', '<').replace('>', '>') # create a notification about scrapped messages if self._scrapped_msgs_sl > 0: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">scrapped %s message because of Hz-settings</pre>' % self._scrapped_msgs_sl self.display.append(txt) self._scrapped_msgs_sl = 0 txt = '<pre style="background-color:#FFFCCC; font-family:Fixedsys,Courier; padding:10px;">---------- %s --------------------\n%s</pre>' % ( datetime.now().strftime("%d.%m.%Y %H:%M:%S.%f"), msg) # set the count of the displayed messages on receiving the first message self._update_max_msg_count(txt) self.display.append(txt) self._print_status()
def roundtrip(m): yaml_text = strify_message(m) print yaml_text loaded = yaml.load(yaml_text) print "loaded", loaded new_inst = m.__class__() if loaded is not None: fill_message_args(new_inst, [loaded]) else: fill_message_args(new_inst, []) return new_inst
def _append_message(self, msg, latched): ''' Adds a label to the dialog's layout and shows the given text. @param msg: the text to add to the dialog @type msg: message object ''' current_time = time.time() with self.lock: # time reset if self.msg_t0 < 0 or self.msg_t0 > current_time: self.msg_t0 = current_time self.msg_tn = current_time self.times = [] else: self.times.append(current_time - self.msg_tn) self.msg_tn = current_time # keep only statistics for the last 5000 messages so as not to run out of memory if len(self.times) > self.STATISTIC_QUEUE_LEN: self.times.pop(0) self.message_count += 1 # skip messages, if they are received often then MESSAGE_HZ_LIMIT if self._last_received_ts != 0 and self.receiving_hz != 0: if not latched and current_time - self._last_received_ts < 1.0 / self.receiving_hz: self._scrapped_msgs += 1 self._scrapped_msgs_sl += 1 return self._last_received_ts = current_time if not self.show_only_rate: # convert message to string and reduce line width to current limit msg = message.strify_message(msg, field_filter=self.field_filter_fn) if isinstance(msg, tuple): msg = msg[0] if self.line_limit != 0: a = '' for l in msg.splitlines(): a = a + (l if len(l) <= self.line_limit else l[0:self.line_limit - 3] + '...') + '\n' msg = a # create a notification about scrapped messages if self._scrapped_msgs_sl > 0: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">scrapped %s message because of Hz-settings</pre>' % self._scrapped_msgs_sl self.display.append(txt) self._scrapped_msgs_sl = 0 txt = '<pre style="background-color:#FFFCCC; font-family:Fixedsys,Courier; padding:10px;">---------- %s --------------------\n%s</pre>' % ( datetime.now().strftime("%d.%m.%Y %H:%M:%S.%f"), msg) # set the count of the displayed messages on receiving the first message if self._blocks_in_msg is None: td = QtGui.QTextDocument(txt) self._blocks_in_msg = td.blockCount() self.display.document().setMaximumBlockCount( self._blocks_in_msg * self.max_displayed_msgs) self.display.append(txt) self._print_status()
def __generate_output(self, msg): d = strify_message(msg) # truncate message lines = d.splitlines() if len(lines) > self.max_lines: rospy.loginfo( 'output truncated, too long (shown %d of %d lines only).' % (self.max_lines, len(lines))) d = '\n'.join(lines[0:self.max_lines]) d += ( '\n\n[%s]' % '*** output truncated, too long ' '(showing %d of %d lines only). ***' % (self.max_lines, len(lines)) ) return d
def print_ros_msg(msg): s = strify_message(msg, indent=' ') print(s, file=sys.stderr) return s
def test_strify_message(self): # strify message is part of roslib, but we want to test with # rostopic's field filters. It's also the case that # roslib.messages cannot be unit tested within the ROS stack # -- part of the reason it needs to be moved elsewhere. from std_msgs.msg import String, Int32, Header from test_rostopic.msg import Simple, TVals, Floats, Arrays, Embed from roslib.rostime import Time, Duration from roslib.message import strify_message from rostopic import create_field_filter simple_v = Simple(1, -2, 3, -4, 'a', 7, 8, 9, 'bar') simple_d = { 'b': 1, 'int16': -2, 'int32': 3, 'int64': -4, 'c': 'a', 'uint16': 7, 'uint32': 8, 'uint64': 9, 'str': 'bar' } simple_nostr = simple_d.copy() del simple_nostr['str'] arrays_v = Arrays([-1], chr(2) + chr(3), [3, 4, 5], [6, 7, 8], ['a1', 'b2', 'b3'], [Time(123, 456), Time(78, 90)]) arrays_d = { 'int8_arr': [-1], 'uint8_arr': [2, 3], 'int32_arr': [3, 4, 5], 'uint32_arr': [6, 7, 8], 'string_arr': ['a1', 'b2', 'b3'], 'time_arr': [{ 'secs': 123, 'nsecs': 456 }, { 'secs': 78, 'nsecs': 90 }] } arrays_nostr = arrays_d.copy() del arrays_nostr['string_arr'] embed_v = Embed(simple_v, arrays_v) embed_d = {'simple': simple_d, 'arrays': arrays_d} f = create_field_filter(echo_nostr=False, echo_noarr=False) m = String() self.assertEquals("data: ''", strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('data: foo', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals( { 't': { 'secs': 123, 'nsecs': 456 }, 'd': { 'secs': 78, 'nsecs': 90 } }, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_d, v) m = arrays_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(arrays_d, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(embed_d, v) f = create_field_filter(echo_nostr=True, echo_noarr=False) m = String() self.assertEquals('', strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals( { 't': { 'secs': 123, 'nsecs': 456 }, 'd': { 'secs': 78, 'nsecs': 90 } }, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_nostr, v) m = arrays_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(arrays_nostr, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'simple': simple_nostr, 'arrays': arrays_nostr}, v) f = create_field_filter(echo_nostr=False, echo_noarr=True) m = String() self.assertEquals("data: ''", strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('data: foo', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals( { 't': { 'secs': 123, 'nsecs': 456 }, 'd': { 'secs': 78, 'nsecs': 90 } }, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_d, v) m = arrays_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(None, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'simple': simple_d, 'arrays': None}, v) f = create_field_filter(echo_nostr=True, echo_noarr=True) m = String() self.assertEquals('', strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals( { 't': { 'secs': 123, 'nsecs': 456 }, 'd': { 'secs': 78, 'nsecs': 90 } }, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_nostr, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'simple': simple_nostr, 'arrays': None}, v)
def _append_message(self, msg, latched, current_time=None, store=True): ''' Adds a label to the dialog's layout and shows the given text. @param msg: the text to add to the dialog @type msg: message object ''' if current_time is None: current_time = time.time() self._latched = latched if store: self._msgs.append((msg, current_time)) if len(self._msgs) > 25: self._msgs.pop() msg_len = -1 if (self.SHOW_BYTES or self.show_only_rate): buff = None try: from cStringIO import StringIO # Python 2.x buff = StringIO() import os msg.serialize(buff) buff.seek(0, os.SEEK_END) msg_len = buff.tell() except ImportError: from io import BytesIO # Python 3.x buff = BytesIO() msg.serialize(buff) msg_len = buff.getbuffer().nbytes if store: self._count_messages(current_time, msg_len) # skip messages, if they are received often then MESSAGE_HZ_LIMIT if self._last_received_ts != 0 and self.receiving_hz != 0: if current_time - self._last_received_ts < 1.0 / self.receiving_hz: if (not latched or (latched and current_time - self._start_time > 3.0)): self._scrapped_msgs += 1 self._scrapped_msgs_sl += 1 return self._last_received_ts = current_time if not self.show_only_rate: # convert message to string and reduce line width to current limit msg = message.strify_message(msg, field_filter=self.field_filter_fn) if isinstance(msg, tuple): msg = msg[0] msg = self._trim_width(msg) msg = msg.replace('<', '<').replace('>', '>') msg_cated = False if self.chars_limit != 0 and len(msg) > self.chars_limit: msg = msg[0:self.chars_limit] msg_cated = True # create a notification about scrapped messages if self._scrapped_msgs_sl > 0: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">scrapped %s message because of Hz-settings</pre>' % self._scrapped_msgs_sl self.display.append(txt) self._scrapped_msgs_sl = 0 txt = '<pre style="background-color:#FFFCCC; color:#000000;font-family:Fixedsys,Courier; padding:10px;">---------- %s --------------------\n%s</pre>' % ( datetime.now().strftime("%d.%m.%Y %H:%M:%S.%f"), msg) # set the count of the displayed messages on receiving the first message self._update_max_msg_count(txt) self.display.append(txt) if msg_cated: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">message has been cut off</pre>' self.display.append(txt) self._print_status()
def test_strify_message(self): # this is a bit overtuned, but it will catch regressions from roslib.message import Message, strify_message class M1(Message): __slots__ = [] _slot_types = [] def __init__(self): pass self.assertEquals('', strify_message(M1())) class M2(Message): __slots__ = ['str', 'int', 'float', 'bool', 'list'] _slot_types = ['string', 'int32', 'float32', 'bool', 'int32[]'] def __init__(self, str_, int_, float_, bool_, list_): self.str = str_ self.int = int_ self.float = float_ self.bool = bool_ self.list = list_ self.assertEquals("""str: string int: 123456789101112 float: 5678.0 bool: True list: [1, 2, 3]""", strify_message(M2('string', 123456789101112, 5678., True, [1,2,3]))) self.assertEquals("""str: '' int: -1 float: 0.0 bool: False list: []""", strify_message(M2('', -1, 0., False, []))) class M3(Message): __slots__ = ['m2'] _slot_types=['M1'] def __init__(self, m2): self.m2 = m2 self.assertEquals("""m2: str: string int: -1 float: 0.0 bool: False list: []""", strify_message(M3(M2('string', -1, 0., False, [])))) # test array of Messages field class M4(Message): __slots__ = ['m2s'] _slot_types=['M2[]'] def __init__(self, m2s): self.m2s = m2s self.assertEquals("""m2s: - str: string int: 1234 float: 5678.0 bool: True list: [1, 2, 3] - str: string int: -1 float: 0.0 bool: False list: []""", strify_message(M4([ M2('string', 1234, 5678., True, [1,2,3]), M2('string', -1, 0., False, []), ]))) # test Time and Duration from roslib.rostime import Time, Duration class M5(Message): __slots__ = ['t', 'd'] _slot_types=['time', 'duration'] def __init__(self, t, d): self.t = t self.d = d self.assertEquals("""t: secs: 987 nsecs: 654 d: secs: 123 nsecs: 456""", strify_message(M5(Time(987, 654), Duration(123, 456)))) # test final clause of strify -- str anything that isn't recognized self.assertEquals("set([1])", strify_message(set([1])))
def test_strify_message(self): # this is a bit overtuned, but it will catch regressions from roslib.message import Message, strify_message class M1(Message): __slots__ = [] _slot_types = [] def __init__(self): pass self.assertEquals('', strify_message(M1())) class M2(Message): __slots__ = ['str', 'int', 'float', 'bool', 'list'] _slot_types = ['string', 'int32', 'float32', 'bool', 'int32[]'] def __init__(self, str_, int_, float_, bool_, list_): self.str = str_ self.int = int_ self.float = float_ self.bool = bool_ self.list = list_ self.assertEquals( """str: string int: 123456789101112 float: 5678.0 bool: True list: [1, 2, 3]""", strify_message( M2('string', 123456789101112, 5678., True, [1, 2, 3]))) self.assertEquals( """str: '' int: -1 float: 0.0 bool: False list: []""", strify_message(M2('', -1, 0., False, []))) class M3(Message): __slots__ = ['m2'] _slot_types = ['M1'] def __init__(self, m2): self.m2 = m2 self.assertEquals( """m2: str: string int: -1 float: 0.0 bool: False list: []""", strify_message(M3(M2('string', -1, 0., False, [])))) # test array of Messages field class M4(Message): __slots__ = ['m2s'] _slot_types = ['M2[]'] def __init__(self, m2s): self.m2s = m2s self.assertEquals( """m2s: - str: string int: 1234 float: 5678.0 bool: True list: [1, 2, 3] - str: string int: -1 float: 0.0 bool: False list: []""", strify_message( M4([ M2('string', 1234, 5678., True, [1, 2, 3]), M2('string', -1, 0., False, []), ]))) # test Time and Duration from roslib.rostime import Time, Duration class M5(Message): __slots__ = ['t', 'd'] _slot_types = ['time', 'duration'] def __init__(self, t, d): self.t = t self.d = d self.assertEquals( """t: secs: 987 nsecs: 654 d: secs: 123 nsecs: 456""", strify_message(M5(Time(987, 654), Duration(123, 456)))) # test final clause of strify -- str anything that isn't recognized self.assertEquals("set([1])", strify_message(set([1])))
def test_strify_message(self): # strify message is part of roslib, but we want to test with # rostopic's field filters. It's also the case that # roslib.messages cannot be unit tested within the ROS stack # -- part of the reason it needs to be moved elsewhere. from std_msgs.msg import String from test_rostopic.msg import Arrays, Embed, Simple, TVals from genpy import Time, Duration from roslib.message import strify_message from rostopic import create_field_filter simple_v = Simple(1, -2, 3, -4, 'a', 7, 8, 9, 'bar') simple_d = {'b': 1, 'int16': -2, 'int32': 3, 'int64': -4, 'c': 'a', 'uint16': 7, 'uint32': 8, 'uint64': 9, 'str': 'bar'} simple_nostr = simple_d.copy() del simple_nostr['str'] arrays_v = Arrays([-1], chr(2)+chr(3), [3, 4, 5], [6, 7, 8], ['a1', 'b2', 'b3'], [Time(123, 456), Time(78, 90)]) arrays_d = {'int8_arr': [-1], 'uint8_arr': [2, 3], 'int32_arr': [3, 4, 5], 'uint32_arr': [6, 7, 8], 'string_arr': ['a1', 'b2', 'b3'], 'time_arr': [{'secs': 123, 'nsecs': 456}, {'secs': 78, 'nsecs': 90}]} arrays_nostr = arrays_d.copy() del arrays_nostr['string_arr'] embed_v = Embed(simple_v, arrays_v) embed_d = {'simple': simple_d, 'arrays': arrays_d} f = create_field_filter(echo_nostr=False, echo_noarr=False) m = String() self.assertEquals("data: ''", strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('data: foo', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'t': {'secs': 123, 'nsecs': 456}, 'd': {'secs': 78, 'nsecs': 90}}, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_d, v) m = arrays_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(arrays_d, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(embed_d, v) f = create_field_filter(echo_nostr=True, echo_noarr=False) m = String() self.assertEquals('', strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'t': {'secs': 123, 'nsecs': 456}, 'd': {'secs': 78, 'nsecs': 90}}, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_nostr, v) m = arrays_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(arrays_nostr, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'simple': simple_nostr, 'arrays': arrays_nostr}, v) f = create_field_filter(echo_nostr=False, echo_noarr=True) m = String() self.assertEquals("data: ''", strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('data: foo', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'t': {'secs': 123, 'nsecs': 456}, 'd': {'secs': 78, 'nsecs': 90}}, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_d, v) m = arrays_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(None, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'simple': simple_d, 'arrays': None}, v) f = create_field_filter(echo_nostr=True, echo_noarr=True) m = String() self.assertEquals('', strify_message(m, field_filter=f)) m = String('foo') self.assertEquals('', strify_message(m, field_filter=f)) m = TVals(Time(123, 456), Duration(78, 90)) v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'t': {'secs': 123, 'nsecs': 456}, 'd': {'secs': 78, 'nsecs': 90}}, v) m = simple_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals(simple_nostr, v) m = embed_v v = yaml.load(strify_message(m, field_filter=f)) self.assertEquals({'simple': simple_nostr, 'arrays': None}, v)