parent
96cd8406ce
commit
ae1906fd09
|
@ -32,13 +32,20 @@
|
||||||
#
|
#
|
||||||
# Revision $Id$
|
# Revision $Id$
|
||||||
# $Author$
|
# $Author$
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ROS msg library for Python
|
ROS msg library for Python
|
||||||
|
|
||||||
Implements: U{http://ros.org/wiki/msg}
|
Implements: U{http://ros.org/wiki/msg}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import cStringIO
|
try:
|
||||||
|
from cStringIO import StringIO # Python 2.x
|
||||||
|
except ImportError:
|
||||||
|
from io import StringIO # Python 3.x
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import itertools
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
|
@ -234,7 +241,7 @@ def _strify_spec(spec, buff=None, indent=''):
|
||||||
@rtype: str
|
@rtype: str
|
||||||
"""
|
"""
|
||||||
if buff is None:
|
if buff is None:
|
||||||
buff = cStringIO.StringIO()
|
buff = StringIO()
|
||||||
for c in spec.constants:
|
for c in spec.constants:
|
||||||
buff.write("%s%s %s=%s\n"%(indent, c.type, c.name, c.val_text))
|
buff.write("%s%s %s=%s\n"%(indent, c.type, c.name, c.val_text))
|
||||||
for type_, name in zip(spec.types, spec.names):
|
for type_, name in zip(spec.types, spec.names):
|
||||||
|
@ -310,7 +317,7 @@ class MsgSpec(object):
|
||||||
@return: zip list of types and names (e.g. [('int32', 'x'), ('int32', 'y')]
|
@return: zip list of types and names (e.g. [('int32', 'x'), ('int32', 'y')]
|
||||||
@rtype: [(str,str),]
|
@rtype: [(str,str),]
|
||||||
"""
|
"""
|
||||||
return zip(self.types, self.names)
|
return list(zip(self.types, self.names)) #py3k
|
||||||
|
|
||||||
def parsed_fields(self):
|
def parsed_fields(self):
|
||||||
"""
|
"""
|
||||||
|
@ -373,7 +380,7 @@ def _init():
|
||||||
|
|
||||||
header = os.path.join(std_msgs_dir, roslib.packages.MSG_DIR, fname)
|
header = os.path.join(std_msgs_dir, roslib.packages.MSG_DIR, fname)
|
||||||
if not os.path.isfile(header):
|
if not os.path.isfile(header):
|
||||||
print >> sys.stderr, "ERROR: cannot locate %s. Expected to find it at '%s'"%(fname, header)
|
sys.stderr.write("ERROR: cannot locate %s. Expected to find it at '%s'\n"%(fname, header))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# register Header under both contexted and de-contexted name
|
# register Header under both contexted and de-contexted name
|
||||||
|
@ -382,7 +389,7 @@ def _init():
|
||||||
register('std_msgs/'+HEADER, spec)
|
register('std_msgs/'+HEADER, spec)
|
||||||
# backwards compat, REP 100
|
# backwards compat, REP 100
|
||||||
register('roslib/'+HEADER, spec)
|
register('roslib/'+HEADER, spec)
|
||||||
for k, spec in EXTENDED_BUILTINS.iteritems():
|
for k, spec in EXTENDED_BUILTINS.items():
|
||||||
register(k, spec)
|
register(k, spec)
|
||||||
|
|
||||||
_initialized = True
|
_initialized = True
|
||||||
|
@ -440,9 +447,9 @@ def get_pkg_msg_specs(package):
|
||||||
try:
|
try:
|
||||||
typespec = load_from_file(msg_file(package, t), package)
|
typespec = load_from_file(msg_file(package, t), package)
|
||||||
specs.append(typespec)
|
specs.append(typespec)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
failures.append(t)
|
failures.append(t)
|
||||||
print "ERROR: unable to load %s"%t
|
print("ERROR: unable to load %s"%t)
|
||||||
return specs, failures
|
return specs, failures
|
||||||
|
|
||||||
def load_package_dependencies(package, load_recursive=False):
|
def load_package_dependencies(package, load_recursive=False):
|
||||||
|
@ -457,7 +464,7 @@ def load_package_dependencies(package, load_recursive=False):
|
||||||
global _loaded_packages
|
global _loaded_packages
|
||||||
_init()
|
_init()
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print "Load dependencies for package", package
|
print("Load dependencies for package", package)
|
||||||
|
|
||||||
if not load_recursive:
|
if not load_recursive:
|
||||||
manifest_file = roslib.manifest.manifest_file(package, True)
|
manifest_file = roslib.manifest.manifest_file(package, True)
|
||||||
|
@ -470,7 +477,7 @@ def load_package_dependencies(package, load_recursive=False):
|
||||||
failures = []
|
failures = []
|
||||||
for d in depends:
|
for d in depends:
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print "Load dependency", d
|
print("Load dependency", d)
|
||||||
#check if already loaded
|
#check if already loaded
|
||||||
# - we are dependent on manifest.getAll returning first-order dependencies first
|
# - we are dependent on manifest.getAll returning first-order dependencies first
|
||||||
if d in _loaded_packages or d == package:
|
if d in _loaded_packages or d == package:
|
||||||
|
@ -495,19 +502,19 @@ def load_package(package):
|
||||||
global _loaded_packages
|
global _loaded_packages
|
||||||
_init()
|
_init()
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print "Load package", package
|
print("Load package", package)
|
||||||
|
|
||||||
#check if already loaded
|
#check if already loaded
|
||||||
# - we are dependent on manifest.getAll returning first-order dependencies first
|
# - we are dependent on manifest.getAll returning first-order dependencies first
|
||||||
if package in _loaded_packages:
|
if package in _loaded_packages:
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print "Package %s is already loaded"%package
|
print("Package %s is already loaded"%package)
|
||||||
return
|
return
|
||||||
|
|
||||||
_loaded_packages.append(package)
|
_loaded_packages.append(package)
|
||||||
specs, failed = get_pkg_msg_specs(package)
|
specs, failed = get_pkg_msg_specs(package)
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print "Package contains the following messages: %s"%specs
|
print("Package contains the following messages: %s"%specs)
|
||||||
for key, spec in specs:
|
for key, spec in specs:
|
||||||
#register spec under both local and fully-qualified key
|
#register spec under both local and fully-qualified key
|
||||||
register(key, spec)
|
register(key, spec)
|
||||||
|
@ -588,7 +595,7 @@ def load_from_string(text, package_context='', full_name='', short_name=''):
|
||||||
l = orig_line.split(COMMENTCHAR)[0].strip() #strip comments
|
l = orig_line.split(COMMENTCHAR)[0].strip() #strip comments
|
||||||
if not l:
|
if not l:
|
||||||
continue #ignore empty lines
|
continue #ignore empty lines
|
||||||
splits = filter(lambda s: s, [x.strip() for x in l.split(" ")]) #split type/name, filter out empties
|
splits = [s for s in [x.strip() for x in l.split(" ")] if s] #split type/name, filter out empties
|
||||||
type_ = splits[0]
|
type_ = splits[0]
|
||||||
if not is_valid_msg_type(type_):
|
if not is_valid_msg_type(type_):
|
||||||
raise MsgSpecException("%s is not a legal message type"%type_)
|
raise MsgSpecException("%s is not a legal message type"%type_)
|
||||||
|
@ -608,7 +615,7 @@ def load_from_string(text, package_context='', full_name='', short_name=''):
|
||||||
val = splits[1]
|
val = splits[1]
|
||||||
try:
|
try:
|
||||||
val_converted = _convert_val(type_, val)
|
val_converted = _convert_val(type_, val)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
raise MsgSpecException("Invalid declaration: %s"%e)
|
raise MsgSpecException("Invalid declaration: %s"%e)
|
||||||
constants.append(Constant(type_, name, val_converted, val.strip()))
|
constants.append(Constant(type_, name, val_converted, val.strip()))
|
||||||
else:
|
else:
|
||||||
|
@ -640,9 +647,9 @@ def load_from_file(file_path, package_context=''):
|
||||||
"""
|
"""
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
if package_context:
|
if package_context:
|
||||||
print "Load spec from", file_path, "into package [%s]"%package_context
|
print("Load spec from", file_path, "into package [%s]"%package_context)
|
||||||
else:
|
else:
|
||||||
print "Load spec from", file_path
|
print("Load spec from", file_path)
|
||||||
|
|
||||||
file_name = os.path.basename(file_path)
|
file_name = os.path.basename(file_path)
|
||||||
type_ = file_name[:-len(EXT)]
|
type_ = file_name[:-len(EXT)]
|
||||||
|
@ -660,7 +667,7 @@ def load_from_file(file_path, package_context=''):
|
||||||
try:
|
try:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
return (type_, load_from_string(text, package_context, type_, base_type_))
|
return (type_, load_from_string(text, package_context, type_, base_type_))
|
||||||
except MsgSpecException, e:
|
except MsgSpecException as e:
|
||||||
raise MsgSpecException('%s: %s'%(file_name, e))
|
raise MsgSpecException('%s: %s'%(file_name, e))
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -725,7 +732,7 @@ def is_registered(msg_type_name):
|
||||||
registered. NOTE: builtin types are not registered.
|
registered. NOTE: builtin types are not registered.
|
||||||
@rtype: bool
|
@rtype: bool
|
||||||
"""
|
"""
|
||||||
return REGISTERED_TYPES.has_key(msg_type_name)
|
return msg_type_name in REGISTERED_TYPES
|
||||||
|
|
||||||
def get_registered(msg_type_name, default_package=None):
|
def get_registered(msg_type_name, default_package=None):
|
||||||
"""
|
"""
|
||||||
|
@ -753,6 +760,6 @@ def register(msg_type_name, msg_spec):
|
||||||
@type msg_spec: L{MsgSpec}
|
@type msg_spec: L{MsgSpec}
|
||||||
"""
|
"""
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print "Register msg %s"%msg_type_name
|
print("Register msg %s"%msg_type_name)
|
||||||
REGISTERED_TYPES[msg_type_name] = msg_spec
|
REGISTERED_TYPES[msg_type_name] = msg_spec
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue