<commit_before>#!/usr/bin/env python

import rospy
from flexbe_msgs.msg import *
from rospkg import RosPack

import pickle
import zlib
import difflib
import os
import yaml
import xml.etree.ElementTree as ET

class BehaviorLauncher(object):

	def __init__(self):
		self._sub = rospy.Subscriber("/flexbe/request_behavior", BehaviorRequest, self._callback)
		self._pub = rospy.Publisher("/flexbe/start_behavior", BehaviorSelection, queue_size=100)
		self._mirror_pub = rospy.Publisher("/flexbe/mirror/structure", ContainerStructure, queue_size=100)

		self._rp = RosPack()
		self._behavior_lib = dict()

		behaviors_package = "flexbe_behaviors"
		if rospy.has_param("behaviors_package"):
			behaviors_package = rospy.get_param("behaviors_package")
		else:
			rospy.loginfo("Using default behaviors package: %s" % behaviors_package)

		manifest_folder = os.path.join(self._rp.get_path(behaviors_package), 'behaviors/')

		file_entries = [os.path.join(manifest_folder, filename) for filename in os.listdir(manifest_folder) if not filename.startswith('#')]
		manifests = sorted([xmlpath for xmlpath in file_entries if not os.path.isdir(xmlpath)])

		for i in range(len(manifests)):
			m = ET.parse(manifests[i]).getroot()
			e = m.find("executable")
			self._behavior_lib[i] = {
				"name": m.get("name"),
				"package": e.get("package_path").split(".")[0],
				"file": e.get("package_path").split(".")[1],
				"class": e.get("class")
			}

		rospy.loginfo("%d behaviors available, ready for start request." % len(self._behavior_lib.items()))


	def _callback(self, msg):
		try:
			be_id, behavior = next((id, be) for (id, be) in self._behavior_lib.items() if be["name"] == msg.behavior_name)
		except Exception as e:
			rospy.logwarn("Did not find behavior with requested name: %s" % msg.behavior_name)
			return

		rospy.loginfo("Request for behavior " + behavior["name"])

		be_selection = BehaviorSelection()
		be_selection.behavior_id = be_id
		be_selection.autonomy_level = msg.autonomy_level
		try:
			for k, v in zip(msg.arg_keys, msg.arg_values):
				if k.split('/')[-1].startswith('YAML:'):
					key = '/'.join(k.split('/')[0:-1]) + '/' + k.split('/')[-1].split(':')[1]
					path = v.split(':')[0]
					ns = v.split(':')[1]
					if path.startswith('~') or path.startswith('/'):
						yamlpath = os.path.expanduser(path)
					else:
						yamlpath = os.path.join(self._rp.get_path(path.split('/')[0]), '/'.join(path.split('/')[1:]))<commit_msg>[flexbe_widget] Correctly resolve file params of embedded behaviors<commit_after>#!/usr/bin/env python

import rospy
from flexbe_msgs.msg import *
from rospkg import RosPack

import pickle
import zlib
import difflib
import os
import yaml
import xml.etree.ElementTree as ET

class BehaviorLauncher(object):

	def __init__(self):
		self._sub = rospy.Subscriber("/flexbe/request_behavior", BehaviorRequest, self._callback)
		self._pub = rospy.Publisher("/flexbe/start_behavior", BehaviorSelection, queue_size=100)
		self._mirror_pub = rospy.Publisher("/flexbe/mirror/structure", ContainerStructure, queue_size=100)

		self._rp = RosPack()
		self._behavior_lib = dict()

		behaviors_package = "flexbe_behaviors"
		if rospy.has_param("behaviors_package"):
			behaviors_package = rospy.get_param("behaviors_package")
		else:
			rospy.loginfo("Using default behaviors package: %s" % behaviors_package)

		manifest_folder = os.path.join(self._rp.get_path(behaviors_package), 'behaviors/')

		file_entries = [os.path.join(manifest_folder, filename) for filename in os.listdir(manifest_folder) if not filename.startswith('#')]
		manifests = sorted([xmlpath for xmlpath in file_entries if not os.path.isdir(xmlpath)])

		for i in range(len(manifests)):
			m = ET.parse(manifests[i]).getroot()
			e = m.find("executable")
			self._behavior_lib[i] = {
				"name": m.get("name"),
				"package": e.get("package_path").split(".")[0],
				"file": e.get("package_path").split(".")[1],
				"class": e.get("class")
			}

		rospy.loginfo("%d behaviors available, ready for start request." % len(self._behavior_lib.items()))


	def _callback(self, msg):
		try:
			be_id, behavior = next((id, be) for (id, be) in self._behavior_lib.items() if be["name"] == msg.behavior_name)
		except Exception as e:
			rospy.logwarn("Did not find behavior with requested name: %s" % msg.behavior_name)
			return

		rospy.loginfo("Request for behavior " + behavior["name"])

		be_selection = BehaviorSelection()
		be_selection.behavior_id = be_id
		be_selection.autonomy_level = msg.autonomy_level
		try:
			for k, v in zip(msg.arg_keys, msg.arg_values):
				if k.startswith('/YAML:'):
					key = k.replace(r'^/YAML:', '/')
					path = v.split(':')[0]
					ns = v.split(':')[1]
					if path.startswith('~') or path.startswith('/'):
						yamlpath = os.path.expanduser(path)
					else:
						yamlpath = os.path.join(self._rp.get_path(path.split('/')[0]), '/'.join(path.split('/')[1:]))
{'fraction_non_alphanumeric': 0.15906767532202004, 'fraction_numerical': 0.005929257820486608, 'mean_word_length': 4.571753986332574, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2975516', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1696, 'n_words': 347}