3 from rospkg
import RosPack
4 import xml.etree.ElementTree
as ET
12 Provides access to all known behaviors. 22 Parses all ROS packages to update the internal behavior library. 25 for pkg
in self._rp.list():
26 for export
in self._rp._load_manifest(pkg).exports:
27 if export.tag ==
"flexbe_behaviors":
32 Recursively add all behavior manifests in the given folder to the internal library. 33 If a package name is specified, only manifests referring to this package are added. 36 @param path: Path of the folder to be traversed. 39 @param pkg: Optional name of a package to only add manifests referring to this package. 41 for entry
in os.listdir(path):
42 entry_path = os.path.join(path, entry)
43 if os.path.isdir(entry_path):
45 elif entry.endswith(
".xml")
and not entry.startswith(
"#"):
46 m = ET.parse(entry_path).getroot()
48 if (m.tag !=
"behavior" 49 or len(m.findall(
".//executable")) == 0
50 or m.find(
"executable").get(
"package_path")
is None 51 or len(m.find(
"executable").get(
"package_path").split(
".")) < 2):
53 e = m.find(
"executable")
54 if pkg
is not None and e.get(
"package_path").split(
".")[0] != pkg:
56 be_id = zlib.adler32(e.get(
"package_path").encode()) & 0x7fffffff
58 "name": m.get(
"name"),
59 "package":
".".join(e.get(
"package_path").split(
".")[:-1]),
60 "file": e.get(
"package_path").split(
".")[-1],
61 "class": e.get(
"class")
66 Provides the library entry corresponding to the given ID. 69 @param be_id: Behavior ID to look up. 71 @return Corresponding library entry or None if not found. 76 Logger.logwarn(
"Did not find ID %d in libary, updating..." % be_id)
78 return self._behavior_lib.get(be_id,
None)
82 Searches for a behavior with the given name and returns it along with its ID. 85 @param be_name: Behavior ID to look up. 87 @return Tuple (be_id, be_entry) corresponding to the name or (None, None) if not found. 89 find =
lambda: next((id, be)
for (id, be)
90 in self._behavior_lib.items()
91 if be[
"name"] == be_name)
95 Logger.logwarn(
"Did not find behavior '%s' in libary, updating..." % be_name)
100 Logger.logerr(
"Still cannot find behavior '%s' in libary after update, giving up!" % be_name)
105 Counts the available behaviors. 107 @return Number of behaviors. 113 Constructs a file path to the source code of corresponding to the given ID. 116 @param be_id: Behavior ID to look up. 119 @param add_tmp: Append "_tmp" to the file to consider a temporary version. 121 @return String containing the absolute path to the source code file. 128 module_path = __import__(be_entry[
"package"]).__path__[-1]
130 Logger.logwarn(
"Cannot import behavior package '%s', using 'rospack find' instead" % be_entry[
"package"])
132 module_path = os.path.join(self._rp.get_path(be_entry[
"package"]),
"src", be_entry[
"package"])
133 filename = be_entry[
"file"] +
'.py' if not add_tmp
else '_tmp.py' 134 return os.path.join(module_path, filename)
def get_sourcecode_filepath(self, be_id, add_tmp=False)
def find_behavior(self, be_name)
def get_behavior(self, be_id)
def count_behaviors(self)
def _add_behavior_manifests(self, path, pkg=None)