CassandraTopic_ros_old.py
Go to the documentation of this file.
00001 from CassandraTopic_ import *
00002 
00003 import std_msgs.msg
00004 import rosmsg
00005 
00006 class CassandraTopic_ros_old(CassandraTopic_):
00007     def __init__(self):        
00008         CassandraTopic_.__init__(MsgClass)
00009         
00010     def encode(self, msg):
00011         return self.ros_to_cassandra(msg)
00012     
00013     def decode(self, data):
00014         msg = self.MsgClass()
00015         _dict = self.cassandra_to_dict(data)
00016         msg = self.dict_to_ros(_dict, msg)
00017         return msg
00018     
00019     def value_to_cassandra(self ,data, parent):
00020 
00021         if type(data) == str:
00022             return { parent : data }
00023         elif type(data) in (int, float, long):
00024             return { parent : str(data) }
00025         elif type(data) == bool:
00026             return self.value_to_cassandra(int(data))
00027         elif isinstance(data, (genpy.rostime.Time, genpy.rostime.Duration)):
00028             return { parent+'.secs':str(data.secs), parent+'.nsecs':str(data.nsecs)}
00029         elif type(data) in (list, tuple):
00030             _dict = {}
00031             for i in range(len(data)):
00032                 res = self.value_to_cassandra(data[i], parent+"["+str(i)+"]")
00033                 for key in res.keys():
00034                     _dict[key] = res[key]
00035             return _dict
00036         elif isinstance(data, rospy.Message):
00037             return self.ros_to_cassandra(data, parent)
00038         else:
00039             raise RosCassandraTranspileException("unknown type: %s"%type(data)) 
00040     
00041     ################################################################################################################    
00042     def ros_to_cassandra(self, data, parent=""):
00043         if not isinstance(data, rospy.Message):
00044             #raise RosCassandraTranspileException("not a valid rospy Message instance: %s"%data.__class__.__name__)
00045             return self.value_to_cassandra(data, parent)
00046     
00047         _dict = {}
00048     
00049         if len(parent) > 0:
00050             parent += "."        
00051         
00052         for elem in data.__slots__:
00053             sub_elements = self.value_to_cassandra( getattr(data, elem) , parent+elem)
00054             
00055             if type(sub_elements) == dict:
00056                 for key in sub_elements.keys():
00057                     _dict[key] = sub_elements[key]
00058                 
00059         return _dict
00060     
00061     ################################################################################################################
00062     
00063     def value_to_dict(self, name, value, _dict):
00064         
00065         # treat as array
00066         if name[0][-1] == ']':
00067             ar = name[0].find('[')
00068             element = int(name[0][ar+1:-1])
00069             name[0] = name[0][:ar]
00070             
00071             # it is already an array
00072             if _dict.has_key(name[0]):
00073                 if len(_dict[name[0]]) == element:
00074                     _dict[name[0]].append(self.value_to_dict(name[1:], value, {}))
00075                 else:
00076                     _dict[name[0]][element] = self.value_to_dict(name[1:], value, _dict[name[0]][element])
00077             else:
00078                 _dict[name[0]] = [self.value_to_dict(name[1:], value, {})]
00079     
00080         elif len(name) == 1:
00081             _dict[name[0]] = value
00082             
00083         else:
00084             if not _dict.has_key(name[0]):
00085                 _dict[name[0]] = self.value_to_dict(name[1:], value, {})
00086             else:
00087                 _dict[name[0]] = self.value_to_dict(name[1:], value, _dict[name[0]])
00088                  
00089         return _dict
00090         
00091     ################################################################################################################
00092     def cassandra_to_dict(self, data):
00093         _dict = {}
00094         for key in data.keys():
00095             name = key.split(".")
00096             _dict = self.value_to_dict(name, data[key], _dict)
00097         return _dict
00098     
00099     
00100     ################################################################################################################
00101     def dict_to_value(self, _dict, _slot, _type):
00102         
00103         if isinstance(_slot, str):
00104             return _dict
00105         elif isinstance(_slot, int):
00106             return int(_dict[_slot])
00107         elif isinstance(_slot, float):
00108             return float(_dict)
00109         elif isinstance(_slot, bool):
00110             return bool(_dict)
00111         elif isinstance(_slot, (list, dict, tuple)):
00112             msg = []
00113             _type = _type.replace("[]","")
00114             _slot = genpy.message.get_message_class(_type)()
00115             for elem in _dict:
00116                 msg.append( self.dict_to_ros( elem, _slot) )
00117             return msg
00118         elif isinstance(_slot, (genpy.rostime.Time, genpy.rostime.Duration)):
00119             t = rospy.Time()
00120             t.set(int(_dict['secs']), int(_dict['nsecs']))
00121             return t
00122         elif isinstance(_slot, rospy.Message):
00123             return self.dict_to_ros(_dict, _slot)
00124         else:
00125             raise RosCassandraTranspileException("unknown type: %s"%type(_slot)) 
00126         
00127     def dict_to_ros(self, _dict, msg):
00128         
00129         for i in range(len(msg.__slots__)):
00130             _slot = msg.__slots__[i]
00131             _type = msg._slot_types[i]
00132             
00133             setattr(msg, 
00134                     _slot,
00135                     self.dict_to_value( _dict[_slot],
00136                                         getattr(msg, _slot),
00137                                         _type))
00138     
00139         return msg


cassandra_ros
Author(s): André Dietrich, Sebastian Zug
autogenerated on Mon Oct 6 2014 10:13:55