message.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from python_qt_binding.QtCore import QDateTime, QObject
00034 
00035 
00036 class Message(QObject):
00037     """
00038     Basic Message object.To directly access members use the get_data() function.
00039     """
00040     def __init__(self, msg=None):
00041         """
00042         :param msg: a log message to initialize the message object, ''Log''
00043         """
00044         super(Message, self).__init__()
00045         self._messagemembers = self.get_message_members()
00046         self._severity = {1: self.tr('Debug'), 2: self.tr('Info'), 4: self.tr('Warn'), 8: self.tr('Error'), 16: self.tr('Fatal')}
00047         if msg is not None:
00048             self._message = msg.msg
00049             self._severity = self._severity[msg.level]
00050             self._node = msg.name
00051             self._time = (msg.header.stamp.secs, msg.header.stamp.nsecs)
00052             self._topics = ', '.join(msg.topics)
00053             self._location = msg.file + ':' + msg.function + ':' + str(msg.line)
00054 
00055     @staticmethod
00056     def get_message_members():
00057         return ('_message', '_severity', '_node', '_time', '_topics', '_location')
00058 
00059     @staticmethod
00060     def header_print():
00061         members = Message.get_message_members()
00062         text = members[2][1:].capitalize() + ';'
00063         text += members[3][1:].capitalize() + ';'
00064         text += members[1][1:].capitalize() + ';'
00065         text += members[4][1:].capitalize() + ';'
00066         text += members[5][1:].capitalize() + ';'
00067         text += members[0][1:].capitalize() + '\n'
00068         return text
00069 
00070     def count(self):
00071         return len(self._messagemembers)
00072 
00073     def time_in_seconds(self):
00074         """
00075         :returns: seconds with decimal fractions of a second to the 9th decimal place, ''str''
00076         """
00077         return str(self._time[0]) + '.' + str(self._time[1]).zfill(9)
00078 
00079     def time_as_qdatetime(self):
00080         """
00081         :returns: time with msecs included, ''QDateTime''
00082         """
00083         time = QDateTime()
00084         time.setTime_t(int(self._time[0]))
00085         time = time.addMSecs(int(str(self._time[1]).zfill(9)[:3]))
00086         return time
00087 
00088     def load_from_array(self, rowdata):
00089         """
00090         :param rowdata:
00091             [0] = message, ''str''
00092             [1] = severity, ''str''
00093             [2] = node name, ''str''
00094             [3] = time in seconds including decimal, ''str''
00095             [4] = topic name, ''str''
00096             [5] = location value, ''str''
00097         """
00098         self._message = rowdata[0]
00099         self._severity = rowdata[1]
00100         self._node = rowdata[2]
00101         self._time = rowdata[3].split('.')
00102         self._topics = rowdata[4]
00103         self._location = rowdata[5]
00104         return self
00105 
00106     def file_load(self, text):
00107         """
00108         :param text: delmited message text as follows, node;time;severity;topics;location;"message" , ''str''
00109         """
00110         text = text[1:]
00111         sc_index = text.find('";"')
00112         if sc_index == -1:
00113             raise ValueError('File format is incorrect, missing ";" marker')
00114         self._node = text[:sc_index]
00115         text = text[text.find('";"') + 3:]
00116         sc_index = text.find('";"')
00117         if sc_index == -1:
00118             raise ValueError('File format is incorrect, missing ";" marker')
00119         sec, nsec = text[:sc_index].split('.')
00120         self._time = (sec, nsec)
00121         text = text[text.find('";"') + 3:]
00122         sc_index = text.find('";"')
00123         if sc_index == -1:
00124             raise ValueError('File format is incorrect, missing ";" marker')
00125         self._severity = text[:sc_index]
00126         text = text[text.find('";"') + 3:]
00127         sc_index = text.find('";"')
00128         if sc_index == -1:
00129             raise ValueError('File format is incorrect, missing ";" marker')
00130         self._topics = text[:sc_index]
00131         text = text[text.find('";"') + 3:]
00132         sc_index = text.find('";"')
00133         if sc_index == -1:
00134             raise ValueError('File format is incorrect, missing ";" marker')
00135         self._location = text[:sc_index]
00136         text = text[sc_index + 2:]
00137         text = text.replace('\\"', '"')
00138         self._message = text[1:-2]
00139         return
00140 
00141     def file_print(self):
00142         text = '"' + self._node + '";'
00143         text += '"' + self.time_in_seconds() + '";'
00144         text += '"' + self._severity + '";'
00145         text += '"' + self._topics + '";'
00146         text += '"' + self._location + '";'
00147         altered_message = self._message.replace('"', '\\"')
00148         text += '"' + altered_message + '"\n'
00149         return text
00150 
00151     def pretty_print(self):
00152         text = self.tr('Node: ') + self._node + '\n'
00153         text += self.tr('Time: ') + self.time_in_seconds() + '\n'
00154         text += self.tr('Severity: ') + self._severity + '\n'
00155         text += self.tr('Published Topics: ') + self._topics + '\n'
00156         text += '\n' + self._message + '\n\n'
00157         text += '-' * 100 + '\n\n'
00158 
00159         return text
00160 
00161     def get_data(self, col):
00162         return getattr(self, Message.get_message_members()[col])


rqt_console
Author(s): Aaron Blasdel
autogenerated on Fri Jan 3 2014 11:54:30