38 from threading
import Lock
40 """ ros_loader contains methods for dynamically loading ROS message classes at
41 runtime. It's achieved by using roslib to load the manifest files for the
42 package that the respective class is contained in.
44 Methods typically return the requested class or instance, or None if not found
52 _manifest_lock = Lock()
57 Exception.__init__(self,
"%s is not a valid type string" % typestring)
61 def __init__(self, package, original_exception):
62 Exception.__init__(self,
63 "Unable to load the manifest for package %s. Caused by: %s"
64 % (package, str(original_exception))
69 def __init__(self, modname, subname, original_exception):
70 Exception.__init__(self,
71 "Unable to import %s.%s from package %s. Caused by: %s"
72 % (modname, subname, modname, str(original_exception))
77 def __init__(self, modname, subname, classname, original_exception):
78 Exception.__init__(self,
79 "Unable to import %s class %s from package %s. Caused by %s"
80 % (subname, classname, modname, str(original_exception))
85 """ Loads the message type specified.
87 Returns the loaded class, or throws exceptions on failure """
92 """ Loads the service type specified.
94 Returns the loaded class, or None on failure """
99 """ If not loaded, loads the specified type.
100 Then returns an instance of it, or None. """
106 """ If not loaded, loads the specified type.
107 Then returns an instance of it, or None. """
114 return cls._request_class()
119 return cls._response_class()
123 """ If not loaded, loads the specified msg class then returns an instance
126 Throws various exceptions if loading the msg class fails """
127 global _loaded_msgs, _msgs_lock
128 return _get_class(typestring,
"msg", _loaded_msgs, _msgs_lock)
132 """ If not loaded, loads the specified srv class then returns an instance
135 Throws various exceptions if loading the srv class fails """
136 global _loaded_srvs, _srvs_lock
137 return _get_class(typestring,
"srv", _loaded_srvs, _srvs_lock)
141 """ If not loaded, loads the specified class then returns an instance
144 Loaded classes are cached in the provided cache dict
146 Throws various exceptions if loading the msg class fails """
155 norm_typestring = modname +
"/" + classname
173 """ Loads the manifest and imports the module that contains the specified
176 Logic is similar to that of roslib.message.get_message_class, but we want
177 more expressive exceptions.
179 Returns the loaded module, or None on failure """
180 global loaded_modules
185 roslib.launcher.load_manifest(modname)
186 except Exception
as exc:
190 pypkg = __import__(
'%s.%s' % (modname, subname))
191 except Exception
as exc:
195 return getattr(getattr(pypkg, subname), classname)
196 except Exception
as exc:
201 """ Split the string the / delimiter and strip out empty strings
203 Performs similar logic to roslib.names.package_resource_name but is a bit
204 more forgiving about excess slashes
206 splits = [x
for x
in typestring.split(
"/")
if x]
218 """ Returns the value for the specified key from the cache.
219 Locks the lock before doing anything. Returns None if key not in cache """