-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosmsg.py
100 lines (79 loc) · 1.86 KB
/
rosmsg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
#
import sys
import os
import re
MSG_EXT=".msg"
primitive_type=["bool", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "float32", "float64", "string", "time", "duration"]
header_type=["Header"]
MSG_PATH=["", "../common_msgs-jade-devel/"]
#
#
#
def parse_msg_type(typename):
types = typename.split("/")
if len(types) == 1:
return load_msg_file(typename)
return load_msg_file(types[1], types[0])
#
#
#
def load_msg_file(typename, pkg_name="", ext=MSG_EXT):
try:
filename = find_msg_file(typename, pkg_name)
if not filename:
print "ERROR: no such type.."
return None
f = open(filename, "r")
lines = f.readlines()
f.close()
data={}
for line in lines:
ln = line.split("#", 2)
if len(ln) == 1 or (len(ln) == 2 and ln[0]):
ll = ln[0].strip()
if not ll: continue
typ, name = ll.split()
typ = check_datatype(typ.strip(), pkg_name)
data[name.strip()] = typ
return data
except:
print "ERROR in load_msg_file (%s %s)" % (typename, pkg_name)
return None
#
#
#
def check_datatype(typ, pkg_name="", ext=MSG_EXT):
try:
if typ in primitive_type or typ in header_type:
return typ
for p in primitive_type:
m = re.search(p+"\[\d*\]", typ)
if m : return typ
data = load_msg_file(typ, pkg_name)
return data
except:
print "ERROR in parse_datatype"
return None
#
#
def find_msg_file(typename, pkg_name="", ext=MSG_EXT):
try:
path=""
if pkg_name:
path += pkg_name+"/msg/"
for p in MSG_PATH:
filename = p+path+typename+ext
if os.path.exists(filename) :
return filename
return None
except:
print "ERROR in parse_datatype"
return None
#
# Message Class
#
class ROS_Message:
def __init__(self, name):
self.name = name
self.msg_type = parse_msg_type(name)